sen66_interface/error.rs
1//! Errors emitted by this library.
2
3use embedded_hal::i2c;
4use thiserror::Error;
5
6/// Error variants emitted when interacting with the sensor.
7#[derive(Debug, Error, PartialEq)]
8pub enum Sen66Error<I2C: i2c::Error> {
9 /// Emitted when an error handling the data has occurred.
10 #[error(transparent)]
11 DataError(#[from] DataError),
12 /// Emitted when the sensor reports a failed forced CO2 recalibration.
13 #[error("The forced CO2 recalibration has failed.")]
14 FailedCo2Recalibration,
15 /// Emitted when an error from the I2C bus has occurred.
16 #[error(transparent)]
17 I2cError(#[from] I2C),
18 /// Emitted when the sensor has an set error flag.
19 #[error(transparent)]
20 DeviceError(#[from] DeviceError),
21 /// Emitted when a command is called in the wrong operating state. Use
22 /// [start_measurement](crate::asynch::Sen66::start_measurement) to
23 /// enter the measuring State, use [stop_measurement](crate::asynch::Sen66::stop_measurement) to enter the idle state.
24 #[error("Command called in invalid state: {0}")]
25 WrongState(&'static str),
26}
27
28#[cfg(feature = "defmt")]
29impl<I2C: i2c::Error> defmt::Format for Sen66Error<I2C> {
30 fn format(&self, f: defmt::Formatter) {
31 defmt::write!(f, "{}", self)
32 }
33}
34
35/// Error variants emitted when handling sensor data.
36#[derive(Debug, Error, PartialEq)]
37pub enum DataError {
38 /// Emitted when the CRC check for received data fails.
39 #[error("CRC check failed.")]
40 CrcFailed,
41 /// Emitted when a string is constructed that contains either non-ASCII values or no null
42 /// terminator within its bounds.
43 #[error("Received data is not a null-terminated ASCII string.")]
44 NotASCIIString,
45 /// Emitted when data received does not match the expected data size.
46 #[error("Buffer size received to wrong size for expected data.")]
47 ReceivedBufferWrongSize,
48 /// Emitted when a enum value received is not within the expected value range. Could occur if
49 /// the firmware of the sensor has received updates.
50 #[error("Unexpected Value for {parameter}: expected {expected} got {actual}")]
51 UnexpectedValueReceived {
52 /// Name of the parameter
53 parameter: &'static str,
54 /// Description of the expected value range
55 expected: &'static str,
56 /// Actual value received
57 actual: u16,
58 },
59 /// Emitted when a value is used to construct data send to the sensor, but the value is not in
60 /// the specified value's range. Adjust the argument to a value within the specified bounds.
61 #[error("{parameter} must be between {min} and {max} {unit}.")]
62 ValueOutOfRange {
63 /// Name of the parameter
64 parameter: &'static str,
65 /// Lower limit of the value
66 min: i32,
67 /// Upper limit of the value
68 max: i32,
69 /// Unit of the value
70 unit: &'static str,
71 },
72}
73
74#[cfg(feature = "defmt")]
75impl defmt::Format for DataError {
76 fn format(&self, f: defmt::Formatter) {
77 defmt::write!(f, "{}", self)
78 }
79}
80
81/// Encodes the error flags set in the [`DeviceStatusRegister`](crate::data::DeviceStatusRegister)
82#[derive(Debug, Error, PartialEq)]
83#[error(
84 "Sensor has errors set:
85 PM: {pm}
86 CO2: {co2}
87 Gas: {gas}
88 RHT: {rht}
89 Fan: {fan}"
90)]
91pub struct DeviceError {
92 /// PM sensor error present
93 pub pm: bool,
94 /// CO2 sensor error present
95 pub co2: bool,
96 /// Gas sensor error present
97 pub gas: bool,
98 /// RH/T sensor error present
99 pub rht: bool,
100 /// Fan error present
101 pub fan: bool,
102}