aquarium_control/sensors/
dht_error.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use thiserror::Error;
10#[derive(Debug, Error)]
11/// Enum codifies the errors the program may encounter in communication with DHT sensor.
12pub enum DhtError {
13    /// Vcc pin acquisition failed
14    #[cfg(all(target_os = "linux", feature = "target_hw"))]
15    #[error(
16        "[{location}] Could not get pin {vcc_pin_nr} from GPIO interface for DHT power supply."
17    )]
18    VccPinAcquisitionFailed {
19        location: String,
20        vcc_pin_nr: u8,
21
22        #[source]
23        source: rppal::gpio::Error,
24    },
25
26    /// Data pin acquisition for output failed
27    #[cfg(all(target_os = "linux", feature = "target_hw"))]
28    #[error("[{location}] Failure to acquire pin {data_pin_nr} for triggering DHT sensor")]
29    DataPinOutputAcquisitionFailed {
30        location: String,
31        data_pin_nr: u8,
32
33        #[source]
34        source: rppal::gpio::Error,
35    },
36
37    /// Data pin acquisition for input failed
38    #[cfg(all(target_os = "linux", feature = "target_hw"))]
39    #[error("[{location}] Failure to acquire pin {input_pin_nr} for reading from DHT sensor")]
40    DataPinInputAcquisitionFailed {
41        location: String,
42        input_pin_nr: u8,
43
44        #[source]
45        source: rppal::gpio::Error,
46    },
47
48    /// Could not set interrupt
49    #[cfg(all(target_os = "linux", feature = "target_hw"))]
50    #[error("[{0}] Could not set interrupt")]
51    CouldNotSetInterrupt(String),
52
53    /// Could not clear the interrupt
54    #[cfg(all(target_os = "linux", feature = "target_hw"))]
55    #[error("[{0}] Could not clear the interrupt")]
56    CouldNotClearInterrupt(String),
57
58    // execution of interrupt failed
59    #[cfg(all(target_os = "linux", feature = "target_hw"))]
60    #[error("[{0}] Execution of interrupt failed")]
61    InterruptFailed(String),
62
63    // transmission yields a wrong number of valid bits
64    #[cfg(all(target_os = "linux", feature = "target_hw"))]
65    #[error("[{0}] Incorrect number of valid ticks received: target={1}, measured={2}.")]
66    WrongNumberOfValidTicks(String, usize, usize),
67
68    /// calculated checksum does not match the received value
69    #[error("[{0}] Calculated checksum ({1}) does not match the received value ({2})")]
70    ChecksumError(String, u8, u8),
71
72    /// timeout during transmission of data
73    #[allow(unused)]
74    #[error("[{0}] Timeout during transmission of data")]
75    Timeout(String),
76
77    /// the temperature value is out of range
78    #[error("[{0}] The temperature value is out of range (measured {1}, limits are {2}, {3})")]
79    TemperatureOutOfRange(String, f32, f32, f32),
80
81    /// humidity value is out of range
82    #[error("[{0}] The humidity is out of range (measured {1}, limits are {2}, {3})")]
83    HumidityOutOfRange(String, f32, f32, f32),
84
85    /// reported from low-level reading when called without gpio lib handle available
86    #[allow(unused)] // used in conditionally compiled code
87    #[error("[{0}] Gpio handle has not been provided. Check the configuration.")]
88    GpioHandleNotProvided(String),
89
90    /// reported from low-level reading function substitute for non-target platform
91    #[allow(unused)] // used in conditionally compiled code
92    #[error("[{0}] Dht driver has not been compiled into the SW for this platform.")]
93    IncorrectPlatform(String),
94
95    /// The measurement has been repeated several times without success.
96    #[allow(unused)] // used in conditionally compiled code
97    #[error("[{location}] Failed to read from DHT sensor (after {retry_counter} retries")]
98    FailedAfterRetries {
99        location: String,
100        retry_counter: u64,
101
102        #[source]
103        source: Box<DhtError>,
104    },
105}