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 | |
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')
-rw-r--r-- | src/channels.rs | 2 | ||||
-rw-r--r-- | src/construction/i2c.rs | 19 | ||||
-rw-r--r-- | src/conversion.rs | 92 | ||||
-rw-r--r-- | src/devices/common.rs | 7 | ||||
-rw-r--r-- | src/devices/features/tier1.rs | 7 | ||||
-rw-r--r-- | src/devices/features/tier2.rs | 56 | ||||
-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 | ||||
-rw-r--r-- | src/ic.rs | 27 | ||||
-rw-r--r-- | src/lib.rs | 11 |
11 files changed, 148 insertions, 145 deletions
diff --git a/src/channels.rs b/src/channels.rs index c202ad3..5b0d4d4 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -35,7 +35,7 @@ pub enum ChannelSelection { macro_rules! impl_channel { ( $IC:ident, $CH:ident ) => { - impl<DI, MODE> hal::adc::Channel<Ads1x1x<DI, ic::$IC, MODE>> for channel::$CH { + impl<DI, CONV, MODE> hal::adc::Channel<Ads1x1x<DI, ic::$IC, CONV, MODE>> for channel::$CH { type ID = ChannelSelection; fn channel() -> Self::ID { diff --git a/src/construction/i2c.rs b/src/construction/i2c.rs index 9ef18eb..3ec474b 100644 --- a/src/construction/i2c.rs +++ b/src/construction/i2c.rs @@ -8,8 +8,8 @@ use interface::I2cInterface; macro_rules! impl_new_destroy { - ( $IC:ident, $create:ident, $destroy:ident ) => { - impl<I2C, E> Ads1x1x<I2cInterface<I2C>, ic::$IC, mode::OneShot> + ( $IC:ident, $create:ident, $destroy:ident, $conv:ty, $converter:expr ) => { + impl<I2C, E> Ads1x1x<I2cInterface<I2C>, ic::$IC, $conv, mode::OneShot> where I2C: blocking::i2c::Write<Error = E> + blocking::i2c::WriteRead<Error = E> { @@ -22,12 +22,13 @@ macro_rules! impl_new_destroy { }, config: Config::default(), a_conversion_was_started: false, + converter: $converter(()), _ic: PhantomData, _mode: PhantomData } } } - impl<I2C, MODE> Ads1x1x<I2cInterface<I2C>, ic::$IC, MODE> + impl<I2C, CONV, MODE> Ads1x1x<I2cInterface<I2C>, ic::$IC, CONV, MODE> { /// Destroy driver instance, return I²C bus instance. pub fn $destroy(self) -> I2C { @@ -37,9 +38,9 @@ macro_rules! impl_new_destroy { } } -impl_new_destroy!(Ads1013, new_ads1013, destroy_ads1013); -impl_new_destroy!(Ads1113, new_ads1113, destroy_ads1113); -impl_new_destroy!(Ads1014, new_ads1014, destroy_ads1014); -impl_new_destroy!(Ads1114, new_ads1114, destroy_ads1114); -impl_new_destroy!(Ads1015, new_ads1015, destroy_ads1015); -impl_new_destroy!(Ads1115, new_ads1115, destroy_ads1115); +impl_new_destroy!(Ads1013, new_ads1013, destroy_ads1013, ic::Resolution12Bit, ic::Resolution12Bit); +impl_new_destroy!(Ads1113, new_ads1113, destroy_ads1113, ic::Resolution16Bit, ic::Resolution16Bit); +impl_new_destroy!(Ads1014, new_ads1014, destroy_ads1014, ic::Resolution12Bit, ic::Resolution12Bit); +impl_new_destroy!(Ads1114, new_ads1114, destroy_ads1114, ic::Resolution16Bit, ic::Resolution16Bit); +impl_new_destroy!(Ads1015, new_ads1015, destroy_ads1015, ic::Resolution12Bit, ic::Resolution12Bit); +impl_new_destroy!(Ads1115, new_ads1115, destroy_ads1115, ic::Resolution16Bit, ic::Resolution16Bit); diff --git a/src/conversion.rs b/src/conversion.rs new file mode 100644 index 0000000..20d04c7 --- /dev/null +++ b/src/conversion.rs @@ -0,0 +1,92 @@ + +use { ic, private, Error }; + +#[doc(hidden)] +pub trait ConvertThreshold<E> : private::Sealed { + fn convert_threshold(value: i16) -> Result<u16, Error<E>>; +} + +impl<E> ConvertThreshold<E> for ic::Resolution12Bit { + fn convert_threshold(value: i16) -> Result<u16, Error<E>> { + if value < -2048 || value > 2047 { + return Err(Error::InvalidInputData); + } + Ok((value << 4) as u16) + } +} + +impl<E> ConvertThreshold<E> for ic::Resolution16Bit { + fn convert_threshold(value: i16) -> Result<u16, Error<E>> { + Ok(value as u16) + } +} + +#[doc(hidden)] +pub trait ConvertMeasurement : private::Sealed { + fn convert_measurement(register_data: u16) -> i16; +} + +impl ConvertMeasurement for ic::Resolution12Bit { + fn convert_measurement(register_data: u16) -> i16 { + let value = register_data; + 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 + } + } +} + +impl ConvertMeasurement for ic::Resolution16Bit { + fn convert_measurement(register_data: u16) -> i16 { + register_data as i16 + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn convert_measurement_12_bits() { + assert_eq!( 0, ic::Resolution12Bit::convert_measurement(0)); + assert_eq!( 2047, ic::Resolution12Bit::convert_measurement(0x7FFF)); + assert_eq!(-2048, ic::Resolution12Bit::convert_measurement(0x8000)); + assert_eq!( -1, ic::Resolution12Bit::convert_measurement(0xFFFF)); + } + + #[test] + fn convert_measurement_16_bits() { + assert_eq!( 0, ic::Resolution16Bit::convert_measurement(0)); + assert_eq!( 32767, ic::Resolution16Bit::convert_measurement(0x7FFF)); + assert_eq!(-32768, ic::Resolution16Bit::convert_measurement(0x8000)); + assert_eq!( -1, ic::Resolution16Bit::convert_measurement(0xFFFF)); + } + + 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_threshold_12_bits() { + assert_invalid_input_data::<()>(ic::Resolution12Bit::convert_threshold(2048)); + assert_invalid_input_data::<()>(ic::Resolution12Bit::convert_threshold(-2049)); + assert_eq!( 0, <ic::Resolution12Bit as ConvertThreshold<()>>::convert_threshold(0).unwrap()); + assert_eq!(0x7FF0, <ic::Resolution12Bit as ConvertThreshold<()>>::convert_threshold(2047).unwrap()); + assert_eq!(0x8000, <ic::Resolution12Bit as ConvertThreshold<()>>::convert_threshold(-2048).unwrap()); + assert_eq!(0xFFF0, <ic::Resolution12Bit as ConvertThreshold<()>>::convert_threshold(-1).unwrap()); + } + + #[test] + fn convert_threshold_16_bits() { + assert_eq!(0x7FFF, <ic::Resolution16Bit as ConvertThreshold<()>>::convert_threshold(32767).unwrap()); + assert_eq!(0x8000, <ic::Resolution16Bit as ConvertThreshold<()>>::convert_threshold(-32768).unwrap()); + } +} diff --git a/src/devices/common.rs b/src/devices/common.rs index 5abb99c..1121265 100644 --- a/src/devices/common.rs +++ b/src/devices/common.rs @@ -1,12 +1,11 @@ //! Common functions -use { Ads1x1x, Error, Register, BitFlags, interface, Config, ic }; +use { Ads1x1x, Error, Register, BitFlags, interface, Config }; use super::OperatingMode; -impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE> +impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> where - DI: interface::WriteData<Error = E>, - IC: ic::Resolution + DI: interface::WriteData<Error = E> { pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> { let config; diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs index 4bbf7d1..30e900e 100644 --- a/src/devices/features/tier1.rs +++ b/src/devices/features/tier1.rs @@ -1,11 +1,10 @@ //! Common functions -use { Ads1x1x, DataRate, Error, Register, BitFlags, interface, ic }; +use { Ads1x1x, DataRate, Error, Register, BitFlags, interface }; -impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE> +impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> where - DI: interface::WriteData<Error = E>, - IC: ic::Resolution + DI: interface::WriteData<Error = E> { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<E>> { diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 2aa649f..5d7ec92 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -3,22 +3,23 @@ //! These are the features included only in ADS1x14, ADS1x15 use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity, - ComparatorLatching, Register, BitFlags }; + ComparatorLatching, Register, BitFlags, conversion }; -impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE> +impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> where DI: interface::WriteData<Error = E>, - IC: ic::Resolution + ic::Tier2Features + IC: ic::Tier2Features, + CONV: conversion::ConvertThreshold<E> { /// Set comparator lower threshold pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error<E>> { - let register_value = convert_threshold::<IC, E>(value)?; + let register_value = CONV::convert_threshold(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)?; + let register_value = CONV::convert_threshold(value)?; self.iface.write_register(Register::HIGH_TH, register_value) } @@ -58,48 +59,3 @@ where Ok(()) } } - - -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/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)?; @@ -1,32 +1,23 @@ /// ICs use private; -#[derive(PartialEq)] -pub enum ResolutionBits { - _12, - _16 -} +pub struct Resolution12Bit(pub(crate)()); +pub struct Resolution16Bit(pub(crate)()); -pub trait Resolution : private::Sealed { - const BITS : ResolutionBits; -} macro_rules! ic_marker { - ($name:ident, $resolution:ident) => { + ($name:ident) => { /// IC marker pub struct $name(()); - impl Resolution for $name { - const BITS: ResolutionBits = ResolutionBits::$resolution; - } }; } -ic_marker!(Ads1013, _12); -ic_marker!(Ads1113, _16); -ic_marker!(Ads1014, _12); -ic_marker!(Ads1114, _16); -ic_marker!(Ads1015, _12); -ic_marker!(Ads1115, _16); +ic_marker!(Ads1013); +ic_marker!(Ads1113); +ic_marker!(Ads1014); +ic_marker!(Ads1114); +ic_marker!(Ads1015); +ic_marker!(Ads1115); pub trait Tier2Features : private::Sealed { } @@ -275,10 +275,11 @@ impl Default for Config { /// ADS1x1x ADC driver #[derive(Debug, Default)] -pub struct Ads1x1x<DI, IC, MODE> { +pub struct Ads1x1x<DI, IC, CONV, MODE> { iface: DI, config: Config, a_conversion_was_started: bool, + converter: CONV, _ic: PhantomData<IC>, _mode: PhantomData<MODE> } @@ -288,9 +289,12 @@ pub mod interface; #[doc(hidden)] pub mod ic; mod channels; +pub use channels::channel; mod devices; mod construction; -pub use channels::channel; +mod conversion; +pub use conversion::ConvertThreshold; +pub use conversion::ConvertMeasurement; mod private { use super::{ ic, interface }; @@ -299,6 +303,9 @@ mod private { impl<I2C> Sealed for interface::I2cInterface<I2C> {} impl<SPI, CS> Sealed for interface::SpiInterface<SPI, CS> {} + impl Sealed for ic::Resolution12Bit {} + impl Sealed for ic::Resolution16Bit {} + impl Sealed for ic::Ads1013 {} impl Sealed for ic::Ads1113 {} impl Sealed for ic::Ads1014 {} |