use embedded_hal_async::{delay::DelayNs, i2c::I2c};
use crate::{
Bmp5xx,
error::{Error::WriteError, Result},
register::OSR_CONFIG,
};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum Oversampling {
X1 = 0x00,
X2 = 0x01,
X4 = 0x02,
X8 = 0x03,
X16 = 0x04,
X32 = 0x05,
X64 = 0x06,
X128 = 0x07,
}
impl Oversampling {
pub(crate) fn t_conv_t(&self) -> f32 {
match self {
Oversampling::X1 => 1.0,
Oversampling::X2 => 1.1,
Oversampling::X4 => 1.5,
Oversampling::X8 => 2.1,
Oversampling::X16 => 3.3,
Oversampling::X32 => 5.8,
Oversampling::X64 => 10.8,
Oversampling::X128 => 20.8,
}
}
pub(crate) fn t_conv_p(&self) -> f32 {
match self {
Oversampling::X1 => 1.0,
Oversampling::X2 => 1.7,
Oversampling::X4 => 2.9,
Oversampling::X8 => 5.4,
Oversampling::X16 => 10.4,
Oversampling::X32 => 20.4,
Oversampling::X64 => 40.4,
Oversampling::X128 => 80.4,
}
}
}
impl<I2C, D> Bmp5xx<I2C, D>
where
I2C: I2c,
D: DelayNs,
{
pub fn osr_temp(&mut self, osr: Oversampling) {
self.osr_t = osr;
}
pub fn osr_pres(&mut self, osr: Oversampling) {
self.osr_p = osr;
}
}
impl<I2C, D> Bmp5xx<I2C, D>
where
I2C: I2c,
D: DelayNs,
{
pub(crate) async fn conf_osr(&mut self, press_en: bool) -> Result<()> {
self.i2c
.write(
self.addr,
&[
OSR_CONFIG,
(press_en as u8) << 6 | (self.osr_p as u8) << 3 | self.osr_t as u8,
],
)
.await
.map_err(|_| WriteError)
}
}