summaryrefslogtreecommitdiffstats
path: root/src/devices/mode/continuous.rs
blob: edc87c2684f725f7b3c47e6c54a90689505ec970 (plain)
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
//! Continuous measurement mode

use crate::{
    conversion, devices::OperatingMode, mode, Ads1x1x, ChannelId, Error, ModeChangeError, Register,
};
use core::marker::PhantomData;

impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::Continuous>
where
    I2C: embedded_hal::i2c::I2c<Error = E>,
    CONV: conversion::ConvertMeasurement,
{
    /// Change operating mode to OneShot
    pub fn into_one_shot(
        mut self,
    ) -> Result<Ads1x1x<I2C, IC, CONV, mode::OneShot>, ModeChangeError<E, Self>> {
        if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::OneShot) {
            return Err(ModeChangeError::I2C(e, self));
        }
        Ok(Ads1x1x {
            i2c: self.i2c,
            address: self.address,
            config: self.config,
            fsr: self.fsr,
            a_conversion_was_started: false,
            _conv: PhantomData,
            _ic: PhantomData,
            _mode: PhantomData,
        })
    }

    /// Read the most recent measurement
    pub fn read(&mut self) -> Result<i16, Error<E>> {
        let value = self.read_register(Register::CONVERSION)?;
        Ok(CONV::convert_measurement(value))
    }

    /// Select the channel for measurements.
    ///
    /// Note that when changing the channel in continuous conversion mode, the
    /// ongoing conversion will be completed.
    /// The following conversions will use the new channel configuration.
    #[allow(unused_variables)]
    pub fn select_channel<CH: ChannelId<Self>>(&mut self, channel: CH) -> Result<(), Error<E>> {
        let config = self.config.with_mux_bits(CH::channel_id());
        self.write_register(Register::CONFIG, config.bits)?;
        self.config = config;
        Ok(())
    }
}