Skip to main content

bme68x/
types.rs

1// Portions of this file are derived from Bosch Sensortec's
2// BME68x SensorAPI v4.4.8.
3// Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved.
4// SPDX-License-Identifier: BSD-3-Clause
5
6//! Platform-independent data types used by the `BME68x` driver.
7
8// The floating-point accessors are convenience views over an exact integer
9// representation. As with Bosch's FPU API, very large u32 values can round.
10#![allow(clippy::cast_precision_loss)]
11
12/// Number of bytes in the three `BME68x` calibration register blocks.
13pub const CALIBRATION_DATA_LEN: usize = 42;
14
15/// Length of the calibration block starting at register `0x8a`.
16pub const CALIBRATION_BLOCK_1_LEN: usize = 23;
17
18/// Length of the calibration block starting at register `0xe1`.
19pub const CALIBRATION_BLOCK_2_LEN: usize = 14;
20
21/// Length of the calibration block starting at register `0x00` on memory page 1.
22pub const CALIBRATION_BLOCK_3_LEN: usize = 5;
23
24const fn signed_byte(value: u8) -> i8 {
25    i8::from_ne_bytes([value])
26}
27
28/// Gas-resistance calculation variant reported by the sensor.
29#[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    /// Original low-gas-resistance calculation used by BME680 devices.
35    GasLow = 0,
36    /// High-gas-resistance calculation used by newer BME688 devices.
37    GasHigh = 1,
38}
39
40impl Variant {
41    /// Converts the variant-ID register value into a typed variant.
42    #[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    /// Returns the value stored in the variant-ID register.
52    #[must_use]
53    pub const fn register_value(self) -> u8 {
54        self as u8
55    }
56}
57
58/// Sensor operating mode.
59#[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    /// No measurement is running.
65    #[default]
66    Sleep = 0,
67    /// Perform one measurement, then return to sleep.
68    Forced = 1,
69    /// Cycle through a heater profile with shared heater duration.
70    Parallel = 2,
71    /// Cycle through a heater profile sequentially.
72    Sequential = 3,
73}
74
75impl OperationMode {
76    /// Converts the mode bits into a typed mode.
77    #[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    /// Returns the mode bits written to the sensor.
89    #[must_use]
90    pub const fn register_value(self) -> u8 {
91        self as u8
92    }
93}
94
95/// Temperature, pressure, or humidity oversampling setting.
96#[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    /// Skip this measurement.
102    #[default]
103    None = 0,
104    /// One sample.
105    X1 = 1,
106    /// Two samples.
107    X2 = 2,
108    /// Four samples.
109    X4 = 3,
110    /// Eight samples.
111    X8 = 4,
112    /// Sixteen samples.
113    X16 = 5,
114}
115
116impl Oversampling {
117    /// Converts an oversampling register field into a typed value.
118    #[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    /// Returns the oversampling register field.
132    #[must_use]
133    pub const fn register_value(self) -> u8 {
134        self as u8
135    }
136
137    /// Returns the number of ADC measurement cycles.
138    #[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/// IIR filter setting.
152///
153/// Variant names follow Bosch's `FILTER_SIZE_*` names. The corresponding
154/// filter coefficients are 2, 4, 8, 16, 32, 64, and 128.
155#[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    /// IIR filtering disabled.
161    #[default]
162    Off = 0,
163    /// Bosch filter-size setting 1 (coefficient 2).
164    Size1 = 1,
165    /// Bosch filter-size setting 3 (coefficient 4).
166    Size3 = 2,
167    /// Bosch filter-size setting 7 (coefficient 8).
168    Size7 = 3,
169    /// Bosch filter-size setting 15 (coefficient 16).
170    Size15 = 4,
171    /// Bosch filter-size setting 31 (coefficient 32).
172    Size31 = 5,
173    /// Bosch filter-size setting 63 (coefficient 64).
174    Size63 = 6,
175    /// Bosch filter-size setting 127 (coefficient 128).
176    Size127 = 7,
177}
178
179impl Filter {
180    /// Converts the filter register field into a typed value.
181    #[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    /// Returns the filter register field.
197    #[must_use]
198    pub const fn register_value(self) -> u8 {
199        self as u8
200    }
201}
202
203/// Standby time used between sequential-mode measurements.
204#[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    /// 0.59 ms.
210    Millis0_59 = 0,
211    /// 62.5 ms.
212    Millis62_5 = 1,
213    /// 125 ms.
214    Millis125 = 2,
215    /// 250 ms.
216    Millis250 = 3,
217    /// 500 ms.
218    Millis500 = 4,
219    /// 1,000 ms.
220    Millis1000 = 5,
221    /// 10 ms.
222    Millis10 = 6,
223    /// 20 ms.
224    Millis20 = 7,
225    /// No standby time.
226    #[default]
227    None = 8,
228}
229
230impl StandbyTime {
231    /// Converts Bosch's logical ODR value into a typed standby time.
232    #[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    /// Returns Bosch's logical ODR value.
249    #[must_use]
250    pub const fn register_value(self) -> u8 {
251        self as u8
252    }
253}
254
255/// Temperature, pressure, humidity, filter, and standby configuration.
256#[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    /// Humidity oversampling.
261    pub humidity_oversampling: Oversampling,
262    /// Temperature oversampling.
263    pub temperature_oversampling: Oversampling,
264    /// Pressure oversampling.
265    pub pressure_oversampling: Oversampling,
266    /// IIR filter setting.
267    pub filter: Filter,
268    /// Sequential-mode standby time.
269    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/// Raw ADC values extracted from one sensor data field.
285#[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    /// 20-bit uncompensated temperature ADC value.
290    pub temperature_adc: u32,
291    /// 20-bit uncompensated pressure ADC value.
292    pub pressure_adc: u32,
293    /// 16-bit uncompensated humidity ADC value.
294    pub humidity_adc: u16,
295    /// 10-bit uncompensated gas-resistance ADC value.
296    pub gas_resistance_adc: u16,
297    /// Four-bit gas-resistance range value.
298    pub gas_range: u8,
299}
300
301/// One fully compensated measurement in Bosch's exact fixed-point units.
302#[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    /// Temperature in hundredths of a degree Celsius.
307    pub temperature: i16,
308    /// Pressure in pascals.
309    pub pressure: u32,
310    /// Relative humidity in thousandths of a percent RH.
311    pub humidity: u32,
312    /// Gas resistance in ohms.
313    pub gas_resistance: u32,
314}
315
316impl FixedMeasurement {
317    /// Temperature in degrees Celsius.
318    #[must_use]
319    pub fn temperature_celsius(self) -> f32 {
320        f32::from(self.temperature) / 100.0
321    }
322
323    /// Pressure in pascals as a floating-point value.
324    #[must_use]
325    pub fn pressure_pascals(self) -> f32 {
326        self.pressure as f32
327    }
328
329    /// Pressure in hectopascals.
330    #[must_use]
331    pub fn pressure_hectopascals(self) -> f32 {
332        self.pressure as f32 / 100.0
333    }
334
335    /// Relative humidity in percent RH.
336    #[must_use]
337    pub fn humidity_percent(self) -> f32 {
338        self.humidity as f32 / 1_000.0
339    }
340
341    /// Gas resistance in ohms as a floating-point value.
342    #[must_use]
343    pub fn gas_resistance_ohms(self) -> f32 {
344        self.gas_resistance as f32
345    }
346}
347
348/// Factory calibration coefficients read from the sensor.
349///
350/// The fields intentionally mirror Bosch's `bme68x_calib_data` names so that
351/// values and differential-test vectors can be compared without translation.
352#[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    /// Humidity calibration coefficient H1.
357    pub par_h1: u16,
358    /// Humidity calibration coefficient H2.
359    pub par_h2: u16,
360    /// Humidity calibration coefficient H3.
361    pub par_h3: i8,
362    /// Humidity calibration coefficient H4.
363    pub par_h4: i8,
364    /// Humidity calibration coefficient H5.
365    pub par_h5: i8,
366    /// Humidity calibration coefficient H6.
367    pub par_h6: u8,
368    /// Humidity calibration coefficient H7.
369    pub par_h7: i8,
370    /// Gas-heater calibration coefficient GH1.
371    pub par_gh1: i8,
372    /// Gas-heater calibration coefficient GH2.
373    pub par_gh2: i16,
374    /// Gas-heater calibration coefficient GH3.
375    pub par_gh3: i8,
376    /// Temperature calibration coefficient T1.
377    pub par_t1: u16,
378    /// Temperature calibration coefficient T2.
379    pub par_t2: i16,
380    /// Temperature calibration coefficient T3.
381    pub par_t3: i8,
382    /// Pressure calibration coefficient P1.
383    pub par_p1: u16,
384    /// Pressure calibration coefficient P2.
385    pub par_p2: i16,
386    /// Pressure calibration coefficient P3.
387    pub par_p3: i8,
388    /// Pressure calibration coefficient P4.
389    pub par_p4: i16,
390    /// Pressure calibration coefficient P5.
391    pub par_p5: i16,
392    /// Pressure calibration coefficient P6.
393    pub par_p6: i8,
394    /// Pressure calibration coefficient P7.
395    pub par_p7: i8,
396    /// Pressure calibration coefficient P8.
397    pub par_p8: i16,
398    /// Pressure calibration coefficient P9.
399    pub par_p9: i16,
400    /// Pressure calibration coefficient P10.
401    pub par_p10: u8,
402    /// Temperature fine-resolution intermediate used by pressure and humidity.
403    pub(crate) t_fine: i32,
404    /// Heater resistance range coefficient.
405    pub res_heat_range: u8,
406    /// Heater resistance value coefficient.
407    pub res_heat_val: i8,
408    /// Gas range switching-error coefficient.
409    pub range_sw_err: i8,
410}
411
412impl CalibrationData {
413    /// Parses the concatenated 42 bytes from Bosch's three calibration blocks.
414    #[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    /// Parses the three discontiguous calibration register blocks.
452    #[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    /// Returns the current Bosch `t_fine` compensation intermediate.
467    #[must_use]
468    pub const fn temperature_fine(&self) -> i32 {
469        self.t_fine
470    }
471
472    /// Encode the immutable factory coefficients in Bosch's canonical
473    /// 42-byte register-block layout.
474    ///
475    /// Reserved bytes and unused bits are zeroed. The mutable `t_fine`
476    /// compensation intermediate is deliberately excluded, so the result is
477    /// stable before and after measurements and can identify sensor changes.
478    #[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    /// Stable FNV-1a 64-bit fingerprint of the normalized coefficients.
513    ///
514    /// Prefer the driver's `calibration_fingerprint()` method when the exact
515    /// raw calibration register image is available. This normalized helper is
516    /// useful for coefficient comparisons, but intentionally omits reserved
517    /// register bits and bytes.
518    #[must_use]
519    pub fn coefficient_fingerprint(&self) -> u64 {
520        calibration_register_fingerprint(&self.canonical_coefficient_bytes())
521    }
522}
523
524/// Calculate an FNV-1a 64-bit fingerprint over exact calibration bytes.
525///
526/// This deterministic checksum is intended to detect a changed sensor. It is
527/// not cryptographic and is not a globally unique sensor serial number.
528#[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}