#![allow(clippy::cast_precision_loss)]
pub const CALIBRATION_DATA_LEN: usize = 42;
pub const CALIBRATION_BLOCK_1_LEN: usize = 23;
pub const CALIBRATION_BLOCK_2_LEN: usize = 14;
pub const CALIBRATION_BLOCK_3_LEN: usize = 5;
const fn signed_byte(value: u8) -> i8 {
i8::from_ne_bytes([value])
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum Variant {
GasLow = 0,
GasHigh = 1,
}
impl Variant {
#[must_use]
pub const fn from_register(value: u8) -> Option<Self> {
match value {
0 => Some(Self::GasLow),
1 => Some(Self::GasHigh),
_ => None,
}
}
#[must_use]
pub const fn register_value(self) -> u8 {
self as u8
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum OperationMode {
#[default]
Sleep = 0,
Forced = 1,
Parallel = 2,
Sequential = 3,
}
impl OperationMode {
#[must_use]
pub const fn from_register(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Sleep),
1 => Some(Self::Forced),
2 => Some(Self::Parallel),
3 => Some(Self::Sequential),
_ => None,
}
}
#[must_use]
pub const fn register_value(self) -> u8 {
self as u8
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum Oversampling {
#[default]
None = 0,
X1 = 1,
X2 = 2,
X4 = 3,
X8 = 4,
X16 = 5,
}
impl Oversampling {
#[must_use]
pub const fn from_register(value: u8) -> Option<Self> {
match value {
0 => Some(Self::None),
1 => Some(Self::X1),
2 => Some(Self::X2),
3 => Some(Self::X4),
4 => Some(Self::X8),
5 => Some(Self::X16),
_ => None,
}
}
#[must_use]
pub const fn register_value(self) -> u8 {
self as u8
}
#[must_use]
pub const fn measurement_cycles(self) -> u32 {
match self {
Self::None => 0,
Self::X1 => 1,
Self::X2 => 2,
Self::X4 => 4,
Self::X8 => 8,
Self::X16 => 16,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum Filter {
#[default]
Off = 0,
Size1 = 1,
Size3 = 2,
Size7 = 3,
Size15 = 4,
Size31 = 5,
Size63 = 6,
Size127 = 7,
}
impl Filter {
#[must_use]
pub const fn from_register(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Off),
1 => Some(Self::Size1),
2 => Some(Self::Size3),
3 => Some(Self::Size7),
4 => Some(Self::Size15),
5 => Some(Self::Size31),
6 => Some(Self::Size63),
7 => Some(Self::Size127),
_ => None,
}
}
#[must_use]
pub const fn register_value(self) -> u8 {
self as u8
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum StandbyTime {
Millis0_59 = 0,
Millis62_5 = 1,
Millis125 = 2,
Millis250 = 3,
Millis500 = 4,
Millis1000 = 5,
Millis10 = 6,
Millis20 = 7,
#[default]
None = 8,
}
impl StandbyTime {
#[must_use]
pub const fn from_register(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Millis0_59),
1 => Some(Self::Millis62_5),
2 => Some(Self::Millis125),
3 => Some(Self::Millis250),
4 => Some(Self::Millis500),
5 => Some(Self::Millis1000),
6 => Some(Self::Millis10),
7 => Some(Self::Millis20),
8 => Some(Self::None),
_ => None,
}
}
#[must_use]
pub const fn register_value(self) -> u8 {
self as u8
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Configuration {
pub humidity_oversampling: Oversampling,
pub temperature_oversampling: Oversampling,
pub pressure_oversampling: Oversampling,
pub filter: Filter,
pub standby_time: StandbyTime,
}
impl Default for Configuration {
fn default() -> Self {
Self {
humidity_oversampling: Oversampling::None,
temperature_oversampling: Oversampling::None,
pressure_oversampling: Oversampling::None,
filter: Filter::Off,
standby_time: StandbyTime::None,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RawMeasurement {
pub temperature_adc: u32,
pub pressure_adc: u32,
pub humidity_adc: u16,
pub gas_resistance_adc: u16,
pub gas_range: u8,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FixedMeasurement {
pub temperature: i16,
pub pressure: u32,
pub humidity: u32,
pub gas_resistance: u32,
}
impl FixedMeasurement {
#[must_use]
pub fn temperature_celsius(self) -> f32 {
f32::from(self.temperature) / 100.0
}
#[must_use]
pub fn pressure_pascals(self) -> f32 {
self.pressure as f32
}
#[must_use]
pub fn pressure_hectopascals(self) -> f32 {
self.pressure as f32 / 100.0
}
#[must_use]
pub fn humidity_percent(self) -> f32 {
self.humidity as f32 / 1_000.0
}
#[must_use]
pub fn gas_resistance_ohms(self) -> f32 {
self.gas_resistance as f32
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CalibrationData {
pub par_h1: u16,
pub par_h2: u16,
pub par_h3: i8,
pub par_h4: i8,
pub par_h5: i8,
pub par_h6: u8,
pub par_h7: i8,
pub par_gh1: i8,
pub par_gh2: i16,
pub par_gh3: i8,
pub par_t1: u16,
pub par_t2: i16,
pub par_t3: i8,
pub par_p1: u16,
pub par_p2: i16,
pub par_p3: i8,
pub par_p4: i16,
pub par_p5: i16,
pub par_p6: i8,
pub par_p7: i8,
pub par_p8: i16,
pub par_p9: i16,
pub par_p10: u8,
pub(crate) t_fine: i32,
pub res_heat_range: u8,
pub res_heat_val: i8,
pub range_sw_err: i8,
}
impl CalibrationData {
#[must_use]
pub fn from_register_bytes(bytes: &[u8; CALIBRATION_DATA_LEN]) -> Self {
const H1_DATA_MASK: u8 = 0x0f;
const RES_HEAT_RANGE_MASK: u8 = 0x30;
const RANGE_SWITCH_ERROR_MASK: u8 = 0xf0;
Self {
par_h1: (u16::from(bytes[25]) << 4) | u16::from(bytes[24] & H1_DATA_MASK),
par_h2: (u16::from(bytes[23]) << 4) | u16::from(bytes[24] >> 4),
par_h3: signed_byte(bytes[26]),
par_h4: signed_byte(bytes[27]),
par_h5: signed_byte(bytes[28]),
par_h6: bytes[29],
par_h7: signed_byte(bytes[30]),
par_gh1: signed_byte(bytes[35]),
par_gh2: i16::from_le_bytes([bytes[33], bytes[34]]),
par_gh3: signed_byte(bytes[36]),
par_t1: u16::from_le_bytes([bytes[31], bytes[32]]),
par_t2: i16::from_le_bytes([bytes[0], bytes[1]]),
par_t3: signed_byte(bytes[2]),
par_p1: u16::from_le_bytes([bytes[4], bytes[5]]),
par_p2: i16::from_le_bytes([bytes[6], bytes[7]]),
par_p3: signed_byte(bytes[8]),
par_p4: i16::from_le_bytes([bytes[10], bytes[11]]),
par_p5: i16::from_le_bytes([bytes[12], bytes[13]]),
par_p6: signed_byte(bytes[15]),
par_p7: signed_byte(bytes[14]),
par_p8: i16::from_le_bytes([bytes[18], bytes[19]]),
par_p9: i16::from_le_bytes([bytes[20], bytes[21]]),
par_p10: bytes[22],
t_fine: 0,
res_heat_range: (bytes[39] & RES_HEAT_RANGE_MASK) / 16,
res_heat_val: signed_byte(bytes[37]),
range_sw_err: signed_byte(bytes[41] & RANGE_SWITCH_ERROR_MASK) / 16,
}
}
#[must_use]
pub fn from_register_blocks(
block_1: &[u8; CALIBRATION_BLOCK_1_LEN],
block_2: &[u8; CALIBRATION_BLOCK_2_LEN],
block_3: &[u8; CALIBRATION_BLOCK_3_LEN],
) -> Self {
let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
bytes[..CALIBRATION_BLOCK_1_LEN].copy_from_slice(block_1);
bytes[CALIBRATION_BLOCK_1_LEN..CALIBRATION_BLOCK_1_LEN + CALIBRATION_BLOCK_2_LEN]
.copy_from_slice(block_2);
bytes[CALIBRATION_BLOCK_1_LEN + CALIBRATION_BLOCK_2_LEN..].copy_from_slice(block_3);
Self::from_register_bytes(&bytes)
}
#[must_use]
pub const fn temperature_fine(&self) -> i32 {
self.t_fine
}
#[must_use]
pub fn canonical_coefficient_bytes(&self) -> [u8; CALIBRATION_DATA_LEN] {
let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
bytes[0..2].copy_from_slice(&self.par_t2.to_le_bytes());
bytes[2] = self.par_t3.to_ne_bytes()[0];
bytes[4..6].copy_from_slice(&self.par_p1.to_le_bytes());
bytes[6..8].copy_from_slice(&self.par_p2.to_le_bytes());
bytes[8] = self.par_p3.to_ne_bytes()[0];
bytes[10..12].copy_from_slice(&self.par_p4.to_le_bytes());
bytes[12..14].copy_from_slice(&self.par_p5.to_le_bytes());
bytes[14] = self.par_p7.to_ne_bytes()[0];
bytes[15] = self.par_p6.to_ne_bytes()[0];
bytes[18..20].copy_from_slice(&self.par_p8.to_le_bytes());
bytes[20..22].copy_from_slice(&self.par_p9.to_le_bytes());
bytes[22] = self.par_p10;
bytes[23] = (self.par_h2 >> 4).to_le_bytes()[0];
bytes[24] =
((self.par_h2 & 0x0f).to_le_bytes()[0] << 4) | (self.par_h1 & 0x0f).to_le_bytes()[0];
bytes[25] = (self.par_h1 >> 4).to_le_bytes()[0];
bytes[26] = self.par_h3.to_ne_bytes()[0];
bytes[27] = self.par_h4.to_ne_bytes()[0];
bytes[28] = self.par_h5.to_ne_bytes()[0];
bytes[29] = self.par_h6;
bytes[30] = self.par_h7.to_ne_bytes()[0];
bytes[31..33].copy_from_slice(&self.par_t1.to_le_bytes());
bytes[33..35].copy_from_slice(&self.par_gh2.to_le_bytes());
bytes[35] = self.par_gh1.to_ne_bytes()[0];
bytes[36] = self.par_gh3.to_ne_bytes()[0];
bytes[37] = self.res_heat_val.to_ne_bytes()[0];
bytes[39] = (self.res_heat_range & 0x03) << 4;
bytes[41] = self.range_sw_err.to_ne_bytes()[0] << 4;
bytes
}
#[must_use]
pub fn coefficient_fingerprint(&self) -> u64 {
calibration_register_fingerprint(&self.canonical_coefficient_bytes())
}
}
#[must_use]
pub fn calibration_register_fingerprint(bytes: &[u8; CALIBRATION_DATA_LEN]) -> u64 {
const OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
const PRIME: u64 = 0x0000_0100_0000_01b3;
let mut hash = OFFSET_BASIS;
for &byte in bytes {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(PRIME);
}
hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn calibration_parsing_matches_bosch_byte_layout() {
let bytes = core::array::from_fn::<_, CALIBRATION_DATA_LEN, _>(|index| {
u8::try_from(index).unwrap_or(0)
});
let calibration = CalibrationData::from_register_bytes(&bytes);
assert_eq!(calibration.par_t2, 0x0100);
assert_eq!(calibration.par_t3, 2);
assert_eq!(calibration.par_p1, 0x0504);
assert_eq!(calibration.par_p2, 0x0706);
assert_eq!(calibration.par_p7, 14);
assert_eq!(calibration.par_p6, 15);
assert_eq!(calibration.par_p10, 22);
assert_eq!(calibration.par_h1, 0x0198);
assert_eq!(calibration.par_h2, 0x0171);
assert_eq!(calibration.par_t1, 0x201f);
assert_eq!(calibration.par_gh2, 0x2221);
assert_eq!(calibration.res_heat_val, 37);
assert_eq!(calibration.res_heat_range, 2);
assert_eq!(calibration.range_sw_err, 2);
assert_eq!(calibration.temperature_fine(), 0);
}
#[test]
fn signed_calibration_fields_preserve_twos_complement() {
let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
bytes[0] = 0xfe;
bytes[1] = 0xff;
bytes[2] = 0x80;
bytes[33] = 0x34;
bytes[34] = 0x80;
bytes[37] = 0xff;
bytes[41] = 0xf0;
let calibration = CalibrationData::from_register_bytes(&bytes);
assert_eq!(calibration.par_t2, -2);
assert_eq!(calibration.par_t3, -128);
assert_eq!(calibration.par_gh2, -32_716);
assert_eq!(calibration.res_heat_val, -1);
assert_eq!(calibration.range_sw_err, -1);
}
#[test]
fn canonical_calibration_bytes_round_trip_and_ignore_temperature_fine() {
let mut bytes = core::array::from_fn::<_, CALIBRATION_DATA_LEN, _>(|index| {
u8::try_from(index).unwrap_or(0)
});
let mut calibration = CalibrationData::from_register_bytes(&bytes);
let canonical = calibration.canonical_coefficient_bytes();
assert_eq!(
CalibrationData::from_register_bytes(&canonical),
calibration
);
bytes[3] ^= 0xff;
bytes[9] ^= 0xff;
assert_eq!(
CalibrationData::from_register_bytes(&bytes).canonical_coefficient_bytes(),
canonical
);
let fingerprint = calibration.coefficient_fingerprint();
calibration.t_fine = 123_456;
assert_eq!(calibration.canonical_coefficient_bytes(), canonical);
assert_eq!(calibration.coefficient_fingerprint(), fingerprint);
assert_eq!(fingerprint, 0x5a5b_240a_eb30_385b);
}
#[test]
fn raw_calibration_fingerprint_includes_reserved_bytes() {
let mut first = [0_u8; CALIBRATION_DATA_LEN];
let mut second = first;
second[3] = 0xa5;
assert_ne!(
calibration_register_fingerprint(&first),
calibration_register_fingerprint(&second)
);
first[3] = 0xa5;
assert_eq!(
calibration_register_fingerprint(&first),
calibration_register_fingerprint(&second)
);
}
#[test]
fn fixed_measurement_float_accessors_only_scale_units() {
let data = FixedMeasurement {
temperature: 2_534,
pressure: 101_325,
humidity: 45_678,
gas_resistance: 123_456,
};
assert!((data.temperature_celsius() - 25.34).abs() < f32::EPSILON);
assert!((data.pressure_pascals() - 101_325.0).abs() < f32::EPSILON);
assert!((data.pressure_hectopascals() - 1_013.25).abs() < f32::EPSILON);
assert!((data.humidity_percent() - 45.678).abs() < f32::EPSILON);
assert!((data.gas_resistance_ohms() - 123_456.0).abs() < f32::EPSILON);
}
#[test]
fn typed_register_values_round_trip() {
for value in 0..=5 {
let setting = Oversampling::from_register(value).unwrap();
assert_eq!(setting.register_value(), value);
}
assert_eq!(Oversampling::from_register(6), None);
for value in 0..=8 {
let setting = StandbyTime::from_register(value).unwrap();
assert_eq!(setting.register_value(), value);
}
assert_eq!(StandbyTime::from_register(9), None);
}
}