ag_lcd/errors.rs
1/// Error type for [LcdDisplay][crate::display::LcdDisplay], returned by [LcdDisplay::error][crate::display::LcdDisplay::error]
2///
3/// LcdDisplay uses an internal error code rather than the standard rust
4/// Result pattern because there are only two places in LcdDisplay where
5/// an error is returned. Unfortunately, *every* public function invokes
6/// one of those places (which has an [Infallible][core::convert::Infallible] error type, no less) and
7/// would be forced to return a result or call unwrap/expect.
8///
9/// This led to a cluttered API in which users had to handle error conditions
10/// when calling functions like [clear][crate::display::LcdDisplay::clear] and [home][crate::display::LcdDisplay::home].
11/// An internal error code which could mostly be ignored except when debugging seemed like a better option.
12#[repr(u8)]
13#[derive(Clone, Eq, PartialEq)]
14pub enum Error {
15 /// No pin RS
16 NoPinRS = 0,
17 /// No pin EN
18 NoPinEN = 1,
19 /// No pin RW
20 NoPinRW = 2,
21 /// No pin D0
22 NoPinD0 = 3,
23 /// No pin D1
24 NoPinD1 = 4,
25 /// No pin D2
26 NoPinD2 = 5,
27 /// No pin D3
28 NoPinD3 = 6,
29 /// No pin D4
30 NoPinD4 = 7,
31 /// No pin D5
32 NoPinD5 = 8,
33 /// No pin D6
34 NoPinD6 = 9,
35 /// No pin D7
36 NoPinD7 = 10,
37 /// No error
38 None = 11,
39 /// [Bus mode][crate::display::Mode] is invalid or not set
40 InvalidMode = 12,
41 /// Invalid conversion from u8 to Error
42 InvalidCode = 13,
43}
44
45impl From<u8> for Error {
46 fn from(v: u8) -> Self {
47 match v {
48 0 => Error::NoPinRS,
49 1 => Error::NoPinEN,
50 2 => Error::NoPinRW,
51 3 => Error::NoPinD0,
52 4 => Error::NoPinD1,
53 5 => Error::NoPinD2,
54 6 => Error::NoPinD3,
55 7 => Error::NoPinD4,
56 8 => Error::NoPinD5,
57 9 => Error::NoPinD6,
58 10 => Error::NoPinD7,
59 11 => Error::None,
60 12 => Error::InvalidMode,
61 _ => Error::InvalidCode,
62 }
63 }
64}