22
33use embedded_hal:: {
44 delay:: DelayNs ,
5- digital:: { ErrorType , InputPin , OutputPin , PinState }
5+ digital:: { ErrorType , InputPin , OutputPin , PinState } ,
66} ;
77
88pub struct Dht11 < P : InputPin + OutputPin , D : DelayNs > {
99 pin : P ,
1010 delay : D ,
1111}
1212
13+ pub struct SensorReading {
14+ pub humidity : u8 ,
15+ pub temperature : i8 ,
16+ }
17+
1318impl < P : InputPin + OutputPin , D : DelayNs > Dht11 < P , D > {
1419 pub fn new ( pin : P , delay : D ) -> Self {
15- Self { pin, delay }
20+ Self { pin, delay }
1621 }
1722
18- pub fn read ( & mut self ) -> Result < bool , <P as ErrorType >:: Error > {
23+ pub fn read ( & mut self ) -> Result < SensorReading , <P as ErrorType >:: Error > {
1924 // Start communication: pull pin low for 18ms, then release.
2025 let _ = self . pin . set_low ( ) ;
21- self . delay . delay_ms ( 18 ) ;
26+ self . delay . delay_ms ( 18 ) ;
2227 let _ = self . pin . set_high ( ) ;
2328
2429 // Wait for sensor to respond.
@@ -36,12 +41,13 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
3641 let checksum = self . read_byte ( ) ;
3742
3843 // TODO
39- panic ! ( "{:?} {:?} {:?} {:?} {:?}" , humidity_integer, humidity_decimal, temperature_integer, temperature_decimal, checksum) ;
40-
41- return self . pin . is_high ( ) ;
44+ // panic!("{:?} {:?} {:?} {:?} {:?}", humidity_integer, humidity_decimal, temperature_integer, temperature_decimal, checksum);
45+ Ok ( SensorReading {
46+ humidity : humidity_integer. unwrap ( ) ,
47+ temperature : temperature_integer. unwrap ( ) as i8 ,
48+ } )
4249 }
4350
44-
4551 fn read_byte ( & mut self ) -> Result < u8 , <P as ErrorType >:: Error > {
4652 let mut byte: u8 = 0 ;
4753 for n in 0 ..8 {
@@ -64,15 +70,22 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht11<P, D> {
6470 /// # Arguments
6571 ///
6672 /// * `state` - The target `PinState` to wait for (either `Low` or `High`).
67- fn wait_until_state ( & mut self , state : PinState ) -> Result < ( ) , <P as ErrorType >:: Error > {
73+ fn wait_until_state ( & mut self , state : PinState ) -> Result < ( ) , <P as ErrorType >:: Error > {
6874 loop {
6975 match state {
70- PinState :: Low => if self . pin . is_low ( ) ? { break ; } ,
71- PinState :: High => if self . pin . is_high ( ) ? { break ; }
76+ PinState :: Low => {
77+ if self . pin . is_low ( ) ? {
78+ break ;
79+ }
80+ }
81+ PinState :: High => {
82+ if self . pin . is_high ( ) ? {
83+ break ;
84+ }
85+ }
7286 } ;
7387 self . delay . delay_us ( 1 ) ;
7488 }
7589 Ok ( ( ) )
7690 }
7791}
78-
0 commit comments