lis2dtw12/
register_settings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// Operating Mode
#[derive(Debug, Copy, Clone, Default)]
pub enum Mode {
    /// Low power mode (12/14-bit resolution, depending on the set LowPowerMode)
    #[default]
    LowPower = 0b00,
    /// High-performance mode (14 Bit resolution)
    HighPerformance = 0b01,
    /// Single data conversion on-demand mode (12/14-bit resolution, depending on the set LowPowerMode)
    SingleDataConversion = 0b10,
}

/// Output Data Rate
///
/// Rates are shown as: <High-performance rate> / <Low-power rate>
#[derive(Debug, Copy, Clone, Default)]
pub enum OutputDataRate {
    /// Power-down mode
    #[default]
    PowerDown = 0b0000,
    /// 12.5 Hz / 1.6 Hz
    Hz1_6 = 0b0001,
    /// 12.5 Hz / 12.5 Hz
    Hz12_5 = 0b0010,
    /// 25 Hz / 25 Hz
    Hz25 = 0b0011,
    /// 50 Hz / 50 Hz
    Hz50 = 0b0100,
    /// 100 Hz / 100 Hz
    Hz100 = 0b0101,
    /// 200 Hz / 200 Hz
    Hz200 = 0b0110,
    /// 400 Hz / 200 Hz
    Hz400 = 0b0111,
    /// 800 Hz / 200 Hz
    Hz800 = 0b1000,
    /// 1600 Hz / 200 Hz
    Hz1600 = 0b1001,
}

/// Low Power Mode
#[derive(Debug, Copy, Clone, Default)]
pub enum LowPowerMode {
    /// Low-power mode 1 (12-bit resolution)
    #[default]
    Mode1 = 0b00,
    /// Low-power mode 2 (14-bit resolution)
    Mode2 = 0b01,
    /// Low-power mode 3 (14-bit resolution)
    Mode3 = 0b10,
    /// Low-power mode 4 (14-bit resolution)
    Mode4 = 0b11,
}

/// Digital filtering cutoff selection / Bandwidth selection
#[derive(Debug, Copy, Clone, Default)]
pub enum BandwidthSelection {
    /// ODR/2 (up to ODR = 800 Hz, 400 Hz when ODR = 1600 Hz)
    #[default]
    OdrDiv2 = 0b00,
    /// ODR/4 (HP/LP)
    OdrDiv4 = 0b01,
    /// ODR/10 (HP/LP)
    OdrDiv10 = 0b10,
    /// ODR/20 (HP/LP)
    OdrDiv20 = 0b11,
}

/// Full-scale selection
#[derive(Debug, Copy, Clone, Default)]
pub enum FullScale {
    /// ±2 g
    #[default]
    G2 = 0b00,
    /// ±4 g
    G4 = 0b01,
    /// ±8 g
    G8 = 0b10,
    /// ±16 g
    G16 = 0b11,
}

impl FullScale {
    pub(crate) fn convert_raw_i16_to_mg(
        self,
        raw: i16,
        set_mode: Mode,
        set_lp_mode: LowPowerMode,
    ) -> f32 {
        // mg/digit
        let factor = match self {
            FullScale::G2 => 0.244,
            FullScale::G4 => 0.488,
            FullScale::G8 => 0.976,
            FullScale::G16 => 1.952,
        };

        let aligned = match (set_mode, set_lp_mode) {
            // 12-bit resolution, only active on LowPowerMode::Mode1
            (Mode::LowPower | Mode::SingleDataConversion, LowPowerMode::Mode1) => (raw >> 4) as f32,
            // 14-bit resolution, active in every other mode
            (_, _) => (raw >> 2) as f32,
        };
        aligned * factor
    }
}

/// Fifo Mode
#[derive(Debug, Copy, Clone, Default)]
pub enum FifoMode {
    /// Bypass mode (FIFO turned off)
    #[default]
    Bypass = 0b000,
    /// FIFO mode: Stop collecting data when FIFO is full
    StopOnFifoFull = 0b001,
    /// Continuous-to-FIFO: Stream mode until trigger is deasserted, then FIFO mode (StopOnFifoFull)
    ContinuousToFifo = 0b011,
    /// Bypass-to-continuous: Bypass mode until trigger is deasserted, then Continuous mode
    BypassToContinuous = 0b100,
    /// Continuous mode: If FIFO is full, the new sample overwrites the older sample
    Continuous = 0b110,
}

/// Thresholds for 4D/6D function @ FS = ±2 g
#[derive(Debug, Copy, Clone, Default)]
pub enum Threshold6D {
    /// 6 (80°)
    #[default]
    Deg80 = 0b00,
    /// 11 (70°)
    Deg70 = 0b01,
    /// 16 (60°)
    Deg60 = 0b10,
    /// 21 (50°)
    Deg50 = 0b11,
}

/// Tap Priority axis selection for tap detection
///
/// MAX_PRIO, MID_PRIO, MIN_PRIO
#[derive(Debug, Copy, Clone, Default)]
pub enum TapPriority {
    /// X, Y, Z
    #[default]
    XYZ = 0b000,
    /// Y, X, Z
    YXZ = 0b001,
    /// X, Z, Y
    XZY = 0b010,
    /// Z, Y, X
    ZYX = 0b011,
    /// X, Y, Z (alternative), same as XYZ
    XYZAlt = 0b100,
    /// Y, Z, X
    YZX = 0b101,
    /// Z, X, Y
    ZXY = 0b110,
    /// Z, Y, X (alternative), same as ZYX
    ZYXAlt = 0b111,
}

/// Free-fall Threshold @ FS = ±2 g
#[derive(Debug, Copy, Clone, Default)]
pub enum FreeFallThreshold {
    /// 5
    #[default]
    Ths5 = 0b000,
    /// 7
    Ths7 = 0b001,
    /// 8
    Ths8 = 0b010,
    /// 10
    Ths10 = 0b011,
    /// 11
    Ths11 = 0b100,
    /// 13
    Ths13 = 0b101,
    /// 15
    Ths15 = 0b110,
    /// 16
    Ths16 = 0b111,
}