bmp390_rs/register/data.rs
1//! ### DATA - Sensor data (`0x04`, 6 bytes, R)
2//!
3//! Contains pressure and temperature data stored in consecutive registers:
4//! - Pressure: `DATA_0 (0x04)`, `DATA_1 (0x05)`, `DATA_2 (0x06)`
5//! - Temperature: `DATA_3 (0x07)`, `DATA_4 (0x08)`, `DATA_5 (0x09)`
6//!
7//! For a total of 6 bytes, or 2 24-bit values.
8//!
9//! Instead of exposing each individual data register, this module exposes a single marker type [`Data`] with address `0x04` and length `6` since these
10//! registers need to be burst read all at once to prevent mix-up of bytes belonging to different measurements.
11//!
12//! **Note:** Reading from this register will yield uncalibrated raw sensor data. In order for them to be usable, they need to be calibrated.
13//! All sensor data read through any of the exposed methods in this driver will always return *calibrated* data unless specified otherwise.
14//!
15//! ### Default values
16//! N/A
17//!
18//! ### Examples
19//! ```rust,no_run
20//! # use crate::bmp390_rs::{Bmp390, Bmp390Result};
21//! # use crate::bmp390_rs::bus::Bus;
22//! # async fn demo<B: Bus>(mut device: Bmp390<B>)
23//! # -> Bmp390Result<(), B::Error> {
24//! use bmp390_rs::register::data::Data;
25//!
26//! // Print raw uncalibrated measurements
27//! let sensor_data = device.read::<Data>().await?;
28//! println!("temp: {}, pressure: {}", sensor_data.temperature, sensor_data.pressure);
29//!
30//! # Ok(()) }
31//! ```
32use crate::register::{InvalidRegisterField, Readable, Reg};
33
34/// Marker struct for the DATA_0 - DATA_5 (0x04 - 0x09) registers.
35///
36/// - **Length:** 6 bytes
37/// - **Access:** Read-only
38///
39/// Used with [`Bmp390::read::<Data>()`] or the method
40/// [`Bmp390::read_sensor_data()`].
41///
42/// The BMP390 will auto-increment on multiple reads, so reading 6 bytes from 0x04 will read
43/// pressure and temperature in one burst read as recommended by the datasheet, section 3.10.
44/// Note that this will return the raw uncalibrated measurement data. So for most use cases
45/// calling [`Bmp390::read_sensor_data()`] is recommended as it will calibrate the data for you.
46pub struct Data;
47impl Reg for Data { const ADDR: u8 = 0x04;}
48
49/// The payload for the Data (0x04 - 0x09) registers.
50#[derive(Copy, Clone, Debug)]
51pub struct DataSample {
52 /// The 24-bit uncalibrated pressure data from the DATA_0, DATA_1 and DATA_2 registers.
53 pub pressure: u32,
54
55 /// The 24-bit uncalibrated temperature data from the DATA_3, DATA_4 and DATA_5 registers.
56 pub temperature: u32,
57}
58
59impl Readable for Data {
60 type Out = DataSample;
61
62 const N: usize = 6;
63
64 fn decode(b: &[u8]) -> Result<Self::Out, InvalidRegisterField> {
65 Ok(DataSample {
66 pressure: u32::from_le_bytes([b[0], b[1], b[2], 0]),
67 temperature: u32::from_le_bytes([b[3], b[4], b[5], 0]),
68 })
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75
76 #[test]
77 fn data_decode() {
78 let reg = Data::decode(&[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]).unwrap();
79
80 assert_eq!(0xCCBBAA, reg.pressure);
81 assert_eq!(0xFFEEDD, reg.temperature);
82 }
83}