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;
|
||||
|
|
70
src/lib.rs
70
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,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<DhtResult, DhtError> {
|
||||
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<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;
|
||||
|
||||
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<DhtResult, DhtError> {
|
||||
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<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 {
|
||||
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<DhtResult, DhtError> {
|
||||
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<Output = Result<DhtResult, DhtError>> {
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue