Fine tune example

This commit is contained in:
hodasemi 2024-03-14 10:25:12 +01:00
parent e4e485b832
commit 7358665d93
2 changed files with 58 additions and 37 deletions

View file

@ -13,7 +13,7 @@ use embassy_rp::{
}; };
use embassy_time::Timer; use embassy_time::Timer;
use dht_pio::{Dht22Type2, DhtError}; use dht_pio::{Dht, Dht22, DhtError};
use embassy_serial; use embassy_serial;
#[panic_handler] #[panic_handler]
@ -54,7 +54,7 @@ async fn main(_spawner: Spawner) {
} = Pio::new(pio, Irqs); } = Pio::new(pio, Irqs);
// create DHT22 // 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 { let dht_reading = async {
loop { loop {
@ -65,20 +65,25 @@ async fn main(_spawner: Spawner) {
.send_number(reading.temperature as u32, 10) .send_number(reading.temperature as u32, 10)
.await .await
.unwrap(); .unwrap();
serial.send_msg("\n").await.unwrap();
serial.send_msg("humid:\n").await.unwrap(); serial.send_msg("humid:\n").await.unwrap();
serial serial
.send_number(reading.humidity as u32, 10) .send_number(reading.humidity as u32, 10)
.await .await
.unwrap(); .unwrap();
serial.send_msg("\n").await.unwrap();
} }
Err(err) => serial Err(err) => match err {
.send_msg(match err { DhtError::Timeout => serial.send_msg("dht timeout error\n").await.unwrap(),
DhtError::Timeout => "dht timeout error\n\r", DhtError::CrcMismatch(data, crc) => {
DhtError::CrcMismatch(_, _) => "dht checksum error\n\r", serial.send_msg("dht checksum error\n").await.unwrap();
DhtError::ReadError => "dht read error\n\r", serial.send_number(data, 10).await.unwrap();
}) serial.send_msg("\n").await.unwrap();
.await serial.send_number(crc, 10).await.unwrap();
.unwrap(), serial.send_msg("\n").await.unwrap();
}
DhtError::ReadError => serial.send_msg("dht read error\n").await.unwrap(),
},
} }
Timer::after_secs(3).await; Timer::after_secs(3).await;

View file

@ -21,6 +21,10 @@ pub struct DhtResult {
pub humidity: f32, pub humidity: f32,
} }
pub trait Dht {
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>>;
}
pub struct Dht22<'d, PIO: Instance, const SM: usize> { pub struct Dht22<'d, PIO: Instance, const SM: usize> {
dht: DhtPio<'d, PIO, SM>, 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), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
}
pub async fn read(&mut self) -> Result<DhtResult, DhtError> { impl<'d, PIO: Instance, const SM: usize> Dht for Dht22<'d, PIO, SM> {
let (raw_temp, raw_hum) = self.dht.read_data().await?; fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
let mut final_t: f32 = (raw_temp & 0x7FFF) as f32; 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 { if (raw_temp & 0x8000) > 0 {
final_t *= -1.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), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
}
pub async fn read(&mut self) -> Result<DhtResult, DhtError> { impl<'d, PIO: Instance, const SM: usize> Dht for Dht22Type2<'d, PIO, SM> {
let (raw_temp, raw_hum) = self.dht.read_data().await?; fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
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 { Ok(DhtResult {
temperature: (tmp as f32) / 10.0, temperature: (tmp as f32) / 10.0,
humidity: raw_hum 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), dht: DhtPio::new(pio, sm, dht_pin),
} }
} }
}
pub async fn read(&mut self) -> Result<DhtResult, DhtError> { impl<'d, PIO: Instance, const SM: usize> Dht for Dht11<'d, PIO, SM> {
let (t, h) = self.dht.read_data().await?; fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32; async {
let (t, h) = self.dht.read_data().await?;
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32;
if (t & 0x8000) > 0 { if (t & 0x8000) > 0 {
final_t *= -1.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,
})
} }
} }