diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 17:32:52 +0100 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-11 17:32:52 +0100 |
commit | fce3d8546bd619964ec5dd1594cd93e744a99521 (patch) | |
tree | 7bc17d465652c86b25b9fb34ccd7ed3bf0218b56 /src/devices/mode | |
parent | b9b5349b4b78a5d59be4a7e4a11f712e2f6a8256 (diff) | |
download | ads1x1x-async-fce3d8546bd619964ec5dd1594cd93e744a99521.tar.gz ads1x1x-async-fce3d8546bd619964ec5dd1594cd93e744a99521.tar.xz ads1x1x-async-fce3d8546bd619964ec5dd1594cd93e744a99521.zip |
Implement value conversions over type parameter
Diffstat (limited to 'src/devices/mode')
-rw-r--r-- | src/devices/mode/continuous.rs | 8 | ||||
-rw-r--r-- | src/devices/mode/mod.rs | 43 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 21 |
3 files changed, 15 insertions, 57 deletions
diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index 1472478..a06df60 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -1,21 +1,21 @@ //! Common functions use core::marker::PhantomData; -use { Ads1x1x, mode, Error, interface, ic }; +use { Ads1x1x, mode, Error, interface }; use super::super::OperatingMode; -impl<DI, IC, E> Ads1x1x<DI, IC, mode::Continuous> +impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, 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>> { + pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, CONV, 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, + converter: self.converter, _ic: PhantomData, _mode: PhantomData }) diff --git a/src/devices/mode/mod.rs b/src/devices/mode/mod.rs index 8e0e012..9625465 100644 --- a/src/devices/mode/mod.rs +++ b/src/devices/mode/mod.rs @@ -1,47 +1,4 @@ //! 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 index 57e72a0..e8aa533 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,24 +1,24 @@ //! Common functions use core::marker::PhantomData; -use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic }; -use { interface, hal, nb }; +use { Ads1x1x, mode, Error, Register, BitFlags, Config }; +use { interface, conversion, hal, nb }; use devices::OperatingMode; use channels::ChannelSelection; -use super::convert_measurement; -impl<DI, IC, E> Ads1x1x<DI, IC, mode::OneShot> +impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> where DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>, - IC: ic::Resolution + CONV: conversion::ConvertMeasurement { /// Change operating mode to Continuous - pub fn into_continuous(mut self) -> Result<Ads1x1x<DI, IC, mode::Continuous>, Error<E>> { + pub fn into_continuous(mut self) -> Result<Ads1x1x<DI, IC, CONV, 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, + converter: self.converter, _ic: PhantomData, _mode: PhantomData }) @@ -37,11 +37,12 @@ where } } -impl<DI, IC, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, mode::OneShot>, i16, CH> for Ads1x1x<DI, IC, mode::OneShot> +impl<DI, IC, CONV, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, CONV, mode::OneShot>, i16, CH> + for Ads1x1x<DI, IC, CONV, 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> + CONV: conversion::ConvertMeasurement, + CH: hal::adc::Channel<Ads1x1x<DI, IC, CONV, mode::OneShot>, ID = ChannelSelection> { type Error = Error<E>; @@ -61,7 +62,7 @@ where // 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)); + return Ok(CONV::convert_measurement(value)); } let config = self.config.with_mux_bits(CH::channel()); self.trigger_measurement(&config).map_err(nb::Error::Other)?; |