From 3e3a790d9f70bfd6cb4210dfe09560d0385fdf5a Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 11 Nov 2018 08:04:42 +0100 Subject: Reorganize modules --- src/devices/ads1x1x/features/tier1.rs | 83 ----------------------------------- 1 file changed, 83 deletions(-) delete mode 100644 src/devices/ads1x1x/features/tier1.rs (limited to 'src/devices/ads1x1x/features/tier1.rs') 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 Ads1x1x -where - DI: interface::WriteData, - IC: ic::Resolution -{ - /// Set data rate - pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error> { - 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> { - let register_value = 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)?; - self.iface.write_register(Register::HIGH_TH, register_value) - } -} - -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()); - } -} - -- cgit v1.2.3-54-g00ecf