Skip to main content

mpu6050_dmp/
fifo.rs

1/// Configuration for the MPU6050's First-In-First-Out (FIFO) buffer.
2///
3/// The FIFO buffer is a hardware queue that stores sensor readings, providing several benefits:
4/// - Reduces I2C bus traffic by allowing batch reading of multiple samples
5/// - Ensures no data is lost even if the host processor is temporarily busy
6/// - Enables precise timing between samples
7/// - Helps synchronize data from different sensors
8///
9/// Fields:
10/// - `temp`: Enable temperature sensor data in FIFO
11/// - `xg`, `yg`, `zg`: Enable individual gyroscope axis data (X, Y, Z)
12/// - `accel`: Enable accelerometer data (all axes)
13/// - `slv0`, `slv1`, `slv2`: Enable auxiliary I2C slave device data
14///
15/// When enabled, each sensor's data will be written to the FIFO buffer
16/// at the configured sample rate. The buffer can hold up to 1024 bytes
17/// of sensor data.
18#[derive(Debug, Default, Copy, Clone)]
19#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct Fifo {
22    pub temp: bool,
23    pub xg: bool,
24    pub yg: bool,
25    pub zg: bool,
26    pub accel: bool,
27    pub slv2: bool,
28    pub slv1: bool,
29    pub slv0: bool,
30}
31
32impl Fifo {
33    /// Creates a new FIFO configuration with all sensors disabled.
34    ///
35    /// This is the safest default state as it:
36    /// - Prevents unwanted data accumulation
37    /// - Minimizes power consumption
38    /// - Allows selective enabling of only needed sensors
39    pub fn all_disabled() -> Self {
40        Self::default()
41    }
42
43    /// Converts a register byte into a FIFO configuration.
44    ///
45    /// Bit mapping:
46    /// - Bit 7: Temperature
47    /// - Bit 6-4: Gyroscope (X,Y,Z)
48    /// - Bit 3: Accelerometer
49    /// - Bit 2-0: I2C Slaves
50    pub(crate) fn from_byte(byte: u8) -> Self {
51        Self {
52            temp: (byte & 0b1000_0000) != 0,
53            xg: (byte & 0b0100_0000) != 0,
54            yg: (byte & 0b0010_0000) != 0,
55            zg: (byte & 0b0001_0000) != 0,
56            accel: (byte & 0b0000_1000) != 0,
57            slv2: (byte & 0b0000_0100) != 0,
58            slv1: (byte & 0b0000_0010) != 0,
59            slv0: (byte & 0b0000_0001) != 0,
60        }
61    }
62
63    pub(crate) fn to_byte(self) -> u8 {
64        let mut byte = 0;
65        if self.temp {
66            byte |= 1 << 7;
67        }
68        if self.xg {
69            byte |= 1 << 6;
70        }
71        if self.yg {
72            byte |= 1 << 5;
73        }
74        if self.zg {
75            byte |= 1 << 4;
76        }
77        if self.accel {
78            byte |= 1 << 3;
79        }
80        if self.slv2 {
81            byte |= 1 << 2;
82        }
83        if self.slv1 {
84            byte |= 1 << 1;
85        }
86        if self.slv0 {
87            byte |= 1 << 0;
88        }
89
90        byte
91    }
92}