From fce3d8546bd619964ec5dd1594cd93e744a99521 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 11 Nov 2018 17:32:52 +0100 Subject: Implement value conversions over type parameter --- src/channels.rs | 2 +- src/construction/i2c.rs | 19 ++++----- src/conversion.rs | 92 ++++++++++++++++++++++++++++++++++++++++++ src/devices/common.rs | 7 ++-- src/devices/features/tier1.rs | 7 ++-- src/devices/features/tier2.rs | 56 +++---------------------- src/devices/mode/continuous.rs | 8 ++-- src/devices/mode/mod.rs | 43 -------------------- src/devices/mode/oneshot.rs | 21 +++++----- src/ic.rs | 27 +++++-------- src/lib.rs | 11 ++++- 11 files changed, 148 insertions(+), 145 deletions(-) create mode 100644 src/conversion.rs (limited to 'src') 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 hal::adc::Channel> for channel::$CH { + impl hal::adc::Channel> 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 Ads1x1x, ic::$IC, mode::OneShot> + ( $IC:ident, $create:ident, $destroy:ident, $conv:ty, $converter:expr ) => { + impl Ads1x1x, ic::$IC, $conv, mode::OneShot> where I2C: blocking::i2c::Write + blocking::i2c::WriteRead { @@ -22,12 +22,13 @@ macro_rules! impl_new_destroy { }, config: Config::default(), a_conversion_was_started: false, + converter: $converter(()), _ic: PhantomData, _mode: PhantomData } } } - impl Ads1x1x, ic::$IC, MODE> + impl Ads1x1x, 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 : private::Sealed { + fn convert_threshold(value: i16) -> Result>; +} + +impl ConvertThreshold for ic::Resolution12Bit { + fn convert_threshold(value: i16) -> Result> { + if value < -2048 || value > 2047 { + return Err(Error::InvalidInputData); + } + Ok((value << 4) as u16) + } +} + +impl ConvertThreshold for ic::Resolution16Bit { + fn convert_threshold(value: i16) -> Result> { + 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(result: Result>) { + 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, >::convert_threshold(0).unwrap()); + assert_eq!(0x7FF0, >::convert_threshold(2047).unwrap()); + assert_eq!(0x8000, >::convert_threshold(-2048).unwrap()); + assert_eq!(0xFFF0, >::convert_threshold(-1).unwrap()); + } + + #[test] + fn convert_threshold_16_bits() { + assert_eq!(0x7FFF, >::convert_threshold(32767).unwrap()); + assert_eq!(0x8000, >::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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData, - IC: ic::Resolution + DI: interface::WriteData { pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error> { 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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData, - IC: ic::Resolution + DI: interface::WriteData { /// Set data rate pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error> { 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 Ads1x1x +impl Ads1x1x where DI: interface::WriteData, - IC: ic::Resolution + ic::Tier2Features + IC: ic::Tier2Features, + CONV: conversion::ConvertThreshold { /// Set comparator lower threshold pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error> { - let register_value = convert_threshold::(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> { - let register_value = convert_threshold::(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(value: i16) -> Result> -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(result: Result>) { - match result { - Err(Error::InvalidInputData) => (), - _ => panic!("InvalidInputData error was not returned.") - } - } - - #[test] - fn convert_12_bits() { - assert_invalid_input_data(convert_threshold::(2048)); - assert_invalid_input_data(convert_threshold::(-2049)); - - assert_eq!( 0, convert_threshold::(0).unwrap()); - assert_eq!(0x7FF0, convert_threshold::(2047).unwrap()); - assert_eq!(0x8000, convert_threshold::(-2048).unwrap()); - assert_eq!(0xFFF0, convert_threshold::(-1).unwrap()); - } - - #[test] - fn convert_16_bits() { - assert_eq!(0x7FFF, convert_threshold::(32767).unwrap()); - assert_eq!(0x8000, convert_threshold::(-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 Ads1x1x +impl Ads1x1x where DI: interface::WriteData, - IC: ic::Resolution { /// Change operating mode to OneShot - pub fn into_one_shot(mut self) -> Result, Error> { + pub fn into_one_shot(mut self) -> Result, Error> { 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(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::(0)); - assert_eq!( 2047, convert_measurement::(0x7FFF)); - assert_eq!(-2048, convert_measurement::(0x8000)); - assert_eq!( -1, convert_measurement::(0xFFFF)); - } - - #[test] - fn convert_16_bits() { - assert_eq!( 0, convert_measurement::(0)); - assert_eq!( 32767, convert_measurement::(0x7FFF)); - assert_eq!(-32768, convert_measurement::(0x8000)); - assert_eq!( -1, convert_measurement::(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 Ads1x1x +impl Ads1x1x where DI: interface::WriteData + interface::ReadData, - IC: ic::Resolution + CONV: conversion::ConvertMeasurement { /// Change operating mode to Continuous - pub fn into_continuous(mut self) -> Result, Error> { + pub fn into_continuous(mut self) -> Result, Error> { 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 hal::adc::OneShot, i16, CH> for Ads1x1x +impl hal::adc::OneShot, i16, CH> + for Ads1x1x where DI: interface::ReadData + interface::WriteData, - IC: ic::Resolution, - CH: hal::adc::Channel, ID = ChannelSelection> + CONV: conversion::ConvertMeasurement, + CH: hal::adc::Channel, ID = ChannelSelection> { type Error = Error; @@ -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::(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)?; diff --git a/src/ic.rs b/src/ic.rs index 7b30f85..a09cb72 100644 --- a/src/ic.rs +++ b/src/ic.rs @@ -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 { } diff --git a/src/lib.rs b/src/lib.rs index 6601888..2d421fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -275,10 +275,11 @@ impl Default for Config { /// ADS1x1x ADC driver #[derive(Debug, Default)] -pub struct Ads1x1x { +pub struct Ads1x1x { iface: DI, config: Config, a_conversion_was_started: bool, + converter: CONV, _ic: PhantomData, _mode: PhantomData } @@ -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 Sealed for interface::I2cInterface {} impl Sealed for interface::SpiInterface {} + 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 {} -- cgit v1.2.3-54-g00ecf