@@ -39,13 +39,20 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
3939 pub fn read_byte ( & mut self ) -> Result < u8 , SensorError > {
4040 let mut byte: u8 = 0 ;
4141 for n in 0 ..8 {
42- let _ = self . wait_until_state ( PinState :: High ) ;
42+ match self . wait_until_state ( PinState :: High ) {
43+ Ok ( _) => { }
44+ Err ( err) => return Err ( err) ,
45+ } ;
46+
4347 self . delay . delay_us ( 30 ) ;
4448 let is_bit_1 = self . pin . is_high ( ) ;
4549 if is_bit_1. unwrap ( ) {
4650 let bit_mask = 1 << ( 7 - ( n % 8 ) ) ;
4751 byte |= bit_mask;
48- let _ = self . wait_until_state ( PinState :: Low ) ;
52+ match self . wait_until_state ( PinState :: Low ) {
53+ Ok ( _) => { }
54+ Err ( err) => return Err ( err) ,
55+ } ;
4956 }
5057 }
5158 Ok ( byte)
@@ -76,65 +83,56 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht<P, D> {
7683 match is_state {
7784 Ok ( true ) => return Ok ( ( ) ) ,
7885 Ok ( false ) => self . delay . delay_us ( 1 ) ,
79- Err ( _) => return Err ( SensorError :: PinError )
86+ Err ( _) => return Err ( SensorError :: PinError ) ,
8087 }
8188 }
8289
8390 Err ( SensorError :: Timeout )
8491 }
8592}
8693
87-
8894#[ cfg( test) ]
8995mod tests {
9096 use super :: * ;
91- use embedded_hal_mock:: eh1:: digital:: { Mock , State , Transaction as PinTransaction } ;
9297 use embedded_hal_mock:: eh1:: delay:: NoopDelay as MockNoop ;
98+ use embedded_hal_mock:: eh1:: digital:: { Mock , State , Transaction as PinTransaction } ;
9399
94100 #[ test]
95101 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- ] ;
102+ // Set up the pin transactions to mock the behavior of the sensor during the reading of a byte.
103+ // Each bit read from the sensor starts with a High state that lasts long enough
104+ // to signify the bit, followed by reading whether it stays High (bit 1) or goes Low (bit 0).
105+ let expectations = [
106+ // Bit 1 - 0
107+ PinTransaction :: get ( State :: High ) ,
108+ PinTransaction :: get ( State :: Low ) ,
109+ // Bit 2 - 1
110+ PinTransaction :: get ( State :: High ) ,
111+ PinTransaction :: get ( State :: High ) ,
112+ PinTransaction :: get ( State :: Low ) ,
113+ // Bit 3 - 0
114+ PinTransaction :: get ( State :: High ) ,
115+ PinTransaction :: get ( State :: Low ) ,
116+ // Bit 4 - 1
117+ PinTransaction :: get ( State :: High ) ,
118+ PinTransaction :: get ( State :: High ) ,
119+ PinTransaction :: get ( State :: Low ) ,
120+ // Bit 5 - 0
121+ PinTransaction :: get ( State :: High ) ,
122+ PinTransaction :: get ( State :: Low ) ,
123+ // Bit 6 - 1
124+ PinTransaction :: get ( State :: High ) ,
125+ PinTransaction :: get ( State :: High ) ,
126+ PinTransaction :: get ( State :: Low ) ,
127+ // Bit 7 - 1
128+ PinTransaction :: get ( State :: High ) ,
129+ PinTransaction :: get ( State :: High ) ,
130+ PinTransaction :: get ( State :: Low ) ,
131+ // Bit 8 - 1
132+ PinTransaction :: get ( State :: High ) ,
133+ PinTransaction :: get ( State :: High ) ,
134+ PinTransaction :: get ( State :: Low ) ,
135+ ] ;
138136
139137 let mock_pin = Mock :: new ( & expectations) ;
140138 let mock_delay = MockNoop :: new ( ) ;
@@ -143,7 +141,7 @@ mod tests {
143141
144142 let result = dht. read_byte ( ) . unwrap ( ) ;
145143 assert_eq ! ( result, 0b01010111 ) ;
146-
144+
147145 dht. pin . done ( ) ;
148146 }
149147
0 commit comments