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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::{
    interface::{ReadData, WriteData},
    AccelMode, AccelOutputDataRate, BitFlags as BF, Error, Lsm303agr, Register,
};

impl<DI, CommE, PinE> Lsm303agr<DI>
where
    DI: ReadData<Error = Error<CommE, PinE>> + WriteData<Error = Error<CommE, PinE>>,
{
    /// Set accelerometer output data rate.
    ///
    /// This changes the power mode if the current one is not appropriate.
    /// When changing from a low-power-only output data rate setting into
    /// a high-resolution or normal power mode, it changes into normal mode.
    pub fn set_accel_odr(&mut self, odr: AccelOutputDataRate) -> Result<(), Error<CommE, PinE>> {
        let (mask, lp_only, lp_compat) = match odr {
            AccelOutputDataRate::Hz1 => (1 << 4, false, true),
            AccelOutputDataRate::Hz10 => (2 << 4, false, true),
            AccelOutputDataRate::Hz25 => (3 << 4, false, true),
            AccelOutputDataRate::Hz50 => (4 << 4, false, true),
            AccelOutputDataRate::Hz100 => (5 << 4, false, true),
            AccelOutputDataRate::Hz200 => (6 << 4, false, true),
            AccelOutputDataRate::Hz400 => (7 << 4, false, true),
            AccelOutputDataRate::Khz1_620LowPower => (8 << 4, true, true),
            AccelOutputDataRate::Khz1_344 => (9 << 4, false, false),
            AccelOutputDataRate::Khz5_376LowPower => (9 << 4, true, true),
        };
        let lp_enabled = self.ctrl_reg1_a.is_high(BF::LP_EN);
        let hr_enabled = self.ctrl_reg4_a.is_high(BF::HR);
        let mut should_lp_be_enabled = lp_enabled;
        if lp_enabled {
            if !lp_compat {
                // HR could not have been enabled.
                should_lp_be_enabled = false;
            }
        } else {
            // Currently normal or HR mode
            if lp_only {
                if hr_enabled {
                    self.disable_hr()?;
                }
                // power mode is (now) normal
                should_lp_be_enabled = true;
            }
        }
        let lp_flag = if should_lp_be_enabled { BF::LP_EN } else { 0 };
        let reg1 = (self.ctrl_reg1_a.bits & !(BF::LP_EN | (0x7 << 4))) | mask | lp_flag;
        self.iface
            .write_accel_register(Register::CTRL_REG1_A, reg1)?;
        self.ctrl_reg1_a = reg1.into();
        self.accel_odr = Some(odr);
        Ok(())
    }

    /// Set accelerometer power/resolution mode
    ///
    /// Returns `Error::InvalidInputData` if the mode is incompatible with the current
    /// accelerometer output data rate.
    pub fn set_accel_mode(&mut self, mode: AccelMode) -> Result<(), Error<CommE, PinE>> {
        check_accel_odr_is_compatible_with_mode(self.accel_odr, mode)?;

        match mode {
            AccelMode::HighResolution => {
                self.disable_lp()?;
                self.enable_hr()?;
            }
            AccelMode::Normal => {
                self.disable_lp()?;
                self.disable_hr()?;
            }
            AccelMode::LowPower => {
                self.disable_hr()?;
                self.enable_lp()?;
            }
            AccelMode::PowerDown => {
                let reg1 = self.ctrl_reg1_a.bits & !(0x7 << 4);
                self.iface
                    .write_accel_register(Register::CTRL_REG1_A, reg1)?;
                self.ctrl_reg1_a = reg1.into();
                self.accel_odr = None;
            }
        }
        Ok(())
    }

    fn enable_hr(&mut self) -> Result<(), Error<CommE, PinE>> {
        let reg4 = self.ctrl_reg4_a.with_high(BF::HR);
        self.iface
            .write_accel_register(Register::CTRL_REG4_A, reg4.bits)?;
        self.ctrl_reg4_a = reg4;
        Ok(())
    }

    fn disable_hr(&mut self) -> Result<(), Error<CommE, PinE>> {
        let reg4 = self.ctrl_reg4_a.with_low(BF::HR);
        self.iface
            .write_accel_register(Register::CTRL_REG4_A, reg4.bits)?;
        self.ctrl_reg4_a = reg4;
        Ok(())
    }

    fn enable_lp(&mut self) -> Result<(), Error<CommE, PinE>> {
        let reg1 = self.ctrl_reg1_a.with_high(BF::LP_EN);
        self.iface
            .write_accel_register(Register::CTRL_REG1_A, reg1.bits)?;
        self.ctrl_reg1_a = reg1;
        Ok(())
    }

    fn disable_lp(&mut self) -> Result<(), Error<CommE, PinE>> {
        let reg1 = self.ctrl_reg1_a.with_low(BF::LP_EN);
        self.iface
            .write_accel_register(Register::CTRL_REG1_A, reg1.bits)?;
        self.ctrl_reg1_a = reg1;
        Ok(())
    }
}

