driver_pal/hal/
error.rs

1/// Error type combining SPI and Pin errors for utility
2#[derive(Debug)]
3pub enum HalError {
4    InvalidConfig,
5    InvalidSpiMode,
6    NoPin,
7    NoDriver,
8
9    #[cfg(feature = "hal-cp2130")]
10    Cp2130(driver_cp2130::Error),
11
12    #[cfg(feature = "hal-linux")]
13    Io(std::io::ErrorKind),
14
15    #[cfg(feature = "hal-linux")]
16    Sysfs(linux_embedded_hal::sysfs_gpio::Error),
17
18    #[cfg(feature = "hal-linux")]
19    SysfsPin(linux_embedded_hal::SysfsPinError),
20
21    #[cfg(feature = "hal-linux")]
22    Spi(linux_embedded_hal::SPIError),
23}
24
25impl HalError {
26    /// Check whether the HalError contains an underlying error type
27    pub fn is_inner(&self) -> bool {
28        use HalError::*;
29
30        match self {
31            InvalidConfig | InvalidSpiMode | NoPin => false,
32            _ => true,
33        }
34    }
35
36    /// Check whether the HalError signals no pin is available
37    pub fn is_no_pin(&self) -> bool {
38        use HalError::*;
39
40        match self {
41            NoPin => true,
42            _ => false,
43        }
44    }
45}
46
47#[cfg(feature = "hal-cp2130")]
48impl From<driver_cp2130::Error> for HalError {
49    fn from(e: driver_cp2130::Error) -> Self {
50        Self::Cp2130(e)
51    }
52}
53
54#[cfg(feature = "hal-linux")]
55impl From<std::io::Error> for HalError {
56    fn from(e: std::io::Error) -> Self {
57        Self::Io(e.kind())
58    }
59}
60
61#[cfg(feature = "hal-linux")]
62impl From<linux_embedded_hal::SysfsPinError> for HalError {
63    fn from(e: linux_embedded_hal::SysfsPinError) -> Self {
64        Self::SysfsPin(e)
65    }
66}
67
68#[cfg(feature = "hal-linux")]
69impl From<linux_embedded_hal::sysfs_gpio::Error> for HalError {
70    fn from(e: linux_embedded_hal::sysfs_gpio::Error) -> Self {
71        Self::Sysfs(e)
72    }
73}
74
75#[cfg(feature = "hal-linux")]
76impl From<linux_embedded_hal::SPIError> for HalError {
77    fn from(e: linux_embedded_hal::SPIError) -> Self {
78        Self::Spi(e)
79    }
80}
81
82#[cfg(feature = "hal-linux")]
83type BusDeviceError = embedded_hal_bus::spi::DeviceError<linux_embedded_hal::SPIError, linux_embedded_hal::SysfsPinError>;
84
85#[cfg(feature = "hal-linux")]
86impl From<BusDeviceError> for HalError {
87    fn from(e: BusDeviceError) -> Self {
88        match e {
89            BusDeviceError::Spi(e) => Self::Spi(e),
90            BusDeviceError::Cs(e) => Self::SysfsPin(e)
91        }
92    }
93}
94
95impl embedded_hal::spi::Error for HalError {
96    fn kind(&self) -> embedded_hal::spi::ErrorKind {
97        embedded_hal::spi::ErrorKind::Other
98    }
99}
100
101impl embedded_hal::digital::Error for HalError {
102    fn kind(&self) -> embedded_hal::digital::ErrorKind {
103        embedded_hal::digital::ErrorKind::Other
104    }
105}