Skip to main content

pamoja_sensors/
error.rs

1//! The error type shared by the sensor drivers.
2
3/// What can go wrong turning a part's raw bytes into a reading.
4///
5/// Most of these drivers only ever decode well-formed register values and so cannot
6/// fail, but parts that carry their own integrity check report a mismatch here so the
7/// caller re-reads rather than trusting corrupted data.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum SensorError {
10    /// A device's own checksum did not match the bytes it covered, so the read was
11    /// corrupted on the bus and must be repeated. Returned, for example, when a
12    /// DS18B20 scratchpad's CRC byte disagrees with the data bytes.
13    Crc,
14    /// A register field holds a code the datasheet leaves undefined, so the value
15    /// cannot have come from a correctly working part. Returned, for example, when an
16    /// HDC1080 configuration register carries the unassigned humidity-resolution code.
17    Invalid,
18    /// A device's identification registers did not carry the values its datasheet
19    /// fixes, so a different part, or nothing at all, answered at that address and
20    /// its readings must not be trusted. Returned, for example, when an INA226's
21    /// manufacturer or die ID register reads something other than TI's 0x5449 and
22    /// 0x226.
23    Identity,
24}
25
26impl core::fmt::Display for SensorError {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            SensorError::Crc => f.write_str("sensor checksum mismatch"),
30            SensorError::Invalid => f.write_str("sensor register field holds an undefined code"),
31            SensorError::Identity => f.write_str("sensor identification mismatch"),
32        }
33    }
34}
35
36// `core::error::Error` rather than `std::error::Error`, so a caller on a
37// microcontroller gets the same trait a caller on a gateway does.
38impl core::error::Error for SensorError {}