From 7358665d93fd7e8d9831d2fbfb4e4bb5e5cac2f8 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 14 Mar 2024 10:25:12 +0100 Subject: [PATCH] Fine tune example --- example/rp_pico_dht22.rs | 25 ++++++++------ src/lib.rs | 70 ++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/example/rp_pico_dht22.rs b/example/rp_pico_dht22.rs index 95be12b..740bca0 100644 --- a/example/rp_pico_dht22.rs +++ b/example/rp_pico_dht22.rs @@ -13,7 +13,7 @@ use embassy_rp::{ }; use embassy_time::Timer; -use dht_pio::{Dht22Type2, DhtError}; +use dht_pio::{Dht, Dht22, DhtError}; use embassy_serial; #[panic_handler] @@ -54,7 +54,7 @@ async fn main(_spawner: Spawner) { } = Pio::new(pio, Irqs); // create DHT22 - let mut dht = Dht22Type2::new(&mut common, sm1, p.PIN_0); + let mut dht = Dht22::new(&mut common, sm1, p.PIN_0); let dht_reading = async { loop { @@ -65,20 +65,25 @@ async fn main(_spawner: Spawner) { .send_number(reading.temperature as u32, 10) .await .unwrap(); + serial.send_msg("\n").await.unwrap(); serial.send_msg("humid:\n").await.unwrap(); serial .send_number(reading.humidity as u32, 10) .await .unwrap(); + serial.send_msg("\n").await.unwrap(); } - Err(err) => serial - .send_msg(match err { - DhtError::Timeout => "dht timeout error\n\r", - DhtError::CrcMismatch(_, _) => "dht checksum error\n\r", - DhtError::ReadError => "dht read error\n\r", - }) - .await - .unwrap(), + Err(err) => match err { + DhtError::Timeout => serial.send_msg("dht timeout error\n").await.unwrap(), + DhtError::CrcMismatch(data, crc) => { + serial.send_msg("dht checksum error\n").await.unwrap(); + serial.send_number(data, 10).await.unwrap(); + serial.send_msg("\n").await.unwrap(); + serial.send_number(crc, 10).await.unwrap(); + serial.send_msg("\n").await.unwrap(); + } + DhtError::ReadError => serial.send_msg("dht read error\n").await.unwrap(), + }, } Timer::after_secs(3).await; diff --git a/src/lib.rs b/src/lib.rs index d056d66..e10dc0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,10 @@ pub struct DhtResult { pub humidity: f32, } +pub trait Dht { + fn read(&mut self) -> impl core::future::Future>; +} + pub struct Dht22<'d, PIO: Instance, const SM: usize> { dht: DhtPio<'d, PIO, SM>, } @@ -35,19 +39,23 @@ impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> { dht: DhtPio::new(pio, sm, dht_pin), } } +} - pub async fn read(&mut self) -> Result { - let (raw_temp, raw_hum) = self.dht.read_data().await?; - let mut final_t: f32 = (raw_temp & 0x7FFF) as f32; +impl<'d, PIO: Instance, const SM: usize> Dht for Dht22<'d, PIO, SM> { + fn read(&mut self) -> impl core::future::Future> { + async { + let (raw_temp, raw_hum) = self.dht.read_data().await?; + let mut final_t: f32 = (raw_temp & 0x7FFF) as f32; - if (raw_temp & 0x8000) > 0 { - final_t *= -1.0; + if (raw_temp & 0x8000) > 0 { + final_t *= -1.0; + } + + Ok(DhtResult { + temperature: final_t / 10.0, + humidity: raw_hum as f32 / 10.0, + }) } - - Ok(DhtResult { - temperature: final_t / 10.0, - humidity: raw_hum as f32 / 10.0, - }) } } @@ -65,16 +73,20 @@ impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> { dht: DhtPio::new(pio, sm, dht_pin), } } +} - pub async fn read(&mut self) -> Result { - let (raw_temp, raw_hum) = self.dht.read_data().await?; +impl<'d, PIO: Instance, const SM: usize> Dht for Dht22Type2<'d, PIO, SM> { + fn read(&mut self) -> impl core::future::Future> { + async { + let (raw_temp, raw_hum) = self.dht.read_data().await?; - let tmp: i16 = raw_temp as i16; + let tmp: i16 = raw_temp as i16; - Ok(DhtResult { - temperature: (tmp as f32) / 10.0, - humidity: raw_hum as f32 / 10.0, - }) + Ok(DhtResult { + temperature: (tmp as f32) / 10.0, + humidity: raw_hum as f32 / 10.0, + }) + } } } @@ -92,18 +104,22 @@ impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> { dht: DhtPio::new(pio, sm, dht_pin), } } +} - pub async fn read(&mut self) -> Result { - let (t, h) = self.dht.read_data().await?; - let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32; +impl<'d, PIO: Instance, const SM: usize> Dht for Dht11<'d, PIO, SM> { + fn read(&mut self) -> impl core::future::Future> { + async { + let (t, h) = self.dht.read_data().await?; + let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32; - if (t & 0x8000) > 0 { - final_t *= -1.0; + if (t & 0x8000) > 0 { + final_t *= -1.0; + } + + Ok(DhtResult { + temperature: final_t, + humidity: (h >> 8) as f32, + }) } - - Ok(DhtResult { - temperature: final_t, - humidity: (h >> 8) as f32, - }) } }