Skip to content

Commit

Permalink
feat: handle negative values in calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
zeldan committed Oct 24, 2024
1 parent b864c23 commit cee62d0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/dht22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ impl<P: InputPin + OutputPin, D: DelayNs> Dht22<P, D> {
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,
Expand Down

0 comments on commit cee62d0

Please sign in to comment.