From 86bf15760352a5f29e07e4e3d0d2b0e48904f405 Mon Sep 17 00:00:00 2001 From: Fraser Savage Date: Mon, 24 Oct 2016 19:09:36 +0100 Subject: [PATCH] Adds humidity reading to the climate module --- climate-si7020/src/lib.rs | 12 +++++++++++- climate-si7020/src/main.rs | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/climate-si7020/src/lib.rs b/climate-si7020/src/lib.rs index d30829d..aa152df 100644 --- a/climate-si7020/src/lib.rs +++ b/climate-si7020/src/lib.rs @@ -11,7 +11,7 @@ use std::time::Duration; #[derive(Copy, Clone)] enum Command { TempHold = 0xE3, - + RHHold = 0xE5, ReadId1 = 0xFA, ReadId2 = 0x0F, ReadId3 = 0xFC, @@ -83,4 +83,14 @@ impl<'a> Climate<'a> { Ok(temp) } + + pub fn read_humidity(&mut self) -> io::Result { + let mut buf = [0; 2]; + try!(self.read(&[Command::RHHold], &mut buf)); + + let raw_humidity = ((buf[0] as u16) << 8) + (buf[1] as u16); + let humidity = ((raw_humidity as f64) * HUMIDITY_SLOPE) - HUMIDITY_OFFSET; + + Ok(humidity) + } } diff --git a/climate-si7020/src/main.rs b/climate-si7020/src/main.rs index 4d5f085..d3d0395 100644 --- a/climate-si7020/src/main.rs +++ b/climate-si7020/src/main.rs @@ -17,7 +17,7 @@ fn main() { println!("Reading climate sensor... (Press CTRL + C to stop)"); loop { println!("Temperature (Fahrenheit): {:?}", climate.read_temperature()); - + println!("Humidity: {:?}", climate.read_humidity()); // Continue the loop after sleeping for 100ms. sleep(Duration::from_millis(100)); }