fn check_accel_odr_is_compatible_with_mode<CommE, PinE>(
    odr: Option<AccelOutputDataRate>,
    mode: AccelMode,
) -> Result<(), Error<CommE, PinE>> {
    if (odr == Some(AccelOutputDataRate::Khz1_620LowPower)
        || odr == Some(AccelOutputDataRate::Khz5_376LowPower))
        && (mode == AccelMode::Normal || mode == AccelMode::HighResolution)
        || (odr == Some(AccelOutputDataRate::Khz1_344) && mode == AccelMode::LowPower)
    {
        Err(Error::InvalidInputData)
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod accel_odr_mode_tests {
    use super::check_accel_odr_is_compatible_with_mode;
    use super::AccelMode;
    use crate::AccelOutputDataRate as ODR;

    macro_rules! compatible {
        ($odr:ident, $power:ident) => {
            check_accel_odr_is_compatible_with_mode::<(), ()>(Some(ODR::$odr), AccelMode::$power)
                .unwrap();
        };
    }

    macro_rules! not_compatible {
        ($odr:ident, $power:ident) => {
            check_accel_odr_is_compatible_with_mode::<(), ()>(Some(ODR::$odr), AccelMode::$power)
                .expect_err("Should have returned error");
        };
    }

    macro_rules! none_odr_compatible {
        ($power:ident) => {
            check_accel_odr_is_compatible_with_mode::<(), ()>(None, AccelMode::$power).unwrap();
        };
    }

    #[test]
    fn all_modes_are_compatible_with_powerdown() {
        compatible!(Hz1, PowerDown);
        compatible!(Hz10, PowerDown);
        compatible!(Hz25, PowerDown);
        compatible!(Hz50, PowerDown);
        compatible!(Hz100, PowerDown);
        compatible!(Hz200, PowerDown);
        compatible!(Hz400, PowerDown);
        compatible!(Khz1_620LowPower, PowerDown);
        compatible!(Khz5_376LowPower, PowerDown);
        compatible!(Khz1_344, PowerDown);
    }

    #[test]
    fn normal_mode_compatibility() {
        compatible!(Hz1, Normal);
        compatible!(Hz10, Normal);
        compatible!(Hz25, Normal);
        compatible!(Hz50, Normal);
        compatible!(Hz100, Normal);
        compatible!(Hz200, Normal);
        compatible!(Hz400, Normal);
        not_compatible!(Khz1_620LowPower, Normal);
        not_compatible!(Khz5_376LowPower, Normal);
        compatible!(Khz1_344, Normal);
    }

    #[test]
    fn high_resolution_mode_compatibility() {
        compatible!(Hz1, HighResolution);
        compatible!(Hz10, HighResolution);
        compatible!(Hz25, HighResolution);
        compatible!(Hz50, HighResolution);
        compatible!(Hz100, HighResolution);
        compatible!(Hz200, HighResolution);
        compatible!(Hz400, HighResolution);
        not_compatible!(Khz1_620LowPower, HighResolution);
        not_compatible!(Khz5_376LowPower, HighResolution);
        compatible!(Khz1_344, HighResolution);
    }

    #[test]
    fn low_power_mode_compatibility() {
        compatible!(Hz1, LowPower);
        compatible!(Hz10, LowPower);
        compatible!(Hz25, LowPower);
        compatible!(Hz50, LowPower);
        compatible!(Hz100, LowPower);
        compatible!(Hz200, LowPower);
        compatible!(Hz400, LowPower);
        compatible!(Khz1_620LowPower, LowPower);
        compatible!(Khz5_376LowPower, LowPower);
        not_compatible!(Khz1_344, LowPower);
    }

    #[test]
    fn none_odr_compatibility() {
        none_odr_compatible!(LowPower);
        none_odr_compatible!(Normal);
        none_odr_compatible!(HighResolution);
        none_odr_compatible!(PowerDown);
    }
}