diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2019-03-30 12:55:05 +0200 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2019-03-31 13:41:26 +0200 |
commit | e6c1725b6a3419d5a28542d79bde271d0d5ef6f6 (patch) | |
tree | 9aab4c1a4b22eda261068a27e9230778a7fcd9c1 /src | |
parent | b226a3605f3b83f88581111723f527aae30fa34c (diff) | |
download | ads1x1x-async-e6c1725b6a3419d5a28542d79bde271d0d5ef6f6.tar.gz ads1x1x-async-e6c1725b6a3419d5a28542d79bde271d0d5ef6f6.tar.xz ads1x1x-async-e6c1725b6a3419d5a28542d79bde271d0d5ef6f6.zip |
Integrate mode change into `into...` methods
Diffstat (limited to 'src')
-rw-r--r-- | src/devices/mode/continuous.rs | 35 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 12 | ||||
-rw-r--r-- | src/lib.rs | 31 |
3 files changed, 38 insertions, 40 deletions
diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index 66951dd..29471de 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -3,7 +3,7 @@ use super::super::OperatingMode; use channels::ChannelSelection; use core::marker::PhantomData; -use {conversion, hal, interface, mode, Ads1x1x, Error, Register}; +use {conversion, hal, interface, mode, Ads1x1x, Error, ModeChangeError, Register}; impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous> where @@ -11,8 +11,12 @@ where CONV: conversion::ConvertMeasurement, { /// Change operating mode to OneShot - pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, CONV, mode::OneShot>, Error<E>> { - self.set_operating_mode(OperatingMode::OneShot)?; + pub fn into_one_shot( + mut self, + ) -> Result<Ads1x1x<DI, 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 { iface: self.iface, config: self.config, @@ -24,37 +28,12 @@ where }) } - /// Start continuous conversions - /// - /// _Note:_ this method is only available in continuous mode. - pub fn start(&mut self) -> Result<(), Error<E>> { - self.set_operating_mode(OperatingMode::Continuous)?; - self.a_conversion_was_started = true; - Ok(()) - } - /// Read the most recent measurement - /// - /// The continuous measurement must be started with [`start()`] before - /// calling this method. Otherwise, `Error::NotStarted` will be returned. - /// - /// _Note:_ this method is only available in continuous mode. - /// - /// [`start()`]: struct.Ads1x1x.html#method.start pub fn read(&mut self) -> Result<i16, Error<E>> { - if !self.a_conversion_was_started { - return Err(Error::NotStarted); - } let value = self.iface.read_register(Register::CONVERSION)?; Ok(CONV::convert_measurement(value)) } -} -impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous> -where - DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, - CONV: conversion::ConvertMeasurement, -{ /// Select the channel for measurements. /// /// Note that when changing the channel in continuous conversion mode, the diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 3595c26..612d5a4 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,9 +1,10 @@ //! Common functions +use super::super::OperatingMode; use channels::ChannelSelection; use core::marker::PhantomData; use {conversion, hal, interface, nb}; -use {mode, Ads1x1x, BitFlags, Config, Error, Register}; +use {mode, Ads1x1x, BitFlags, Config, Error, ModeChangeError, Register}; impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> where @@ -11,12 +12,17 @@ where CONV: conversion::ConvertMeasurement, { /// Change operating mode to Continuous - pub fn into_continuous(self) -> Result<Ads1x1x<DI, IC, CONV, mode::Continuous>, Error<E>> { + pub fn into_continuous( + mut self, + ) -> Result<Ads1x1x<DI, IC, CONV, mode::Continuous>, ModeChangeError<E, Self>> { + if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::Continuous) { + return Err(ModeChangeError::I2C(e, self)); + } Ok(Ads1x1x { iface: self.iface, config: self.config, fsr: self.fsr, - a_conversion_was_started: self.a_conversion_was_started, + a_conversion_was_started: true, _conv: PhantomData, _ic: PhantomData, _mode: PhantomData, @@ -140,21 +140,25 @@ //! //! ### Change into continuous conversion mode and read the last measurement //! +//! Changing the mode may fail in case there was a communication error. +//! In this case, you can retrieve the unchanged device from the error type. +//! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate ads1x1x; -//! use ads1x1x::{ Ads1x1x, SlaveAddr }; +//! use ads1x1x::{ Ads1x1x, SlaveAddr, ModeChangeError }; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let address = SlaveAddr::default(); //! let adc = Ads1x1x::new_ads1013(dev, address); -//! let mut adc = adc.into_continuous().unwrap(); -//! adc.start().unwrap(); -//! while(adc.is_measurement_in_progress().unwrap()) { -//! // some delay... +//! match adc.into_continuous() { +//! Err(ModeChangeError::I2C(e, adc)) => /* mode change failed handling */ panic!(), +//! Ok(mut adc) => { +//! let measurement = adc.read().unwrap(); +//! // ... +//! } //! } -//! let measurement = adc.read().unwrap(); //! # } //! ``` //! @@ -212,15 +216,24 @@ extern crate embedded_hal as hal; extern crate nb; use core::marker::PhantomData; -/// All possible errors in this crate +/// Errors in this crate #[derive(Debug)] pub enum Error<E> { /// I²C bus error I2C(E), /// Invalid input data provided InvalidInputData, - /// Continuous measurement was not started - NotStarted, +} + +/// Error type for mode changes. +/// +/// This allows to retrieve the unchanged device in case of an error. +pub enum ModeChangeError<E, DEV> { + /// I²C bus error while changing mode. + /// + /// `E` is the error that happened. + /// `DEV` is the device with the mode unchanged. + I2C(E, DEV), } const DEVICE_BASE_ADDRESS: u8 = 0b100_1000; |