From cee62d02a0d92eb4c4f06901e53f68cb33d72c03 Mon Sep 17 00:00:00 2001 From: zeldan Date: Thu, 24 Oct 2024 23:36:45 +0200 Subject: [PATCH] feat: handle negative values in calculations --- src/dht22.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/dht22.rs b/src/dht22.rs index f704c1f..4455347 100644 --- a/src/dht22.rs +++ b/src/dht22.rs @@ -48,8 +48,14 @@ impl Dht22 { let humidity_value = ((humidity_high as u16) << 8) | (humidity_low as u16); let humidity_percentage = humidity_value as f32 / 10.0; - let temperature_value = ((temperature_high as u16) << 8) | (temperature_low as u16); - let temperatue_percentage = temperature_value as f32 / 10.0; + let temperature_high_clean = temperature_high & 0x7F; // 0x7F = 0111 1111 + let temperature_value = ((temperature_high_clean as u16) << 8) | (temperature_low as u16); + let mut temperatue_percentage = temperature_value as f32 / 10.0; + + if temperature_high & 0x80 != 0 { + temperatue_percentage = -temperatue_percentage; + } + Ok(SensorReading { humidity: humidity_percentage,