use std::sync::{Mutex, PoisonError};
pub use rusty_esp_signal_core::radar::presence::{ENCODED_LEN, Occupancy, Presence};
use crate::board;
use crate::error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
pub baud: u32,
pub engineering: bool,
}
impl Config {
#[must_use]
pub const fn ld2410() -> Self {
Config {
baud: 256_000,
engineering: false,
}
}
#[must_use]
pub const fn at_baud(mut self, baud: u32) -> Self {
self.baud = baud;
self
}
#[must_use]
pub const fn with_engineering(mut self) -> Self {
self.engineering = true;
self
}
}
impl Default for Config {
fn default() -> Self {
Config::ld2410()
}
}
static CONFIG: Mutex<Option<Config>> = Mutex::new(None);
pub fn begin(config: Config) -> bool {
let ok = error::ok(board::with(|b| b.radar_begin(&config)));
if ok {
*CONFIG.lock().unwrap_or_else(PoisonError::into_inner) = Some(config);
}
ok
}
#[must_use]
pub fn read() -> Option<Presence> {
board::with(|b| b.radar_read()).ok().flatten()
}
#[must_use]
pub fn config() -> Option<Config> {
*CONFIG.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn end() {
*CONFIG.lock().unwrap_or_else(PoisonError::into_inner) = None;
}