use embedded_hal::i2c;
use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
pub enum Sen66Error<I2C: i2c::Error> {
#[error(transparent)]
DataError(#[from] DataError),
#[error("The forced CO2 recalibration has failed.")]
FailedCo2Recalibration,
#[error(transparent)]
I2cError(#[from] I2C),
#[error(transparent)]
DeviceError(#[from] DeviceError),
#[error("Command called in invalid state: {0}")]
WrongState(&'static str),
}
#[cfg(feature = "defmt")]
impl<I2C: i2c::Error> defmt::Format for Sen66Error<I2C> {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}", self)
}
}
#[derive(Debug, Error, PartialEq)]
pub enum DataError {
#[error("CRC check failed.")]
CrcFailed,
#[error("Received data is not a null-terminated ASCII string.")]
NotASCIIString,
#[error("Buffer size received to wrong size for expected data.")]
ReceivedBufferWrongSize,
#[error("Unexpected Value for {parameter}: expected {expected} got {actual}")]
UnexpectedValueReceived {
parameter: &'static str,
expected: &'static str,
actual: u16,
},
#[error("{parameter} must be between {min} and {max} {unit}.")]
ValueOutOfRange {
parameter: &'static str,
min: i32,
max: i32,
unit: &'static str,
},
}
#[cfg(feature = "defmt")]
impl defmt::Format for DataError {
fn format(&self, f: defmt::Formatter) {
defmt::write!(f, "{}", self)
}
}
#[derive(Debug, Error, PartialEq)]
#[error(
"Sensor has errors set:
PM: {pm}
CO2: {co2}
Gas: {gas}
RHT: {rht}
Fan: {fan}"
)]
pub struct DeviceError {
pub pm: bool,
pub co2: bool,
pub gas: bool,
pub rht: bool,
pub fan: bool,
}