rustrum / dht-hal-drv

DHT11 DHT22 sensors platform agnostic driver based on embeded-hal
Other
4 stars 4 forks source link

Temperature and Humidity Calculation Issue - Fixed by Qualifying Enum Variants in Match #3

Closed voteblake closed 2 years ago

voteblake commented 4 years ago

First, thank for your work on this library. It's been working great for reading my DHT22.

Issue

The code as-is populates the DhtValue struct with the raw u8s from my sensor. Calling the temperature() or humidity() functions results in me being returned either a 0.0 or 1.0 regardless of the underlying raw data in the DhtValue.value array.

use dht_hal_drv;
use spin_sleep;

# Setup I2C pins

let dht_read = dht_hal_drv::dht_read(dht_hal_drv::DhtType::DHT22, &mut dht22pin, &mut |d| {
                spin_sleep::sleep(time::Duration::from_micros(d as u64));
});
match dht_read {
    Ok(reading) => {
        println!("Reading: {:?}", &reading);
        println!("Temp: {:.1}\t Hum: {:.1}", reading.temperature(), reading.humidity());
    }
    Err(e) => println!("Error: {:?}", e),
}

Returns:

Reading: DhtValue { dht_type: DHT22, value: [1, 173, 0, 222, 140] }
Temp: 0.0        Hum: 1.0

Fix

Looking in to this I noticed the crate compiles but does emit some warnings:

warning[E0170]: pattern binding `DHT11` is named the same as one of the variants of the type `DhtType`
   --> dht-hal-drv/src/lib.rs:125:13
    |
125 |             DHT11 => self.value[2] as f32,
    |             ^^^^^ help: to match on the variant, qualify the path: `DhtType::DHT11`
    |
    = note: `#[warn(bindings_with_variant_name)]` on by default

Changing the values in the match statements in the temperature() and humidity() functions to the fully qualified enum variant addresses these and other warnings:

DhtType::DHT11 => self.value[0] as f32,

With this in place the output is now as expected:

Reading: DhtValue { dht_type: DHT22, value: [1, 176, 0, 221, 142] }
Temp: 22.1       Hum: 43.2
rumatoest commented 4 years ago

Thanks will merge it in next release.

obourgain commented 3 years ago

Hello, any plans to release this? Thank you

rumatoest commented 2 years ago

Something was done about it