1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use embedded_hal::{
delay::DelayNs,
digital::{InputPin, OutputPin, PinState},
};
use crate::SensorError;
#[cfg(test)]
const DEFAULT_MAX_ATTEMPTS: usize = 10;
#[cfg(not(test))]
const DEFAULT_MAX_ATTEMPTS: usize = 10_000;
/// Common base struct for DHT11, DHT22 sensors.
pub struct Dht<P: InputPin + OutputPin, D: DelayNs> {
pub pin: P,
pub delay: D,
}
impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
pub fn new(pin: P, delay: D) -> Self {
Self { pin, delay }
}
/// Reads a byte (8 bits) from the sensor.
///
/// This method reads 8 bits sequentially from the sensor to construct a byte.
/// It follows the communication protocol of the DHT11/DHT22 sensors:
///
/// For each bit:
/// - Waits for the pin to go **high** (start of bit transmission).
/// - Delays for **30 microseconds** to sample the bit value.
/// - If the pin is **high** after the delay, the bit is interpreted as **'1'**.
/// - If the pin is **low**, the bit is interpreted as **'0'**.
/// - Waits for the pin to go **low** (end of bit transmission).
///
/// The bits are assembled into a byte, starting from the most significant bit (MSB).
///
/// # Returns
///
/// - `Ok(u8)`: The byte read from the sensor.
/// - `Err(SensorError<P::Error>)`: If a pin error occurs.
pub fn read_byte(&mut self) -> Result<u8, SensorError> {
let mut byte: u8 = 0;
for n in 0..8 {
match self.wait_until_state(PinState::High) {
Ok(_) => {}
Err(err) => return Err(err),
};
self.delay.delay_us(30);
let is_bit_1 = self.pin.is_high();
if is_bit_1.unwrap() {
let bit_mask = 1 << (7 - (n % 8));
byte |= bit_mask;
match self.wait_until_state(PinState::Low) {
Ok(_) => {}
Err(err) => return Err(err),
};
}
}
Ok(byte)
}
/// Waits until the pin reaches the specified state.
///
/// This helper function continuously polls the pin until it reaches the desired `PinState`.
/// It introduces a **1-microsecond delay** between each poll to prevent excessive CPU usage.
///
/// # Arguments
///
/// - `state`: The target `PinState` to wait for (`PinState::High` or `PinState::Low`).
///
/// # Returns
///
/// - `Ok(())`: When the pin reaches the desired state.
/// - `Err(SensorError::Timeout)`: If the desired state is not reached in time.
/// - `Err(SensorError::Io(...))`: If a pin error occurs while reading the pin state.
///
pub fn wait_until_state(&mut self, state: PinState) -> Result<(), SensorError> {
for _ in 0..DEFAULT_MAX_ATTEMPTS {
let is_state = match state {
PinState::Low => self.pin.is_low(),
PinState::High => self.pin.is_high(),
};
match is_state {
Ok(true) => return Ok(()),
Ok(false) => self.delay.delay_us(1),
Err(_) => return Err(SensorError::PinError),
}
}
Err(SensorError::Timeout)
}
}
#[cfg(test)]
mod tests {
extern crate std;
use std::io::ErrorKind;
use super::*;
use embedded_hal_mock::eh1::delay::NoopDelay as MockNoop;
use embedded_hal_mock::eh1::digital::{Mock, State, Transaction as PinTransaction};
use embedded_hal_mock::eh1::MockError;
#[test]
fn test_read_byte() {
// Set up the pin transactions to mock the behavior of the sensor during the reading of a byte.
// Each bit read from the sensor starts with a High state that lasts long enough
// to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0).
let expectations = [
// Bit 1 - 0
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 2 - 1
PinTransaction::get(State::High),
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 3 - 0
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 4 - 1
PinTransaction::get(State::High),
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 5 - 0
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 6 - 1
PinTransaction::get(State::High),
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 7 - 1
PinTransaction::get(State::High),
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
// Bit 8 - 1
PinTransaction::get(State::High),
PinTransaction::get(State::High),
PinTransaction::get(State::Low),
];
let mock_pin = Mock::new(&expectations);
let mock_delay = MockNoop::new();
let mut dht = Dht::new(mock_pin, mock_delay);
let result = dht.read_byte().unwrap();
assert_eq!(result, 0b01010111);
dht.pin.done();
}
#[test]
fn test_wait_until_state() {
let expectations = [
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::High),
];
let mock_pin = Mock::new(&expectations);
let mock_delay = MockNoop::new();
let mut dht = Dht::new(mock_pin, mock_delay);
let result = dht.wait_until_state(PinState::High);
assert!(result.is_ok());
dht.pin.done();
}
#[test]
fn test_wait_until_state_timeout_error() {
let expectations: [PinTransaction; DEFAULT_MAX_ATTEMPTS] = [
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
PinTransaction::get(State::Low),
];
let mock_pin = Mock::new(&expectations);
let mock_delay = MockNoop::new();
let mut dht = Dht::new(mock_pin, mock_delay);
let result = dht.wait_until_state(PinState::High);
assert!(matches!(result, Err(SensorError::Timeout)));
dht.pin.done();
}
#[test]
fn test_wait_until_state_pin_error() {
let err = MockError::Io(ErrorKind::NotConnected);
let expectations = [PinTransaction::get(State::High).with_error(err)];
let mock_pin = Mock::new(&expectations);
let mock_delay = MockNoop::new();
let mut dht = Dht::new(mock_pin, mock_delay);
let result = dht.wait_until_state(PinState::High);
assert!(matches!(result, Err(SensorError::PinError)));
dht.pin.done();
}
}