1#![allow(clippy::cast_precision_loss)]
11
12pub const CALIBRATION_DATA_LEN: usize = 42;
14
15pub const CALIBRATION_BLOCK_1_LEN: usize = 23;
17
18pub const CALIBRATION_BLOCK_2_LEN: usize = 14;
20
21pub const CALIBRATION_BLOCK_3_LEN: usize = 5;
23
24const fn signed_byte(value: u8) -> i8 {
25 i8::from_ne_bytes([value])
26}
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
30#[cfg_attr(feature = "defmt", derive(defmt::Format))]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32#[repr(u8)]
33pub enum Variant {
34 GasLow = 0,
36 GasHigh = 1,
38}
39
40impl Variant {
41 #[must_use]
43 pub const fn from_register(value: u8) -> Option<Self> {
44 match value {
45 0 => Some(Self::GasLow),
46 1 => Some(Self::GasHigh),
47 _ => None,
48 }
49 }
50
51 #[must_use]
53 pub const fn register_value(self) -> u8 {
54 self as u8
55 }
56}
57
58#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
60#[cfg_attr(feature = "defmt", derive(defmt::Format))]
61#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
62#[repr(u8)]
63pub enum OperationMode {
64 #[default]
66 Sleep = 0,
67 Forced = 1,
69 Parallel = 2,
71 Sequential = 3,
73}
74
75impl OperationMode {
76 #[must_use]
78 pub const fn from_register(value: u8) -> Option<Self> {
79 match value {
80 0 => Some(Self::Sleep),
81 1 => Some(Self::Forced),
82 2 => Some(Self::Parallel),
83 3 => Some(Self::Sequential),
84 _ => None,
85 }
86 }
87
88 #[must_use]
90 pub const fn register_value(self) -> u8 {
91 self as u8
92 }
93}
94
95#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
97#[cfg_attr(feature = "defmt", derive(defmt::Format))]
98#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99#[repr(u8)]
100pub enum Oversampling {
101 #[default]
103 None = 0,
104 X1 = 1,
106 X2 = 2,
108 X4 = 3,
110 X8 = 4,
112 X16 = 5,
114}
115
116impl Oversampling {
117 #[must_use]
119 pub const fn from_register(value: u8) -> Option<Self> {
120 match value {
121 0 => Some(Self::None),
122 1 => Some(Self::X1),
123 2 => Some(Self::X2),
124 3 => Some(Self::X4),
125 4 => Some(Self::X8),
126 5 => Some(Self::X16),
127 _ => None,
128 }
129 }
130
131 #[must_use]
133 pub const fn register_value(self) -> u8 {
134 self as u8
135 }
136
137 #[must_use]
139 pub const fn measurement_cycles(self) -> u32 {
140 match self {
141 Self::None => 0,
142 Self::X1 => 1,
143 Self::X2 => 2,
144 Self::X4 => 4,
145 Self::X8 => 8,
146 Self::X16 => 16,
147 }
148 }
149}
150
151#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
156#[cfg_attr(feature = "defmt", derive(defmt::Format))]
157#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
158#[repr(u8)]
159pub enum Filter {
160 #[default]
162 Off = 0,
163 Size1 = 1,
165 Size3 = 2,
167 Size7 = 3,
169 Size15 = 4,
171 Size31 = 5,
173 Size63 = 6,
175 Size127 = 7,
177}
178
179impl Filter {
180 #[must_use]
182 pub const fn from_register(value: u8) -> Option<Self> {
183 match value {
184 0 => Some(Self::Off),
185 1 => Some(Self::Size1),
186 2 => Some(Self::Size3),
187 3 => Some(Self::Size7),
188 4 => Some(Self::Size15),
189 5 => Some(Self::Size31),
190 6 => Some(Self::Size63),
191 7 => Some(Self::Size127),
192 _ => None,
193 }
194 }
195
196 #[must_use]
198 pub const fn register_value(self) -> u8 {
199 self as u8
200 }
201}
202
203#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
205#[cfg_attr(feature = "defmt", derive(defmt::Format))]
206#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
207#[repr(u8)]
208pub enum StandbyTime {
209 Millis0_59 = 0,
211 Millis62_5 = 1,
213 Millis125 = 2,
215 Millis250 = 3,
217 Millis500 = 4,
219 Millis1000 = 5,
221 Millis10 = 6,
223 Millis20 = 7,
225 #[default]
227 None = 8,
228}
229
230impl StandbyTime {
231 #[must_use]
233 pub const fn from_register(value: u8) -> Option<Self> {
234 match value {
235 0 => Some(Self::Millis0_59),
236 1 => Some(Self::Millis62_5),
237 2 => Some(Self::Millis125),
238 3 => Some(Self::Millis250),
239 4 => Some(Self::Millis500),
240 5 => Some(Self::Millis1000),
241 6 => Some(Self::Millis10),
242 7 => Some(Self::Millis20),
243 8 => Some(Self::None),
244 _ => None,
245 }
246 }
247
248 #[must_use]
250 pub const fn register_value(self) -> u8 {
251 self as u8
252 }
253}
254
255#[derive(Clone, Copy, Debug, Eq, PartialEq)]
257#[cfg_attr(feature = "defmt", derive(defmt::Format))]
258#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
259pub struct Configuration {
260 pub humidity_oversampling: Oversampling,
262 pub temperature_oversampling: Oversampling,
264 pub pressure_oversampling: Oversampling,
266 pub filter: Filter,
268 pub standby_time: StandbyTime,
270}
271
272impl Default for Configuration {
273 fn default() -> Self {
274 Self {
275 humidity_oversampling: Oversampling::None,
276 temperature_oversampling: Oversampling::None,
277 pressure_oversampling: Oversampling::None,
278 filter: Filter::Off,
279 standby_time: StandbyTime::None,
280 }
281 }
282}
283
284#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
286#[cfg_attr(feature = "defmt", derive(defmt::Format))]
287#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
288pub struct RawMeasurement {
289 pub temperature_adc: u32,
291 pub pressure_adc: u32,
293 pub humidity_adc: u16,
295 pub gas_resistance_adc: u16,
297 pub gas_range: u8,
299}
300
301#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
303#[cfg_attr(feature = "defmt", derive(defmt::Format))]
304#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
305pub struct FixedMeasurement {
306 pub temperature: i16,
308 pub pressure: u32,
310 pub humidity: u32,
312 pub gas_resistance: u32,
314}
315
316impl FixedMeasurement {
317 #[must_use]
319 pub fn temperature_celsius(self) -> f32 {
320 f32::from(self.temperature) / 100.0
321 }
322
323 #[must_use]
325 pub fn pressure_pascals(self) -> f32 {
326 self.pressure as f32
327 }
328
329 #[must_use]
331 pub fn pressure_hectopascals(self) -> f32 {
332 self.pressure as f32 / 100.0
333 }
334
335 #[must_use]
337 pub fn humidity_percent(self) -> f32 {
338 self.humidity as f32 / 1_000.0
339 }
340
341 #[must_use]
343 pub fn gas_resistance_ohms(self) -> f32 {
344 self.gas_resistance as f32
345 }
346}
347
348#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
353#[cfg_attr(feature = "defmt", derive(defmt::Format))]
354#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
355pub struct CalibrationData {
356 pub par_h1: u16,
358 pub par_h2: u16,
360 pub par_h3: i8,
362 pub par_h4: i8,
364 pub par_h5: i8,
366 pub par_h6: u8,
368 pub par_h7: i8,
370 pub par_gh1: i8,
372 pub par_gh2: i16,
374 pub par_gh3: i8,
376 pub par_t1: u16,
378 pub par_t2: i16,
380 pub par_t3: i8,
382 pub par_p1: u16,
384 pub par_p2: i16,
386 pub par_p3: i8,
388 pub par_p4: i16,
390 pub par_p5: i16,
392 pub par_p6: i8,
394 pub par_p7: i8,
396 pub par_p8: i16,
398 pub par_p9: i16,
400 pub par_p10: u8,
402 pub(crate) t_fine: i32,
404 pub res_heat_range: u8,
406 pub res_heat_val: i8,
408 pub range_sw_err: i8,
410}
411
412impl CalibrationData {
413 #[must_use]
415 pub fn from_register_bytes(bytes: &[u8; CALIBRATION_DATA_LEN]) -> Self {
416 const H1_DATA_MASK: u8 = 0x0f;
417 const RES_HEAT_RANGE_MASK: u8 = 0x30;
418 const RANGE_SWITCH_ERROR_MASK: u8 = 0xf0;
419
420 Self {
421 par_h1: (u16::from(bytes[25]) << 4) | u16::from(bytes[24] & H1_DATA_MASK),
422 par_h2: (u16::from(bytes[23]) << 4) | u16::from(bytes[24] >> 4),
423 par_h3: signed_byte(bytes[26]),
424 par_h4: signed_byte(bytes[27]),
425 par_h5: signed_byte(bytes[28]),
426 par_h6: bytes[29],
427 par_h7: signed_byte(bytes[30]),
428 par_gh1: signed_byte(bytes[35]),
429 par_gh2: i16::from_le_bytes([bytes[33], bytes[34]]),
430 par_gh3: signed_byte(bytes[36]),
431 par_t1: u16::from_le_bytes([bytes[31], bytes[32]]),
432 par_t2: i16::from_le_bytes([bytes[0], bytes[1]]),
433 par_t3: signed_byte(bytes[2]),
434 par_p1: u16::from_le_bytes([bytes[4], bytes[5]]),
435 par_p2: i16::from_le_bytes([bytes[6], bytes[7]]),
436 par_p3: signed_byte(bytes[8]),
437 par_p4: i16::from_le_bytes([bytes[10], bytes[11]]),
438 par_p5: i16::from_le_bytes([bytes[12], bytes[13]]),
439 par_p6: signed_byte(bytes[15]),
440 par_p7: signed_byte(bytes[14]),
441 par_p8: i16::from_le_bytes([bytes[18], bytes[19]]),
442 par_p9: i16::from_le_bytes([bytes[20], bytes[21]]),
443 par_p10: bytes[22],
444 t_fine: 0,
445 res_heat_range: (bytes[39] & RES_HEAT_RANGE_MASK) / 16,
446 res_heat_val: signed_byte(bytes[37]),
447 range_sw_err: signed_byte(bytes[41] & RANGE_SWITCH_ERROR_MASK) / 16,
448 }
449 }
450
451 #[must_use]
453 pub fn from_register_blocks(
454 block_1: &[u8; CALIBRATION_BLOCK_1_LEN],
455 block_2: &[u8; CALIBRATION_BLOCK_2_LEN],
456 block_3: &[u8; CALIBRATION_BLOCK_3_LEN],
457 ) -> Self {
458 let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
459 bytes[..CALIBRATION_BLOCK_1_LEN].copy_from_slice(block_1);
460 bytes[CALIBRATION_BLOCK_1_LEN..CALIBRATION_BLOCK_1_LEN + CALIBRATION_BLOCK_2_LEN]
461 .copy_from_slice(block_2);
462 bytes[CALIBRATION_BLOCK_1_LEN + CALIBRATION_BLOCK_2_LEN..].copy_from_slice(block_3);
463 Self::from_register_bytes(&bytes)
464 }
465
466 #[must_use]
468 pub const fn temperature_fine(&self) -> i32 {
469 self.t_fine
470 }
471
472 #[must_use]
479 pub fn canonical_coefficient_bytes(&self) -> [u8; CALIBRATION_DATA_LEN] {
480 let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
481 bytes[0..2].copy_from_slice(&self.par_t2.to_le_bytes());
482 bytes[2] = self.par_t3.to_ne_bytes()[0];
483 bytes[4..6].copy_from_slice(&self.par_p1.to_le_bytes());
484 bytes[6..8].copy_from_slice(&self.par_p2.to_le_bytes());
485 bytes[8] = self.par_p3.to_ne_bytes()[0];
486 bytes[10..12].copy_from_slice(&self.par_p4.to_le_bytes());
487 bytes[12..14].copy_from_slice(&self.par_p5.to_le_bytes());
488 bytes[14] = self.par_p7.to_ne_bytes()[0];
489 bytes[15] = self.par_p6.to_ne_bytes()[0];
490 bytes[18..20].copy_from_slice(&self.par_p8.to_le_bytes());
491 bytes[20..22].copy_from_slice(&self.par_p9.to_le_bytes());
492 bytes[22] = self.par_p10;
493 bytes[23] = (self.par_h2 >> 4).to_le_bytes()[0];
494 bytes[24] =
495 ((self.par_h2 & 0x0f).to_le_bytes()[0] << 4) | (self.par_h1 & 0x0f).to_le_bytes()[0];
496 bytes[25] = (self.par_h1 >> 4).to_le_bytes()[0];
497 bytes[26] = self.par_h3.to_ne_bytes()[0];
498 bytes[27] = self.par_h4.to_ne_bytes()[0];
499 bytes[28] = self.par_h5.to_ne_bytes()[0];
500 bytes[29] = self.par_h6;
501 bytes[30] = self.par_h7.to_ne_bytes()[0];
502 bytes[31..33].copy_from_slice(&self.par_t1.to_le_bytes());
503 bytes[33..35].copy_from_slice(&self.par_gh2.to_le_bytes());
504 bytes[35] = self.par_gh1.to_ne_bytes()[0];
505 bytes[36] = self.par_gh3.to_ne_bytes()[0];
506 bytes[37] = self.res_heat_val.to_ne_bytes()[0];
507 bytes[39] = (self.res_heat_range & 0x03) << 4;
508 bytes[41] = self.range_sw_err.to_ne_bytes()[0] << 4;
509 bytes
510 }
511
512 #[must_use]
519 pub fn coefficient_fingerprint(&self) -> u64 {
520 calibration_register_fingerprint(&self.canonical_coefficient_bytes())
521 }
522}
523
524#[must_use]
529pub fn calibration_register_fingerprint(bytes: &[u8; CALIBRATION_DATA_LEN]) -> u64 {
530 const OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
531 const PRIME: u64 = 0x0000_0100_0000_01b3;
532
533 let mut hash = OFFSET_BASIS;
534 for &byte in bytes {
535 hash ^= u64::from(byte);
536 hash = hash.wrapping_mul(PRIME);
537 }
538 hash
539}
540
541#[cfg(test)]
542mod tests {
543 use super::*;
544
545 #[test]
546 fn calibration_parsing_matches_bosch_byte_layout() {
547 let bytes = core::array::from_fn::<_, CALIBRATION_DATA_LEN, _>(|index| {
548 u8::try_from(index).unwrap_or(0)
549 });
550 let calibration = CalibrationData::from_register_bytes(&bytes);
551
552 assert_eq!(calibration.par_t2, 0x0100);
553 assert_eq!(calibration.par_t3, 2);
554 assert_eq!(calibration.par_p1, 0x0504);
555 assert_eq!(calibration.par_p2, 0x0706);
556 assert_eq!(calibration.par_p7, 14);
557 assert_eq!(calibration.par_p6, 15);
558 assert_eq!(calibration.par_p10, 22);
559 assert_eq!(calibration.par_h1, 0x0198);
560 assert_eq!(calibration.par_h2, 0x0171);
561 assert_eq!(calibration.par_t1, 0x201f);
562 assert_eq!(calibration.par_gh2, 0x2221);
563 assert_eq!(calibration.res_heat_val, 37);
564 assert_eq!(calibration.res_heat_range, 2);
565 assert_eq!(calibration.range_sw_err, 2);
566 assert_eq!(calibration.temperature_fine(), 0);
567 }
568
569 #[test]
570 fn signed_calibration_fields_preserve_twos_complement() {
571 let mut bytes = [0_u8; CALIBRATION_DATA_LEN];
572 bytes[0] = 0xfe;
573 bytes[1] = 0xff;
574 bytes[2] = 0x80;
575 bytes[33] = 0x34;
576 bytes[34] = 0x80;
577 bytes[37] = 0xff;
578 bytes[41] = 0xf0;
579
580 let calibration = CalibrationData::from_register_bytes(&bytes);
581 assert_eq!(calibration.par_t2, -2);
582 assert_eq!(calibration.par_t3, -128);
583 assert_eq!(calibration.par_gh2, -32_716);
584 assert_eq!(calibration.res_heat_val, -1);
585 assert_eq!(calibration.range_sw_err, -1);
586 }
587
588 #[test]
589 fn canonical_calibration_bytes_round_trip_and_ignore_temperature_fine() {
590 let mut bytes = core::array::from_fn::<_, CALIBRATION_DATA_LEN, _>(|index| {
591 u8::try_from(index).unwrap_or(0)
592 });
593 let mut calibration = CalibrationData::from_register_bytes(&bytes);
594 let canonical = calibration.canonical_coefficient_bytes();
595
596 assert_eq!(
597 CalibrationData::from_register_bytes(&canonical),
598 calibration
599 );
600 bytes[3] ^= 0xff;
601 bytes[9] ^= 0xff;
602 assert_eq!(
603 CalibrationData::from_register_bytes(&bytes).canonical_coefficient_bytes(),
604 canonical
605 );
606
607 let fingerprint = calibration.coefficient_fingerprint();
608 calibration.t_fine = 123_456;
609 assert_eq!(calibration.canonical_coefficient_bytes(), canonical);
610 assert_eq!(calibration.coefficient_fingerprint(), fingerprint);
611 assert_eq!(fingerprint, 0x5a5b_240a_eb30_385b);
612 }
613
614 #[test]
615 fn raw_calibration_fingerprint_includes_reserved_bytes() {
616 let mut first = [0_u8; CALIBRATION_DATA_LEN];
617 let mut second = first;
618 second[3] = 0xa5;
619
620 assert_ne!(
621 calibration_register_fingerprint(&first),
622 calibration_register_fingerprint(&second)
623 );
624 first[3] = 0xa5;
625 assert_eq!(
626 calibration_register_fingerprint(&first),
627 calibration_register_fingerprint(&second)
628 );
629 }
630
631 #[test]
632 fn fixed_measurement_float_accessors_only_scale_units() {
633 let data = FixedMeasurement {
634 temperature: 2_534,
635 pressure: 101_325,
636 humidity: 45_678,
637 gas_resistance: 123_456,
638 };
639
640 assert!((data.temperature_celsius() - 25.34).abs() < f32::EPSILON);
641 assert!((data.pressure_pascals() - 101_325.0).abs() < f32::EPSILON);
642 assert!((data.pressure_hectopascals() - 1_013.25).abs() < f32::EPSILON);
643 assert!((data.humidity_percent() - 45.678).abs() < f32::EPSILON);
644 assert!((data.gas_resistance_ohms() - 123_456.0).abs() < f32::EPSILON);
645 }
646
647 #[test]
648 fn typed_register_values_round_trip() {
649 for value in 0..=5 {
650 let setting = Oversampling::from_register(value).unwrap();
651 assert_eq!(setting.register_value(), value);
652 }
653 assert_eq!(Oversampling::from_register(6), None);
654
655 for value in 0..=8 {
656 let setting = StandbyTime::from_register(value).unwrap();
657 assert_eq!(setting.register_value(), value);
658 }
659 assert_eq!(StandbyTime::from_register(9), None);
660 }
661}