summaryrefslogtreecommitdiffstats
path: root/src/devices/ads1x1x
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2018-11-11 08:04:42 +0100
committerDiego Barrios Romero <eldruin@gmail.com>2018-11-11 08:04:42 +0100
commit3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a (patch)
tree8cf40e43cb93b15a774696c3af639d504addce8c /src/devices/ads1x1x
parent8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc (diff)
downloadads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.tar.gz
ads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.tar.xz
ads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.zip
Reorganize modules
Diffstat (limited to 'src/devices/ads1x1x')
-rw-r--r--src/devices/ads1x1x/common.rs36
-rw-r--r--src/devices/ads1x1x/features/mod.rs5
-rw-r--r--src/devices/ads1x1x/features/tier1.rs83
-rw-r--r--src/devices/ads1x1x/features/tier2.rs48
-rw-r--r--src/devices/ads1x1x/mod.rs10
-rw-r--r--src/devices/ads1x1x/mode/continuous.rs23
-rw-r--r--src/devices/ads1x1x/mode/mod.rs47
-rw-r--r--src/devices/ads1x1x/mode/oneshot.rs66
8 files changed, 0 insertions, 318 deletions
diff --git a/src/devices/ads1x1x/common.rs b/src/devices/ads1x1x/common.rs
deleted file mode 100644
index 5abb99c..0000000
--- a/src/devices/ads1x1x/common.rs
+++ /dev/null
@@ -1,36 +0,0 @@
-//! Common functions
-
-use { Ads1x1x, Error, Register, BitFlags, interface, Config, ic };
-use super::OperatingMode;
-
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
-where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
-{
- pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {
- let config;
- match mode {
- OperatingMode::OneShot => config = self.config.with_high(BitFlags::OP_MODE),
- OperatingMode::Continuous => config = self.config.with_low(BitFlags::OP_MODE),
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
- }
-
- /// Reset the internal state of this driver to the default values.
- ///
- /// *Note:* This does not alter the state or configuration of the device.
- ///
- /// This resets the cached configuration register value in this driver to
- /// the power-up (reset) configuration of the device.
- ///
- /// This needs to be called after performing a reset on the device, for
- /// example through an I2C general-call Reset command, which was not done
- /// through this driver to ensure that the configurations in the device
- /// and in the driver match.
- pub fn reset_internal_driver_state(&mut self) {
- self.config = Config::default();
- }
-}
diff --git a/src/devices/ads1x1x/features/mod.rs b/src/devices/ads1x1x/features/mod.rs
deleted file mode 100644
index c24b7b4..0000000
--- a/src/devices/ads1x1x/features/mod.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-//! Implementation of IC features separated in tiers depending on the hardware
-//! support.
-
-mod tier1;
-mod tier2;
diff --git a/src/devices/ads1x1x/features/tier1.rs b/src/devices/ads1x1x/features/tier1.rs
deleted file mode 100644
index 9f72b77..0000000
--- a/src/devices/ads1x1x/features/tier1.rs
+++ /dev/null
@@ -1,83 +0,0 @@
-//! Common functions
-
-use { Ads1x1x, DataRate, Error, Register, BitFlags, interface, ic };
-
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
-where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
-{
- /// Set data rate
- pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<E>> {
- let config;
- match rate {
- DataRate::Sps128 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps250 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps490 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps920 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps1600 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps2400 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps3300 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
- }
-
- /// Set comparator lower threshold
- pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error<E>> {
- let register_value = convert_threshold::<IC, E>(value)?;
- self.iface.write_register(Register::LOW_TH, register_value)
- }
-
- /// Set comparator upper threshold
- pub fn set_high_threshold(&mut self, value: i16) -> Result<(), Error<E>> {
- let register_value = convert_threshold::<IC, E>(value)?;
- self.iface.write_register(Register::HIGH_TH, register_value)
- }
-}
-
-fn convert_threshold<IC, E>(value: i16) -> Result<u16, Error<E>>
-where
- IC: ic::Resolution
-{
- if IC::BITS == ic::ResolutionBits::_12 {
- if value < -2048 || value > 2047 {
- return Err(Error::InvalidInputData);
- }
- Ok((value << 4) as u16)
- }
- else {
- Ok(value as u16)
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- fn assert_invalid_input_data<E>(result: Result<u16, Error<E>>) {
- match result {
- Err(Error::InvalidInputData) => (),
- _ => panic!("InvalidInputData error was not returned.")
- }
- }
-
- #[test]
- fn convert_12_bits() {
- assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(2048));
- assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(-2049));
-
- assert_eq!( 0, convert_threshold::<ic::Ads1013, ()>(0).unwrap());
- assert_eq!(0x7FF0, convert_threshold::<ic::Ads1013, ()>(2047).unwrap());
- assert_eq!(0x8000, convert_threshold::<ic::Ads1013, ()>(-2048).unwrap());
- assert_eq!(0xFFF0, convert_threshold::<ic::Ads1013, ()>(-1).unwrap());
- }
-
- #[test]
- fn convert_16_bits() {
- assert_eq!(0x7FFF, convert_threshold::<ic::Ads1113, ()>(32767).unwrap());
- assert_eq!(0x8000, convert_threshold::<ic::Ads1113,()>(-32768).unwrap());
- }
-}
-
diff --git a/src/devices/ads1x1x/features/tier2.rs b/src/devices/ads1x1x/features/tier2.rs
deleted file mode 100644
index 43dce5c..0000000
--- a/src/devices/ads1x1x/features/tier2.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-//! Tier 2 features.
-//!
-//! These are the features included only in ADS1x14, ADS1x15
-
-use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity,
- ComparatorLatching, Register, BitFlags };
-
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
-where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution + ic::Tier2Features
-{
- /// Set comparator mode
- pub fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> {
- let config;
- match mode {
- ComparatorMode::Traditional => config = self.config.with_low(BitFlags::COMP_MODE),
- ComparatorMode::Window => config = self.config.with_high(BitFlags::COMP_MODE)
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
- }
-
- /// Set comparator polarity
- pub fn set_comparator_polarity(&mut self, polarity: ComparatorPolarity) -> Result<(), Error<E>> {
- let config;
- match polarity {
- ComparatorPolarity::ActiveLow => config = self.config.with_low( BitFlags::COMP_POL),
- ComparatorPolarity::ActiveHigh => config = self.config.with_high(BitFlags::COMP_POL)
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
- }
-
- /// Set comparator latching
- pub fn set_comparator_latching(&mut self, latching: ComparatorLatching) -> Result<(), Error<E>> {
- let config;
- match latching {
- ComparatorLatching::Nonlatching => config = self.config.with_low( BitFlags::COMP_LAT),
- ComparatorLatching::Latching => config = self.config.with_high(BitFlags::COMP_LAT)
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
- }
-}
diff --git a/src/devices/ads1x1x/mod.rs b/src/devices/ads1x1x/mod.rs
deleted file mode 100644
index 95d8907..0000000
--- a/src/devices/ads1x1x/mod.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-//! Functions for all devices
-
-enum OperatingMode {
- OneShot,
- Continuous
-}
-
-mod common;
-mod mode;
-mod features;
diff --git a/src/devices/ads1x1x/mode/continuous.rs b/src/devices/ads1x1x/mode/continuous.rs
deleted file mode 100644
index 1472478..0000000
--- a/src/devices/ads1x1x/mode/continuous.rs
+++ /dev/null
@@ -1,23 +0,0 @@
-//! Common functions
-
-use core::marker::PhantomData;
-use { Ads1x1x, mode, Error, interface, ic };
-use super::super::OperatingMode;
-
-impl<DI, IC, E> Ads1x1x<DI, IC, mode::Continuous>
-where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
-{
- /// Change operating mode to OneShot
- pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, mode::OneShot>, Error<E>> {
- self.set_operating_mode(OperatingMode::OneShot)?;
- Ok(Ads1x1x {
- iface: self.iface,
- config: self.config,
- a_conversion_was_started: self.a_conversion_was_started,
- _ic: PhantomData,
- _mode: PhantomData
- })
- }
-}
diff --git a/src/devices/ads1x1x/mode/mod.rs b/src/devices/ads1x1x/mode/mod.rs
deleted file mode 100644
index 8e0e012..0000000
--- a/src/devices/ads1x1x/mode/mod.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-//! Functions for all devices specific to each operating mode
-
-use ic;
-mod oneshot;
-mod continuous;
-
-fn convert_measurement<IC>(register_data: u16) -> i16
-where
- IC: ic::Resolution
-{
- let value = register_data;
- if IC::BITS == ic::ResolutionBits::_12 {
- let is_negative = (value & 0b1000_0000_0000_0000) != 0;
- if is_negative {
- let value = 0b1111_0000_0000_0000 | (value >> 4);
- value as i16
- }
- else {
- (value >> 4) as i16
- }
- }
- else {
- value as i16
- }
-}
-
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn convert_12_bits() {
- assert_eq!( 0, convert_measurement::<ic::Ads1013>(0));
- assert_eq!( 2047, convert_measurement::<ic::Ads1013>(0x7FFF));
- assert_eq!(-2048, convert_measurement::<ic::Ads1013>(0x8000));
- assert_eq!( -1, convert_measurement::<ic::Ads1013>(0xFFFF));
- }
-
- #[test]
- fn convert_16_bits() {
- assert_eq!( 0, convert_measurement::<ic::Ads1113>(0));
- assert_eq!( 32767, convert_measurement::<ic::Ads1113>(0x7FFF));
- assert_eq!(-32768, convert_measurement::<ic::Ads1113>(0x8000));
- assert_eq!( -1, convert_measurement::<ic::Ads1113>(0xFFFF));
- }
-}
diff --git a/src/devices/ads1x1x/mode/oneshot.rs b/src/devices/ads1x1x/mode/oneshot.rs
deleted file mode 100644
index 7ff9ac9..0000000
--- a/src/devices/ads1x1x/mode/oneshot.rs
+++ /dev/null
@@ -1,66 +0,0 @@
-//! Common functions
-
-use core::marker::PhantomData;
-use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic };
-use { interface, hal, nb };
-use devices::ads1x1x::OperatingMode;
-use devices::channels::ChannelSelection;
-use super::convert_measurement;
-
-impl<DI, IC, E> Ads1x1x<DI, IC, mode::OneShot>
-where
- DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
- IC: ic::Resolution
-{
- /// Change operating mode to Continuous
- pub fn into_continuous(mut self) -> Result<Ads1x1x<DI, IC, mode::Continuous>, Error<E>> {
- self.set_operating_mode(OperatingMode::Continuous)?;
- Ok(Ads1x1x {
- iface: self.iface,
- config: self.config,
- a_conversion_was_started: self.a_conversion_was_started,
- _ic: PhantomData,
- _mode: PhantomData
- })
- }
-
- fn is_measurement_in_progress(&mut self) -> Result<bool, Error<E>> {
- let config = Config {
- bits: self.iface.read_register(Register::CONFIG)?
- };
- Ok(!config.is_high(BitFlags::OS))
- }
-
- fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> {
- let config = config.with_high(BitFlags::OS);
- self.iface.write_register(Register::CONFIG, config.bits)
- }
-}
-
-impl<DI, IC, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, mode::OneShot>, i16, CH> for Ads1x1x<DI, IC, mode::OneShot>
-where
- DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,
- IC: ic::Resolution,
- CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>, ID = ChannelSelection>
-{
- type Error = Error<E>;
-
- /// Request that the ADC begin a conversion on the specified channel
- fn read(&mut self, _channel: &mut CH) -> nb::Result<i16, Self::Error> {
- //TODO for devices with MUX select channel, if it is the wrong one, return AlreadyInProgress or WrongChannel error
- if self.is_measurement_in_progress().map_err(nb::Error::Other)? {
- return Err(nb::Error::WouldBlock);
- }
- if self.a_conversion_was_started {
- // result is ready
- let value = self.iface.read_register(Register::CONVERSION).map_err(nb::Error::Other)?;
- self.a_conversion_was_started = false;
- return Ok(convert_measurement::<IC>(value));
- }
- let config = self.config.with_mux_bits(CH::channel());
- self.trigger_measurement(&config).map_err(nb::Error::Other)?;
- self.config = config;
- self.a_conversion_was_started = true;
- Err(nb::Error::WouldBlock)
- }
-}