summaryrefslogtreecommitdiffstats
path: root/src/devices/mode
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/mode')
-rw-r--r--src/devices/mode/continuous.rs35
-rw-r--r--src/devices/mode/oneshot.rs12
2 files changed, 16 insertions, 31 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,