Improve error handling + clean

This commit is contained in:
Jonathan BAUDIN 2023-09-08 23:13:58 +02:00
parent 647182759a
commit 85da7e34e0

View file

@ -8,16 +8,12 @@ use rp2040_hal::pio::{Rx, UninitStateMachine};
#[derive(Debug)] #[derive(Debug)]
pub enum DhtError { pub enum DhtError {
/// DHT never aswer to start signal
StartTimout,
/// Timeout during communication. /// Timeout during communication.
Timeout, Timeout,
/// CRC mismatch. /// CRC mismatch.
CrcMismatch, CrcMismatch,
/// Failed to get pin state (low or high) /// FIFO Read error
ReadPinError, ReadError,
/// Internal state error
InternatError,
} }
#[derive(Debug)] #[derive(Debug)]
@ -59,6 +55,7 @@ impl<P: PIOExt, STI: StateMachineIndex> PicoDht<P, STI> {
} }
} }
/// Read data from the sensor. This blocking function (for maximum timeout of 2 seconds).
pub fn read_data(&mut self, delay: &mut Delay) -> Result<DhtResult, DhtError> { pub fn read_data(&mut self, delay: &mut Delay) -> Result<DhtResult, DhtError> {
let mut timeout = 2000; let mut timeout = 2000;
@ -74,7 +71,13 @@ impl<P: PIOExt, STI: StateMachineIndex> PicoDht<P, STI> {
return Err(DhtError::Timeout); return Err(DhtError::Timeout);
} }
let raw = self.rx_fifo.read().unwrap_or_default(); let raw = match self.rx_fifo.read() {
Some(d) => d,
None => {
self.sm.restart();
return Err(DhtError::ReadError);
}
};
let t_raw = raw & 0x0000FFFF; let t_raw = raw & 0x0000FFFF;
let h_raw = (raw & 0xFFFF0000) >> 16; let h_raw = (raw & 0xFFFF0000) >> 16;