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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Common device configuration methods.
use crate::{
    marker, private, AdcRange, BitFlags as BF, Config, Error, FifoAlmostFullLevelInterrupt,
    LedPulseWidth, Max3010x, Register as Reg, SampleAveraging, SamplingRate,
};
use hal::blocking::i2c;

impl FifoAlmostFullLevelInterrupt {
    fn get_register_value(self) -> u8 {
        use FifoAlmostFullLevelInterrupt as L;
        match self {
            L::L0 => 0,
            L::L1 => 1,
            L::L2 => 2,
            L::L3 => 3,
            L::L4 => 4,
            L::L5 => 5,
            L::L6 => 6,
            L::L7 => 7,
            L::L8 => 8,
            L::L9 => 9,
            L::L10 => 10,
            L::L11 => 11,
            L::L12 => 12,
            L::L13 => 13,
            L::L14 => 14,
            L::L15 => 15,
        }
    }
}

macro_rules! flip_flag_method_impl {
    ($name:ident, $doc:expr, $reg:ident, $reg_variable:ident, $config_method:ident, $bitflag:ident) => {
        #[doc = $doc]
        pub fn $name(&mut self) -> Result<(), Error<E>> {
            let $reg_variable = self.$reg_variable.$config_method(BF::$bitflag);
            self.write_data(&[Reg::$reg, $reg_variable.bits])?;
            self.$reg_variable = $reg_variable;
            Ok(())
        }
    }
}

macro_rules! high_low_flag_impl {
    ($enable_name:ident, $enable_doc:expr, $disable_name:ident, $disable_doc:expr, $reg:ident, $reg_variable:ident, $bitflag:ident) => {
        flip_flag_method_impl!(
            $enable_name,
            $enable_doc,
            $reg,
            $reg_variable,
            with_high,
            $bitflag
        );
        flip_flag_method_impl!(
            $disable_name,
            $disable_doc,
            $reg,
            $reg_variable,
            with_low,
            $bitflag
        );
    };
}

impl<I2C, E, IC, MODE> Max3010x<I2C, IC, MODE>
where
    I2C: i2c::WriteRead<Error = E> + i2c::Write<Error = E>,
{
    /// Resets the FIFO read and write pointers and overflow counter to 0.
    pub fn clear_fifo(&mut self) -> Result<(), Error<E>> {
        self.write_data(&[Reg::FIFO_WR_PTR, 0, 0, 0])
    }

    /// Set sample averaging
    pub fn set_sample_averaging(
        &mut self,
        sample_averaging: SampleAveraging,
    ) -> Result<(), Error<E>> {
        let fifo_config = self.fifo_config.with_low(0b1110_0000);
        let fifo_config = match sample_averaging {
            SampleAveraging::Sa1 => fifo_config,
            SampleAveraging::Sa2 => fifo_config.with_high(0b0010_0000),
            SampleAveraging::Sa4 => fifo_config.with_high(0b0100_0000),
            SampleAveraging::Sa8 => fifo_config.with_high(0b0110_0000),
            SampleAveraging::Sa16 => fifo_config.with_high(0b1000_0000),
            SampleAveraging::Sa32 => fifo_config.with_high(0b1010_0000),
        };
        self.write_data(&[Reg::FIFO_CONFIG, fifo_config.bits])?;
        self.fifo_config = fifo_config;
        Ok(())
    }

    /// Trigger a software reset
    pub fn reset(&mut self) -> Result<(), Error<E>> {
        let mode = self.mode.with_high(BF::RESET);
        self.write_data(&[Reg::MODE, mode.bits])
    }

    /// Put the device in power-save mode.
    pub fn shutdown(&mut self) -> Result<(), Error<E>> {
        let mode = self.mode.with_high(BF::SHUTDOWN);
        self.change_mode(mode)
    }

    /// Wake up from power-save mode.
    pub fn wake_up(&mut self) -> Result<(), Error<E>> {
        let mode = self.mode.with_low(BF::SHUTDOWN);
        self.change_mode(mode)
    }

    high_low_flag_impl!(
        enable_fifo_rollover,
        "Enable FIFO rollover",
        disable_fifo_rollover,
        "Disable FIFO rollover",
        FIFO_CONFIG,
        fifo_config,
        FIFO_ROLLOVER_EN
    );

    /// Set number of empty data samples available in the FIFO
    /// when a FIFO-almost-full interrupt will be issued.
    pub fn set_fifo_almost_full_level_interrupt(
        &mut self,
        level: FifoAlmostFullLevelInterrupt,
    ) -> Result<(), Error<E>> {
        let fifo_config = self
            .fifo_config
            .with_low(0b0000_0111)
            .with_high(level.get_register_value());
        self.write_data(&[Reg::FIFO_CONFIG, fifo_config.bits])?;
        self.fifo_config = fifo_config;
        Ok(())
    }

    high_low_flag_impl!(
        enable_fifo_almost_full_interrupt,
        "Enable FIFO almost full interrupt",
        disable_fifo_almost_full_interrupt,
        "Disable FIFO almost full interrupt",
        INT_EN1,
        int_en1,
        FIFO_A_FULL_INT
    );

    high_low_flag_impl!(
        enable_alc_overflow_interrupt,
        "Enable ambient light cancellation overflow interrupt",
        disable_alc_overflow_interrupt,
        "Disable ambient light cancellation overflow interrupt",
        INT_EN1,
        int_en1,
        ALC_OVF_INT
    );

    high_low_flag_impl!(
        enable_temperature_ready_interrupt,
        "Enable internal die temperature conversion ready interrupt",
        disable_temperature_ready_interrupt,
        "Disable internal die temperature conversion ready interrupt",
        INT_EN2,
        int_en2,
        DIE_TEMP_RDY_INT
    );

    pub(crate) fn change_mode(&mut self, mode: Config) -> Result<(), Error<E>> {
        self.write_data(&[Reg::MODE, mode.bits])?;
        self.mode = mode;
        Ok(())
    }
}

