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 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;
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -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,8 +39,11 @@ 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> {
|
||||||
|
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
|
||||||
|
async {
|
||||||
let (raw_temp, raw_hum) = self.dht.read_data().await?;
|
let (raw_temp, raw_hum) = self.dht.read_data().await?;
|
||||||
let mut final_t: f32 = (raw_temp & 0x7FFF) as f32;
|
let mut final_t: f32 = (raw_temp & 0x7FFF) as f32;
|
||||||
|
|
||||||
|
@ -49,6 +56,7 @@ impl<'d, PIO: Instance, const SM: usize> Dht22<'d, PIO, SM> {
|
||||||
humidity: raw_hum as f32 / 10.0,
|
humidity: raw_hum as f32 / 10.0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> {
|
pub struct Dht22Type2<'d, PIO: Instance, const SM: usize> {
|
||||||
|
@ -65,8 +73,11 @@ 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> {
|
||||||
|
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
|
||||||
|
async {
|
||||||
let (raw_temp, raw_hum) = self.dht.read_data().await?;
|
let (raw_temp, raw_hum) = self.dht.read_data().await?;
|
||||||
|
|
||||||
let tmp: i16 = raw_temp as i16;
|
let tmp: i16 = raw_temp as i16;
|
||||||
|
@ -76,6 +87,7 @@ impl<'d, PIO: Instance, const SM: usize> Dht22Type2<'d, PIO, SM> {
|
||||||
humidity: raw_hum as f32 / 10.0,
|
humidity: raw_hum as f32 / 10.0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Dht11<'d, PIO: Instance, const SM: usize> {
|
pub struct Dht11<'d, PIO: Instance, const SM: usize> {
|
||||||
|
@ -92,8 +104,11 @@ 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> {
|
||||||
|
fn read(&mut self) -> impl core::future::Future<Output = Result<DhtResult, DhtError>> {
|
||||||
|
async {
|
||||||
let (t, h) = self.dht.read_data().await?;
|
let (t, h) = self.dht.read_data().await?;
|
||||||
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32;
|
let mut final_t: f32 = ((t & 0x7FFF) >> 8) as f32;
|
||||||
|
|
||||||
|
@ -106,4 +121,5 @@ impl<'d, PIO: Instance, const SM: usize> Dht11<'d, PIO, SM> {
|
||||||
humidity: (h >> 8) as f32,
|
humidity: (h >> 8) as f32,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue