From c17424a686fe2dc4f8928052c68deda57794a44d Mon Sep 17 00:00:00 2001 From: FliegendeWurst <2012gdwu+github@posteo.de> Date: Sun, 24 Sep 2023 12:36:50 +0200 Subject: [PATCH] Fix line requests in take_measurement --- src/bin/take_measurement.rs | 9 ++++++++- src/lib.rs | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/bin/take_measurement.rs b/src/bin/take_measurement.rs index 312839a..4aecddc 100644 --- a/src/bin/take_measurement.rs +++ b/src/bin/take_measurement.rs @@ -31,6 +31,10 @@ fn main() { .unwrap(); while temps.len() < 5 && attempts < 10 { if let Ok((rh, temp)) = raspi_oled::am2302_reading(&line) { + // TODO: try out gpio_am2302_rs! + //if let Ok(reading) = gpio_am2302_rs::try_read(26) { + //let rh = reading.humidity as i64; + //let temp = (reading.temperature * 10.0) as i64; if rh > 0 && temp < 500 { rhs.push(rh); temps.push(temp); @@ -43,10 +47,13 @@ fn main() { // median = hopefully no faulty readings temps.sort(); rhs.sort(); + let rh = rhs[rhs.len() / 2]; + let temp = temps[temps.len() / 2]; + println!("info: acquired {} readings (temps {:?}, rhs {:?}), using rh {} and temp {}", temps.len(), temps, rhs, rh, temp); database .execute( "INSERT INTO sensor_readings (time, humidity, celsius) VALUES (?1, ?2, ?3)", - params![time.as_secs(), rhs[rhs.len() / 2], temps[temps.len() / 2]], + params![time.as_secs(), rh, temp], ) .unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index 720db7f..f2cd417 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,6 +76,7 @@ fn read_events(line: &gpio_cdev::Line, timeout: std::time::Duration) -> Result for SensorError { } pub fn am2302_reading(line: &Line) -> Result<(u16, u16), SensorError> { - line.request(LineRequestFlags::OUTPUT, 1, "rust-am2302").unwrap(); + let out = line.request(LineRequestFlags::OUTPUT, 1, "rust-am2302").unwrap(); + out.set_value(1)?; sleep(Duration::from_millis(500)); set_max_priority(); // set low for 20 ms - if let Err(e) = line.request(LineRequestFlags::OUTPUT, 0, "rust-am2302") { - set_normal_priority(); - return Err(SensorError::Io(e)); - } + out.set_value(0)?; sleep(Duration::from_millis(3)); + drop(out); let events = read_events(&line, Duration::from_secs(1)); println!("{:?} {:?}", events, events.as_ref().map(|x| x.len()));