#[doc(hidden)]
pub trait ValidateSrPw: private::Sealed {
    /// Check the pulse width and sample rate combination
    fn check<E>(width: LedPulseWidth, rate: SamplingRate) -> Result<(), Error<E>>;
}

fn check_red_only<E>(pw: LedPulseWidth, sr: SamplingRate) -> Result<(), Error<E>> {
    use LedPulseWidth::*;
    use SamplingRate::*;

    if (sr == Sps3200 && (pw == Pw118 || pw == Pw215 || pw == Pw411))
        || (sr == Sps1600 && pw == Pw411)
    {
        Err(Error::InvalidArguments)
    } else {
        Ok(())
    }
}

fn check_red_ir<E>(pw: LedPulseWidth, sr: SamplingRate) -> Result<(), Error<E>> {
    use LedPulseWidth::*;
    use SamplingRate::*;

    if sr == Sps3200
        || (sr == Sps1600 && (pw == Pw118 || pw == Pw215 || pw == Pw411))
        || (sr == Sps1000 && (pw == Pw215 || pw == Pw411))
        || (sr == Sps800 && pw == Pw411)
    {
        Err(Error::InvalidArguments)
    } else {
        Ok(())
    }
}

impl ValidateSrPw for marker::mode::HeartRate {
    fn check<E>(pw: LedPulseWidth, sr: SamplingRate) -> Result<(), Error<E>> {
        check_red_only(pw, sr)
    }
}

impl ValidateSrPw for marker::mode::Oximeter {
    fn check<E>(pw: LedPulseWidth, sr: SamplingRate) -> Result<(), Error<E>> {
        check_red_ir(pw, sr)
    }
}

impl ValidateSrPw for marker::mode::MultiLED {
    fn check<E>(_width: LedPulseWidth, _rate: SamplingRate) -> Result<(), Error<E>> {
        Ok(())
    }
}

impl<I2C, E, IC, MODE> Max3010x<I2C, IC, MODE>
where
    I2C: i2c::WriteRead<Error = E> + i2c::Write<Error = E>,
    MODE: ValidateSrPw,
{
    /// Configure the LED pulse width.
    ///
    /// This determines the ADC resolution.
    pub fn set_pulse_width(&mut self, width: LedPulseWidth) -> Result<(), Error<E>> {
        use LedPulseWidth::*;
        MODE::check::<E>(width, self.get_sampling_rate())?;
        let config = self.spo2_config.with_low(BF::LED_PW0).with_low(BF::LED_PW1);
        let config = match width {
            Pw69 => config,
            Pw118 => config.with_high(BF::LED_PW0),
            Pw215 => config.with_high(BF::LED_PW1),
            Pw411 => config.with_high(BF::LED_PW0).with_high(BF::LED_PW1),
        };
        self.write_data(&[Reg::SPO2_CONFIG, config.bits])?;
        self.spo2_config = config;
        Ok(())
    }

    /// Configure the sample rate
    ///
    /// This depends on the LED pulse width. Calling this with an inappropriate
    /// value for the selected pulse with will return `Error::InvalidArgument`
    pub fn set_sampling_rate(&mut self, sampling_rate: SamplingRate) -> Result<(), Error<E>> {
        use SamplingRate::*;
        MODE::check::<E>(self.get_pulse_width(), sampling_rate)?;
        let config = self
            .spo2_config
            .with_low(BF::SPO2_SR0)
            .with_low(BF::SPO2_SR1)
            .with_low(BF::SPO2_SR2);
        let config = match sampling_rate {
            Sps50 => config,
            Sps100 => config.with_high(BF::SPO2_SR0),
            Sps200 => config.with_high(BF::SPO2_SR1),
            Sps400 => config.with_high(BF::SPO2_SR1).with_high(BF::SPO2_SR0),
            Sps800 => config.with_high(BF::SPO2_SR2),
            Sps1000 => config.with_high(BF::SPO2_SR2).with_high(BF::SPO2_SR0),
            Sps1600 => config.with_high(BF::SPO2_SR2).with_high(BF::SPO2_SR1),
            Sps3200 => config
                .with_high(BF::SPO2_SR2)
                .with_high(BF::SPO2_SR1)
                .with_high(BF::SPO2_SR0),
        };
        self.write_data(&[Reg::SPO2_CONFIG, config.bits])?;
        self.spo2_config = config;
        Ok(())
    }
}

