Fine tune example
This commit is contained in:
parent
e4e485b832
commit
7358665d93
2 changed files with 58 additions and 37 deletions
|
@ -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;
|
||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -21,6 +21,10 @@ pub struct DhtResult {
|
|||
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> {
|
||||
dht: DhtPio<'d, PIO, SM>,
|
||||
}
|
||||
|
@ -35,8 +39,11 @@ 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<DhtResult, DhtError> {
|
||||
impl<'d, PIO: Instance, const SM: usize> Dht for Dht22<'d, PIO, SM> {
|
||||
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
|
||||
async {
|
||||
let (raw_temp, raw_hum) = self.dht.read_data().await?;
|
||||
let mut final_t: f32 = (raw_temp & 0x7FFF) as f32;
|
||||
|
||||
|
@ -50,6 +57,7 @@ impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> {
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> {
|
||||
dht: DhtPio<'d, PIO, SM>,
|
||||
|
@ -65,8 +73,11 @@ 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<DhtResult, DhtError> {
|
||||
impl<'d, PIO: Instance, const SM: usize> Dht for Dht22Type2<'d, PIO, SM> {
|
||||
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;
|
||||
|
@ -77,6 +88,7 @@ impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> {
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Dht11<'d, PIO: Instance, const SM: usize> {
|
||||
dht: DhtPio<'d, PIO, SM>,
|
||||
|
@ -92,8 +104,11 @@ 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<DhtResult, DhtError> {
|
||||
impl<'d, PIO: Instance, const SM: usize> Dht for Dht11<'d, PIO, SM> {
|
||||
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
|
||||
async {
|
||||
let (t, h) = self.dht.read_data().await?;
|
||||
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32;
|
||||
|
||||
|
@ -107,3 +122,4 @@ impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> {
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue