diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 08:04:42 +0100 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 08:04:42 +0100 |
commit | 3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a (patch) | |
tree | 8cf40e43cb93b15a774696c3af639d504addce8c /src/devices/mode | |
parent | 8b9e9ecdb85a30ac1c57fe6b96498cae6ad9f7cc (diff) | |
download | ads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.tar.gz ads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.tar.xz ads1x1x-async-3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a.zip |
Reorganize modules
Diffstat (limited to 'src/devices/mode')
-rw-r--r-- | src/devices/mode/continuous.rs | 23 | ||||
-rw-r--r-- | src/devices/mode/mod.rs | 47 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 66 |
3 files changed, 136 insertions, 0 deletions
diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs new file mode 100644 index 0000000..1472478 --- /dev/null +++ b/src/devices/mode/continuous.rs @@ -0,0 +1,23 @@ +//! 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/mode/mod.rs b/src/devices/mode/mod.rs new file mode 100644 index 0000000..8e0e012 --- /dev/null +++ b/src/devices/mode/mod.rs @@ -0,0 +1,47 @@ +//! 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/mode/oneshot.rs b/src/devices/mode/oneshot.rs new file mode 100644 index 0000000..30958ab --- /dev/null +++ b/src/devices/mode/oneshot.rs @@ -0,0 +1,66 @@ +//! Common functions + +use core::marker::PhantomData; +use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic }; +use { interface, hal, nb }; +use devices::OperatingMode; +use 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) + } +} |