dht_hal/
kind.rs

1pub trait DhtKind {
2    #[doc(hidden)]
3    const START_DELAY_US: u16;
4
5    #[doc(hidden)]
6    fn temp_celcius(integral: u8, decimal: u8) -> f32;
7
8    #[doc(hidden)]
9    fn humidity_percent(integral: u8, decimal: u8) -> f32;
10}
11
12pub struct Dht11 {
13    _p: (),
14}
15
16pub struct Dht22 {
17    _p: (),
18}
19
20impl DhtKind for Dht11 {
21    // Datasheet says 20 ms.
22    const START_DELAY_US: u16 = 20 * 1000;
23
24    fn temp_celcius(integral: u8, decimal: u8) -> f32 {
25        // XXX(eliza): this is kind of copied from the Adafruit driver implementation,
26        // which doesn't really explain what it's doing.
27        let mut temp = integral as f32;
28        if decimal & 0x80 != 0 {
29            temp = -1.0 - temp;
30        }
31        temp + (decimal & 0x0f) as f32 * 0.1
32    }
33
34    fn humidity_percent(integral: u8, decimal: u8) -> f32 {
35        integral as f32 + decimal as f32 * 0.1
36    }
37}
38
39impl DhtKind for Dht22 {
40    // Datasheet says "at least" 1 ms, so we'll delay for just over 1ms.
41    const START_DELAY_US: u16 = 1100;
42
43    fn temp_celcius(integral: u8, decimal: u8) -> f32 {
44        let mut temp = (((integral & 0x7F) as u16) << 8 | decimal as u16) as f32;
45        temp *= 0.1;
46        if integral & 0x80 != 0 {
47            temp *= -1.0;
48        }
49        temp
50    }
51
52    fn humidity_percent(integral: u8, decimal: u8) -> f32 {
53        ((integral as u16) << 8 | decimal as u16) as f32 * 0.1
54    }
55}