11use embedded_hal:: {
22 delay:: DelayNs ,
3- digital:: { ErrorType , InputPin , OutputPin , PinState } ,
3+ digital:: { InputPin , OutputPin , PinState } ,
44} ;
55
66use crate :: SensorError ;
77
8+ #[ cfg( test) ]
9+ const DEFAULT_MAX_ATTEMPTS : usize = 10 ;
10+ #[ cfg( not( test) ) ]
811const DEFAULT_MAX_ATTEMPTS : usize = 10_000 ;
912
1013/// Common base struct for DHT11, DHT22 sensors.
@@ -39,13 +42,20 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
3942 pub fn read_byte ( & mut self ) -> Result < u8 , SensorError > {
4043 let mut byte: u8 = 0 ;
4144 for n in 0 ..8 {
42- let _ = self . wait_until_state ( PinState :: High ) ;
45+ match self . wait_until_state ( PinState :: High ) {
46+ Ok ( _) => { }
47+ Err ( err) => return Err ( err) ,
48+ } ;
49+
4350 self . delay . delay_us ( 30 ) ;
4451 let is_bit_1 = self . pin . is_high ( ) ;
4552 if is_bit_1. unwrap ( ) {
4653 let bit_mask = 1 << ( 7 - ( n % 8 ) ) ;
4754 byte |= bit_mask;
48- let _ = self . wait_until_state ( PinState :: Low ) ;
55+ match self . wait_until_state ( PinState :: Low ) {
56+ Ok ( _) => { }
57+ Err ( err) => return Err ( err) ,
58+ } ;
4959 }
5060 }
5161 Ok ( byte)
@@ -76,65 +86,60 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
7686 match is_state {
7787 Ok ( true ) => return Ok ( ( ) ) ,
7888 Ok ( false ) => self . delay . delay_us ( 1 ) ,
79- Err ( _) => return Err ( SensorError :: PinError )
89+ Err ( _) => return Err ( SensorError :: PinError ) ,
8090 }
8191 }
8292
8393 Err ( SensorError :: Timeout )
8494 }
8595}
8696
87-
8897#[ cfg( test) ]
8998mod tests {
99+ extern crate std;
100+ use std:: io:: ErrorKind ;
101+
90102 use super :: * ;
91- use embedded_hal_mock:: eh1:: digital:: { Mock , State , Transaction as PinTransaction } ;
92103 use embedded_hal_mock:: eh1:: delay:: NoopDelay as MockNoop ;
104+ use embedded_hal_mock:: eh1:: digital:: { Mock , State , Transaction as PinTransaction } ;
105+ use embedded_hal_mock:: eh1:: MockError ;
93106
94107 #[ test]
95108 fn test_read_byte ( ) {
96- // Set up the pin transactions to mock the behavior of the sensor during the reading of a byte.
97- // Each bit read from the sensor starts with a High state that lasts long enough
98- // to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0).
99- let expectations = [
100- // Bit 1 - 0
101- PinTransaction :: get ( State :: High ) ,
102- PinTransaction :: get ( State :: Low ) ,
103-
104- // Bit 2 - 1
105- PinTransaction :: get ( State :: High ) ,
106- PinTransaction :: get ( State :: High ) ,
107- PinTransaction :: get ( State :: Low ) ,
108-
109- // Bit 3 - 0
110- PinTransaction :: get ( State :: High ) ,
111- PinTransaction :: get ( State :: Low ) ,
112-
113- // Bit 4 - 1
114- PinTransaction :: get ( State :: High ) ,
115- PinTransaction :: get ( State :: High ) ,
116- PinTransaction :: get ( State :: Low ) ,
117-
118- // Bit 5 - 0
119- PinTransaction :: get ( State :: High ) ,
120- PinTransaction :: get ( State :: Low ) ,
121-
122- // Bit 6 - 1
123- PinTransaction :: get ( State :: High ) ,
124- PinTransaction :: get ( State :: High ) ,
125- PinTransaction :: get ( State :: Low ) ,
126-
127- // Bit 7 - 1
128- PinTransaction :: get ( State :: High ) ,
129- PinTransaction :: get ( State :: High ) ,
130- PinTransaction :: get ( State :: Low ) ,
131-
132- // Bit 8 - 1
133- PinTransaction :: get ( State :: High ) ,
134- PinTransaction :: get ( State :: High ) ,
135- PinTransaction :: get ( State :: Low ) ,
136-
137- ] ;
109+ // Set up the pin transactions to mock the behavior of the sensor during the reading of a byte.
110+ // Each bit read from the sensor starts with a High state that lasts long enough
111+ // to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0).
112+ let expectations = [
113+ // Bit 1 - 0
114+ PinTransaction :: get ( State :: High ) ,
115+ PinTransaction :: get ( State :: Low ) ,
116+ // Bit 2 - 1
117+ PinTransaction :: get ( State :: High ) ,
118+ PinTransaction :: get ( State :: High ) ,
119+ PinTransaction :: get ( State :: Low ) ,
120+ // Bit 3 - 0
121+ PinTransaction :: get ( State :: High ) ,
122+ PinTransaction :: get ( State :: Low ) ,
123+ // Bit 4 - 1
124+ PinTransaction :: get ( State :: High ) ,
125+ PinTransaction :: get ( State :: High ) ,
126+ PinTransaction :: get ( State :: Low ) ,
127+ // Bit 5 - 0
128+ PinTransaction :: get ( State :: High ) ,
129+ PinTransaction :: get ( State :: Low ) ,
130+ // Bit 6 - 1
131+ PinTransaction :: get ( State :: High ) ,
132+ PinTransaction :: get ( State :: High ) ,
133+ PinTransaction :: get ( State :: Low ) ,
134+ // Bit 7 - 1
135+ PinTransaction :: get ( State :: High ) ,
136+ PinTransaction :: get ( State :: High ) ,
137+ PinTransaction :: get ( State :: Low ) ,
138+ // Bit 8 - 1
139+ PinTransaction :: get ( State :: High ) ,
140+ PinTransaction :: get ( State :: High ) ,
141+ PinTransaction :: get ( State :: Low ) ,
142+ ] ;
138143
139144 let mock_pin = Mock :: new ( & expectations) ;
140145 let mock_delay = MockNoop :: new ( ) ;
@@ -143,7 +148,7 @@ mod tests {
143148
144149 let result = dht. read_byte ( ) . unwrap ( ) ;
145150 assert_eq ! ( result, 0b01010111 ) ;
146-
151+
147152 dht. pin . done ( ) ;
148153 }
149154
@@ -165,4 +170,45 @@ mod tests {
165170
166171 dht. pin . done ( ) ;
167172 }
173+
174+ #[ test]
175+ fn test_wait_until_state_timeout_error ( ) {
176+ let expectations: [ PinTransaction ; DEFAULT_MAX_ATTEMPTS ] = [
177+ PinTransaction :: get ( State :: Low ) ,
178+ PinTransaction :: get ( State :: Low ) ,
179+ PinTransaction :: get ( State :: Low ) ,
180+ PinTransaction :: get ( State :: Low ) ,
181+ PinTransaction :: get ( State :: Low ) ,
182+ PinTransaction :: get ( State :: Low ) ,
183+ PinTransaction :: get ( State :: Low ) ,
184+ PinTransaction :: get ( State :: Low ) ,
185+ PinTransaction :: get ( State :: Low ) ,
186+ PinTransaction :: get ( State :: Low ) ,
187+ ] ;
188+
189+ let mock_pin = Mock :: new ( & expectations) ;
190+ let mock_delay = MockNoop :: new ( ) ;
191+
192+ let mut dht = Dht :: new ( mock_pin, mock_delay) ;
193+
194+ let result = dht. wait_until_state ( PinState :: High ) ;
195+ assert ! ( matches!( result, Err ( SensorError :: Timeout ) ) ) ;
196+
197+ dht. pin . done ( ) ;
198+ }
199+
200+ #[ test]
201+ fn test_wait_until_state_pin_error ( ) {
202+ let err = MockError :: Io ( ErrorKind :: NotConnected ) ;
203+ let expectations = [ PinTransaction :: get ( State :: High ) . with_error ( err) ] ;
204+ let mock_pin = Mock :: new ( & expectations) ;
205+ let mock_delay = MockNoop :: new ( ) ;
206+
207+ let mut dht = Dht :: new ( mock_pin, mock_delay) ;
208+
209+ let result = dht. wait_until_state ( PinState :: High ) ;
210+ assert ! ( matches!( result, Err ( SensorError :: PinError ) ) ) ;
211+
212+ dht. pin . done ( ) ;
213+ }
168214}
0 commit comments