impl<I2C, E, IC> Max3010x<I2C, IC, marker::mode::Oximeter>
where
    I2C: i2c::Write<Error = E>,
{
    /// Configure analog-to-digital converter range. (Only available in Oximeter mode)
    pub fn set_adc_range(&mut self, range: AdcRange) -> Result<(), Error<E>> {
        use AdcRange::*;
        let new_config = self
            .spo2_config
            .with_low(BF::ADC_RGE0)
            .with_low(BF::ADC_RGE1);
        let new_config = match range {
            Fs2k => new_config,
            Fs4k => new_config.with_high(BF::ADC_RGE0),
            Fs8k => new_config.with_high(BF::ADC_RGE1),
            Fs16k => new_config.with_high(BF::ADC_RGE0).with_high(BF::ADC_RGE1),
        };
        self.write_data(&[Reg::SPO2_CONFIG, new_config.bits])?;
        self.spo2_config = new_config;
        Ok(())
    }
}

#[doc(hidden)]
pub trait HasDataReadyInterrupt {}

impl HasDataReadyInterrupt for marker::mode::HeartRate {}
impl HasDataReadyInterrupt for marker::mode::Oximeter {}

impl<I2C, E, IC, MODE> Max3010x<I2C, IC, MODE>
where
    I2C: i2c::Write<Error = E>,
    MODE: HasDataReadyInterrupt,
{
    high_low_flag_impl!(
        enable_new_fifo_data_ready_interrupt,
        "Enable new FIFO data ready interrupt",
        disable_new_fifo_data_ready_interrupt,
        "Disable new FIFO data ready interrupt",
        INT_EN1,
        int_en1,
        PPG_RDY_INT
    );
}

#[cfg(test)]
mod tests {
    use super::{check_red_ir, check_red_only, LedPulseWidth as LedPw, SamplingRate as SR};

    #[test]
    fn invalid_combinations_oximeter_sampling_rate_800_pulse_width_411() {
        check_red_ir::<()>(LedPw::Pw411, SR::Sps800).expect_err("Should return error.");
    }

    #[test]
    fn invalid_combinations_oximeter_sampling_rate_1000_pulse_width() {
        check_red_ir::<()>(LedPw::Pw215, SR::Sps1000).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw411, SR::Sps1000).expect_err("Should return error.");
    }
    #[test]
    fn invalid_combinations_oximeter_sampling_rate_1600_pulse_width() {
        check_red_ir::<()>(LedPw::Pw118, SR::Sps1600).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw215, SR::Sps1600).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw411, SR::Sps1600).expect_err("Should return error.");
    }
    #[test]
    fn invalid_combinations_oximeter_sampling_rate_3200_pulse_width() {
        check_red_ir::<()>(LedPw::Pw69, SR::Sps3200).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw118, SR::Sps3200).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw215, SR::Sps3200).expect_err("Should return error.");
        check_red_ir::<()>(LedPw::Pw411, SR::Sps3200).expect_err("Should return error.");
    }

    #[test]
    fn invalid_combinations_heart_rate_sampling_rate_1600_pulse_width_411() {
        check_red_only::<()>(LedPw::Pw411, SR::Sps1600).expect_err("Should return error.");
    }
    #[test]
    fn invalid_combinations_heart_rate_sampling_rate_3200_pulse_width() {
        check_red_only::<()>(LedPw::Pw118, SR::Sps3200).expect_err("Should return error.");
        check_red_only::<()>(LedPw::Pw215, SR::Sps3200).expect_err("Should return error.");
        check_red_only::<()>(LedPw::Pw411, SR::Sps3200).expect_err("Should return error.");
    }
}