use embedded_hal_async::{delay::DelayNs, i2c::I2c};
use crate::{
Bmp5xx,
error::{Error::WriteError, Result},
register::{INT_CONFIG, INT_SOURCE},
};
pub struct Interrupt {
enabled: bool,
source: IntSource,
mode: IntMode,
polarity: IntPolarity,
int_pin: IntPin,
}
impl Default for Interrupt {
fn default() -> Self {
Self {
enabled: false,
source: IntSource::default(),
mode: IntMode::Pulsed,
polarity: IntPolarity::ActiveLow,
int_pin: IntPin::PushPull,
}
}
}
impl Interrupt {
pub fn enable(mut self, enable: bool) -> Self {
self.enabled = enable;
self
}
pub fn mode(mut self, mode: IntMode) -> Self {
self.mode = mode;
self
}
pub fn polarity(mut self, polarity: IntPolarity) -> Self {
self.polarity = polarity;
self
}
pub fn pin(mut self, pin: IntPin) -> Self {
self.int_pin = pin;
self
}
}
pub struct IntSource {
data_ready: bool,
fifo_full: bool,
fifo_threshold: bool,
pressure_oor: bool,
}
impl Default for IntSource {
fn default() -> Self {
Self {
data_ready: true,
fifo_full: false,
fifo_threshold: false,
pressure_oor: false,
}
}
}
impl IntSource {
pub fn data_ready(mut self, data_ready: bool) -> Self {
self.data_ready = data_ready;
self
}
pub fn fifo_full(mut self, fifo_full: bool) -> Self {
self.fifo_full = fifo_full;
self
}
pub fn fifo_threshold(mut self, fifo_threshold: bool) -> Self {
self.fifo_threshold = fifo_threshold;
self
}
pub fn pressure_oor(mut self, pressure_oor: bool) -> Self {
self.pressure_oor = pressure_oor;
self
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum IntMode {
Pulsed = 0x00,
Latched = 0x01,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum IntPolarity {
ActiveLow = 0x00,
ActiveHigh = 0x01,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum IntPin {
PushPull = 0x00,
OpenDrain = 0x01,
}
impl<I2C, D> Bmp5xx<I2C, D>
where
I2C: I2c,
D: DelayNs,
{
pub async fn int(&mut self, conf: Interrupt) -> Result<()> {
let source = conf.source;
self.i2c
.write(
self.addr,
&[
INT_SOURCE,
(source.pressure_oor as u8) << 3
| (source.fifo_threshold as u8) << 2
| (source.fifo_full as u8) << 1
| source.data_ready as u8,
],
)
.await
.map_err(|_| WriteError)?;
self.i2c
.write(
self.addr,
&[
INT_CONFIG,
(conf.enabled as u8) << 3
| (conf.int_pin as u8) << 2
| (conf.polarity as u8) << 1
| conf.mode as u8,
],
)
.await
.map_err(|_| WriteError)
}
}