diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2019-03-30 13:36:06 +0200 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2019-03-31 13:41:26 +0200 |
commit | 50223ad02e2f487540107e320c8bef40a9849582 (patch) | |
tree | 58b4e44e2261940d07aba96b8d8e83457e641f53 | |
parent | ab1221e175f2da577c7a3fb510b5ae5cf41b0697 (diff) | |
download | ads1x1x-async-50223ad02e2f487540107e320c8bef40a9849582.tar.gz ads1x1x-async-50223ad02e2f487540107e320c8bef40a9849582.tar.xz ads1x1x-async-50223ad02e2f487540107e320c8bef40a9849582.zip |
Format code
-rw-r--r-- | src/channels.rs | 40 | ||||
-rw-r--r-- | src/conversion.rs | 13 | ||||
-rw-r--r-- | src/devices/features/tier2.rs | 12 | ||||
-rw-r--r-- | src/devices/mod.rs | 4 | ||||
-rw-r--r-- | src/devices/mode/mod.rs | 2 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 10 | ||||
-rw-r--r-- | src/lib.rs | 55 | ||||
-rw-r--r-- | tests/common/mod.rs | 40 | ||||
-rw-r--r-- | tests/construction.rs | 5 | ||||
-rw-r--r-- | tests/mux.rs | 84 | ||||
-rw-r--r-- | tests/tier1.rs | 242 | ||||
-rw-r--r-- | tests/tier2.rs | 187 |
12 files changed, 518 insertions, 176 deletions
diff --git a/src/channels.rs b/src/channels.rs index d5ae7bc..90b9b5d 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -74,14 +74,38 @@ impl Config { pub(crate) fn with_mux_bits(&self, ch: ChannelSelection) -> Self { use self::ChannelSelection as CS; match ch { - CS::DifferentialA0A1 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), - CS::DifferentialA0A3 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), - CS::DifferentialA1A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), - CS::DifferentialA2A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), - CS::SingleA0 => self.with_high(BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), - CS::SingleA1 => self.with_high(BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), - CS::SingleA2 => self.with_high(BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), - CS::SingleA3 => self.with_high(BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), + CS::DifferentialA0A1 => self + .with_low(BF::MUX2) + .with_low(BF::MUX1) + .with_low(BF::MUX0), + CS::DifferentialA0A3 => self + .with_low(BF::MUX2) + .with_low(BF::MUX1) + .with_high(BF::MUX0), + CS::DifferentialA1A3 => self + .with_low(BF::MUX2) + .with_high(BF::MUX1) + .with_low(BF::MUX0), + CS::DifferentialA2A3 => self + .with_low(BF::MUX2) + .with_high(BF::MUX1) + .with_high(BF::MUX0), + CS::SingleA0 => self + .with_high(BF::MUX2) + .with_low(BF::MUX1) + .with_low(BF::MUX0), + CS::SingleA1 => self + .with_high(BF::MUX2) + .with_low(BF::MUX1) + .with_high(BF::MUX0), + CS::SingleA2 => self + .with_high(BF::MUX2) + .with_high(BF::MUX1) + .with_low(BF::MUX0), + CS::SingleA3 => self + .with_high(BF::MUX2) + .with_high(BF::MUX1) + .with_high(BF::MUX0), } } } diff --git a/src/conversion.rs b/src/conversion.rs index fef80ac..2be9f1e 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -44,25 +44,24 @@ impl ConvertMeasurement for ic::Resolution16Bit { } } - #[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!(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)); + 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!(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)); + assert_eq!(-1, ic::Resolution16Bit::convert_measurement(0xFFFF)); } fn assert_invalid_input_data<E>(result: Result<u16, Error<E>>) { diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 48e4eb9..0b0b92c 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -128,7 +128,10 @@ where /// The comparator can be enabled by setting the comparator queue. /// See [`set_comparator_queue()`](struct.Ads1x1x.html#method.set_comparator_queue) pub fn disable_comparator(&mut self) -> Result<(), Error<E>> { - let config = self.config.with_high(BF::COMP_QUE1).with_high(BF::COMP_QUE0); + let config = self + .config + .with_high(BF::COMP_QUE1) + .with_high(BF::COMP_QUE0); self.iface.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) @@ -142,7 +145,12 @@ where /// /// When calling this the comparator will be disabled and the thresholds will be cleared. pub fn use_alert_rdy_pin_as_ready(&mut self) -> Result<(), Error<E>> { - if self.config != self.config.with_high(BF::COMP_QUE1).with_high(BF::COMP_QUE0) { + if self.config + != self + .config + .with_high(BF::COMP_QUE1) + .with_high(BF::COMP_QUE0) + { self.disable_comparator()?; } self.iface.write_register(Register::HIGH_TH, 0x8000)?; diff --git a/src/devices/mod.rs b/src/devices/mod.rs index 5b87738..01efeba 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -4,6 +4,6 @@ enum OperatingMode { Continuous, } -mod mode; -mod features; mod common; +mod features; +mod mode; diff --git a/src/devices/mode/mod.rs b/src/devices/mode/mod.rs index 9625465..670ebe9 100644 --- a/src/devices/mode/mod.rs +++ b/src/devices/mode/mod.rs @@ -1,4 +1,4 @@ //! Functions for all devices specific to each operating mode -mod oneshot; mod continuous; +mod oneshot; diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 612d5a4..5901292 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -58,13 +58,19 @@ where /// measurement on a different channel is requested, a new measurement on /// using the new channel selection is triggered. fn read(&mut self, _channel: &mut CH) -> nb::Result<i16, Self::Error> { - if self.is_measurement_in_progress().map_err(nb::Error::Other)? { + if self + .is_measurement_in_progress() + .map_err(nb::Error::Other)? + { return Err(nb::Error::WouldBlock); } let same_channel = self.config == self.config.with_mux_bits(CH::channel()); if self.a_conversion_was_started && same_channel { // result is ready - let value = self.iface.read_register(Register::CONVERSION).map_err(nb::Error::Other)?; + let value = self + .iface + .read_register(Register::CONVERSION) + .map_err(nb::Error::Other)?; self.a_conversion_was_started = false; return Ok(CONV::convert_measurement(value)); } @@ -388,9 +388,7 @@ impl SlaveAddr { fn addr(self, default: u8) -> u8 { match self { SlaveAddr::Default => default, - SlaveAddr::Alternative(a1, a0) => default | - ((a1 as u8) << 1) | - a0 as u8 + SlaveAddr::Alternative(a1, a0) => default | ((a1 as u8) << 1) | a0 as u8, } } } @@ -398,33 +396,32 @@ impl SlaveAddr { struct Register; impl Register { - const CONVERSION : u8 = 0x00; - const CONFIG : u8 = 0x01; - const LOW_TH : u8 = 0x02; - const HIGH_TH : u8 = 0x03; + const CONVERSION: u8 = 0x00; + const CONFIG: u8 = 0x01; + const LOW_TH: u8 = 0x02; + const HIGH_TH: u8 = 0x03; } struct BitFlags; impl BitFlags { - const OS : u16 = 0b1000_0000_0000_0000; - const MUX2 : u16 = 0b0100_0000_0000_0000; - const MUX1 : u16 = 0b0010_0000_0000_0000; - const MUX0 : u16 = 0b0001_0000_0000_0000; - const PGA2 : u16 = 0b0000_1000_0000_0000; - const PGA1 : u16 = 0b0000_0100_0000_0000; - const PGA0 : u16 = 0b0000_0010_0000_0000; - const OP_MODE : u16 = 0b0000_0001_0000_0000; - const DR2 : u16 = 0b0000_0000_1000_0000; - const DR1 : u16 = 0b0000_0000_0100_0000; - const DR0 : u16 = 0b0000_0000_0010_0000; - const COMP_MODE : u16 = 0b0000_0000_0001_0000; - const COMP_POL : u16 = 0b0000_0000_0000_1000; - const COMP_LAT : u16 = 0b0000_0000_0000_0100; - const COMP_QUE1 : u16 = 0b0000_0000_0000_0010; - const COMP_QUE0 : u16 = 0b0000_0000_0000_0001; + const OS: u16 = 0b1000_0000_0000_0000; + const MUX2: u16 = 0b0100_0000_0000_0000; + const MUX1: u16 = 0b0010_0000_0000_0000; + const MUX0: u16 = 0b0001_0000_0000_0000; + const PGA2: u16 = 0b0000_1000_0000_0000; + const PGA1: u16 = 0b0000_0100_0000_0000; + const PGA0: u16 = 0b0000_0010_0000_0000; + const OP_MODE: u16 = 0b0000_0001_0000_0000; + const DR2: u16 = 0b0000_0000_1000_0000; + const DR1: u16 = 0b0000_0000_0100_0000; + const DR0: u16 = 0b0000_0000_0010_0000; + const COMP_MODE: u16 = 0b0000_0000_0001_0000; + const COMP_POL: u16 = 0b0000_0000_0000_1000; + const COMP_LAT: u16 = 0b0000_0000_0000_0100; + const COMP_QUE1: u16 = 0b0000_0000_0000_0010; + const COMP_QUE0: u16 = 0b0000_0000_0000_0001; } - #[derive(Debug, Clone, PartialEq)] struct Config { bits: u16, @@ -471,17 +468,17 @@ pub struct Ads1x1x<DI, IC, CONV, MODE> { _mode: PhantomData<MODE>, } -#[doc(hidden)] -pub mod interface; +mod channels; #[doc(hidden)] pub mod ic; -mod channels; +#[doc(hidden)] +pub mod interface; pub use channels::channel; -mod devices; mod construction; mod conversion; -pub use conversion::ConvertThreshold; +mod devices; pub use conversion::ConvertMeasurement; +pub use conversion::ConvertThreshold; mod private { use super::{ic, interface}; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ae3236c..223f97a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -9,31 +9,31 @@ pub const DEVICE_ADDRESS: u8 = 0b100_1000; pub struct Register; #[allow(unused)] impl Register { - pub const CONVERSION : u8 = 0x00; - pub const CONFIG : u8 = 0x01; - pub const LOW_TH : u8 = 0x02; - pub const HIGH_TH : u8 = 0x03; + pub const CONVERSION: u8 = 0x00; + pub const CONFIG: u8 = 0x01; + pub const LOW_TH: u8 = 0x02; + pub const HIGH_TH: u8 = 0x03; } pub struct BitFlags; #[allow(unused)] impl BitFlags { - pub const OS : u16 = 0b1000_0000_0000_0000; - pub const MUX2 : u16 = 0b0100_0000_0000_0000; - pub const MUX1 : u16 = 0b0010_0000_0000_0000; - pub const MUX0 : u16 = 0b0001_0000_0000_0000; - pub const PGA2 : u16 = 0b0000_1000_0000_0000; - pub const PGA1 : u16 = 0b0000_0100_0000_0000; - pub const PGA0 : u16 = 0b0000_0010_0000_0000; - pub const OP_MODE : u16 = 0b0000_0001_0000_0000; - pub const DR2 : u16 = 0b0000_0000_1000_0000; - pub const DR1 : u16 = 0b0000_0000_0100_0000; - pub const DR0 : u16 = 0b0000_0000_0010_0000; - pub const COMP_MODE : u16 = 0b0000_0000_0001_0000; - pub const COMP_POL : u16 = 0b0000_0000_0000_1000; - pub const COMP_LAT : u16 = 0b0000_0000_0000_0100; - pub const COMP_QUE1 : u16 = 0b0000_0000_0000_0010; - pub const COMP_QUE0 : u16 = 0b0000_0000_0000_0001; + pub const OS: u16 = 0b1000_0000_0000_0000; + pub const MUX2: u16 = 0b0100_0000_0000_0000; + pub const MUX1: u16 = 0b0010_0000_0000_0000; + pub const MUX0: u16 = 0b0001_0000_0000_0000; + pub const PGA2: u16 = 0b0000_1000_0000_0000; + pub const PGA1: u16 = 0b0000_0100_0000_0000; + pub const PGA0: u16 = 0b0000_0010_0000_0000; + pub const OP_MODE: u16 = 0b0000_0001_0000_0000; + pub const DR2: u16 = 0b0000_0000_1000_0000; + pub const DR1: u16 = 0b0000_0000_0100_0000; + pub const DR0: u16 = 0b0000_0000_0010_0000; + pub const COMP_MODE: u16 = 0b0000_0000_0001_0000; + pub const COMP_POL: u16 = 0b0000_0000_0000_1000; + pub const COMP_LAT: u16 = 0b0000_0000_0000_0100; + pub const COMP_QUE1: u16 = 0b0000_0000_0000_0010; + pub const COMP_QUE0: u16 = 0b0000_0000_0000_0001; } pub struct Config { diff --git a/tests/construction.rs b/tests/construction.rs index 698d06a..f31ad2f 100644 --- a/tests/construction.rs +++ b/tests/construction.rs @@ -1,8 +1,7 @@ mod common; use common::{ - new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113, - new_ads1014, destroy_ads1014, new_ads1114, destroy_ads1114, - new_ads1015, destroy_ads1015, new_ads1115, destroy_ads1115 + destroy_ads1013, destroy_ads1014, destroy_ads1015, destroy_ads1113, destroy_ads1114, + destroy_ads1115, new_ads1013, new_ads1014, new_ads1015, new_ads1113, new_ads1114, new_ads1115, }; macro_rules! impl_tests { diff --git a/tests/mux.rs b/tests/mux.rs index b961fc7..309656e 100644 --- a/tests/mux.rs +++ b/tests/mux.rs @@ -7,24 +7,35 @@ extern crate ads1x1x; use ads1x1x::channel; mod common; -use common::{ new_ads1015 as new, destroy_ads1015 as destroy, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags as BF, Config }; - +use common::{ + destroy_ads1015 as destroy, new_ads1015 as new, BitFlags as BF, Config, Register, + DEVICE_ADDRESS as DEV_ADDR, +}; macro_rules! mux_test { ($name:ident, $CS:ident, $config_bits:expr, $other_CS:ident, $other_config_bits:expr) => { mod $name { - use embedded_hal::adc::OneShot; use super::*; + use embedded_hal::adc::OneShot; #[test] fn can_read() { let default_config = Config::default(); let config = Config::default().with_high(BF::OS).with_high($config_bits); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let transactions = [ + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![default_config.msb(), default_config.lsb()], + ), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config.msb(), config.lsb()], + ), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), + ]; let mut dev = new(&transactions); let measurement = block!(dev.read(&mut channel::$CS)).unwrap(); assert_eq!(-2048, measurement); @@ -36,12 +47,29 @@ macro_rules! mux_test { let default_config = Config::default(); let config = Config::default().with_high(BF::OS).with_high($config_bits); let other_config = Config::default().with_high($other_config_bits); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, other_config.msb(), other_config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![other_config.msb(), other_config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let transactions = [ + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![default_config.msb(), default_config.lsb()], + ), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config.msb(), config.lsb()], + ), + I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, other_config.msb(), other_config.lsb()], + ), + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![other_config.msb(), other_config.lsb()], + ), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), + ]; let mut dev = new(&transactions); assert_would_block!(dev.read(&mut channel::$CS)); let measurement = block!(dev.read(&mut channel::$other_CS)).unwrap(); @@ -54,10 +82,14 @@ macro_rules! mux_test { let config1 = Config::default().with_low(BF::OP_MODE); let config2 = config1.with_high($config_bits); let transactions = [ - I2cTrans::write(DEV_ADDR, - vec![Register::CONFIG, config1.msb(), config1.lsb()]), - I2cTrans::write(DEV_ADDR, - vec![Register::CONFIG, config2.msb(), config2.lsb()]) + I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, config1.msb(), config1.lsb()], + ), + I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, config2.msb(), config2.lsb()], + ), ]; let dev = new(&transactions); let mut dev = dev.into_continuous().ok().unwrap(); @@ -71,8 +103,20 @@ macro_rules! mux_test { mux_test!(diffa0a1, DifferentialA0A1, 0, SingleA0, BF::MUX2); mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0, SingleA0, BF::MUX2); mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1, SingleA0, BF::MUX2); -mux_test!(diffa2a3, DifferentialA2A3, BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!( + diffa2a3, + DifferentialA2A3, + BF::MUX1 | BF::MUX0, + SingleA0, + BF::MUX2 +); mux_test!(singlea0, SingleA0, BF::MUX2, DifferentialA0A1, 0); mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0, SingleA0, BF::MUX2); mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1, SingleA0, BF::MUX2); -mux_test!(singlea3, SingleA3, BF::MUX2 | BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!( + singlea3, + SingleA3, + BF::MUX2 | BF::MUX1 | BF::MUX0, + SingleA0, + BF::MUX2 +); diff --git a/tests/tier1.rs b/tests/tier1.rs index de44b3c..89a60e0 100644 --- a/tests/tier1.rs +++ b/tests/tier1.rs @@ -7,22 +7,28 @@ extern crate ads1x1x; use ads1x1x::{channel, DataRate12Bit, DataRate16Bit}; mod common; -use common::{ new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; +use common::{ + destroy_ads1013, destroy_ads1113, new_ads1013, new_ads1113, BitFlags as BF, Config, Register, + DEVICE_ADDRESS as DEV_ADDR, +}; macro_rules! measure_tests { ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => { mod $IC { - use embedded_hal::adc::OneShot; use super::*; + use embedded_hal::adc::OneShot; mod would_block { use super::*; #[test] fn read_if_measurement_in_progress() { - let config = Config::default().with_low(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()] ) ]; + let config = Config::default().with_low(BF::OS); + let transactions = [I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config.msb(), config.lsb()], + )]; let mut dev = $create(&transactions); assert_would_block!(dev.read(&mut channel::DifferentialA0A1)); $destroy(dev); @@ -32,11 +38,24 @@ macro_rules! measure_tests { #[test] fn can_measure() { let default_config = Config::default(); - let config_with_os = Config::default().with_high(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_with_os.msb(), config_with_os.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_with_os.msb(), config_with_os.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let config_with_os = Config::default().with_high(BF::OS); + let transactions = [ + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![default_config.msb(), default_config.lsb()], + ), + I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, config_with_os.msb(), config_with_os.lsb()], + ), + I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config_with_os.msb(), config_with_os.lsb()], + ), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), + ]; let mut dev = $create(&transactions); let measurement = block!(dev.read(&mut channel::DifferentialA0A1)).unwrap(); assert_eq!($expected, measurement); @@ -45,9 +64,11 @@ macro_rules! measure_tests { #[test] fn can_measure_continuous() { - let config = Config::default().with_low(BitFlags::OP_MODE); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let config = Config::default().with_low(BF::OP_MODE); + let transactions = [ + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00]), + ]; let dev = $create(&transactions); let mut dev = dev.into_continuous().ok().unwrap(); let measurement = dev.read().unwrap(); @@ -55,64 +76,180 @@ macro_rules! measure_tests { $destroy(dev); } } - } + }; } -measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); +measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); measure_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); - mod data_rate_12bit { + use super::*; + macro_rules! test { ($name:ident, $variant:ident, $config:expr) => { #[test] fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let transactions = [I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, $config.msb(), $config.lsb()], + )]; let mut dev = new_ads1013(&transactions); dev.set_data_rate(DataRate12Bit::$variant).unwrap(); destroy_ads1013(dev); } - } + }; } - use super::*; - test!(sps128, Sps128, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps250, Sps250, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps490, Sps490, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps920, Sps920, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!( + sps128, + Sps128, + Config::default() + .with_low(BF::DR2) + .with_low(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps250, + Sps250, + Config::default() + .with_low(BF::DR2) + .with_low(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps490, + Sps490, + Config::default() + .with_low(BF::DR2) + .with_high(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps920, + Sps920, + Config::default() + .with_low(BF::DR2) + .with_high(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps1600, + Sps1600, + Config::default() + .with_high(BF::DR2) + .with_low(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps2400, + Sps2400, + Config::default() + .with_high(BF::DR2) + .with_low(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps3300, + Sps3300, + Config::default() + .with_high(BF::DR2) + .with_high(BF::DR1) + .with_low(BF::DR0) + ); } mod data_rate_16bit { + use super::*; + macro_rules! test { ($name:ident, $variant:ident, $config:expr) => { #[test] fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let transactions = [I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, $config.msb(), $config.lsb()], + )]; let mut dev = new_ads1113(&transactions); dev.set_data_rate(DataRate16Bit::$variant).unwrap(); destroy_ads1113(dev); } - } + }; } - use super::*; - test!(sps8, Sps8, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps16, Sps16, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps32, Sps32, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps64, Sps64, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps128, Sps128, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps250, Sps250, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test!(sps475, Sps475, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test!(sps860, Sps860, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); + test!( + sps8, + Sps8, + Config::default() + .with_low(BF::DR2) + .with_low(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps16, + Sps16, + Config::default() + .with_low(BF::DR2) + .with_low(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps32, + Sps32, + Config::default() + .with_low(BF::DR2) + .with_high(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps64, + Sps64, + Config::default() + .with_low(BF::DR2) + .with_high(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps128, + Sps128, + Config::default() + .with_high(BF::DR2) + .with_low(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps250, + Sps250, + Config::default() + .with_high(BF::DR2) + .with_low(BF::DR1) + .with_high(BF::DR0) + ); + test!( + sps475, + Sps475, + Config::default() + .with_high(BF::DR2) + .with_high(BF::DR1) + .with_low(BF::DR0) + ); + test!( + sps860, + Sps860, + Config::default() + .with_high(BF::DR2) + .with_high(BF::DR1) + .with_high(BF::DR0) + ); } #[test] fn can_read_measurement_in_progress() { - let config_os = Config::default().with_low(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; + let config_os = Config::default().with_low(BF::OS); + let transactions = [I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config_os.msb(), config_os.lsb()], + )]; let mut dev = new_ads1013(&transactions); assert!(dev.is_measurement_in_progress().unwrap()); destroy_ads1013(dev); @@ -120,8 +257,12 @@ fn can_read_measurement_in_progress() { #[test] fn can_read_measurement_not_in_progress() { - let config_os = Config::default().with_high(BitFlags::OS); - let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config_os.msb(), config_os.lsb()]) ]; + let config_os = Config::default().with_high(BF::OS); + let transactions = [I2cTrans::write_read( + DEV_ADDR, + vec![Register::CONFIG], + vec![config_os.msb(), config_os.lsb()], + )]; let mut dev = new_ads1013(&transactions); assert!(!dev.is_measurement_in_progress().unwrap()); destroy_ads1013(dev); @@ -129,7 +270,7 @@ fn can_read_measurement_not_in_progress() { #[test] fn can_convert_to_continuous() { - let config = Config::default().with_low(BitFlags::OP_MODE); + let config = Config::default().with_low(BF::OP_MODE); let transactions = [I2cTrans::write( DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()], @@ -141,15 +282,18 @@ fn can_convert_to_continuous() { #[test] fn can_convert_to_one_shot() { - let config_cont = Config::default().with_low(BitFlags::OP_MODE); + let config_cont = Config::default().with_low(BF::OP_MODE); let config_os = Config::default(); - let transactions = [I2cTrans::write( - DEV_ADDR, - vec![Register::CONFIG, config_cont.msb(), config_cont.lsb()]), + let transactions = [ I2cTrans::write( - DEV_ADDR, - vec![Register::CONFIG, config_os.msb(), config_os.lsb()], - )]; + DEV_ADDR, + vec![Register::CONFIG, config_cont.msb(), config_cont.lsb()], + ), + I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, config_os.msb(), config_os.lsb()], + ), + ]; let dev = new_ads1013(&transactions); let dev = dev.into_continuous().ok().unwrap(); let dev = dev.into_one_shot().ok().unwrap(); diff --git a/tests/tier2.rs b/tests/tier2.rs index 62a9759..e42a348 100644 --- a/tests/tier2.rs +++ b/tests/tier2.rs @@ -1,59 +1,96 @@ extern crate embedded_hal_mock; use embedded_hal_mock::i2c::Transaction as I2cTrans; extern crate ads1x1x; -use ads1x1x::{ FullScaleRange, ComparatorMode, ComparatorPolarity, - ComparatorLatching, ComparatorQueue }; +use ads1x1x::{ + ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue, FullScaleRange, +}; mod common; -use common::{ new_ads1014, destroy_ads1014, - DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; +use common::{ + destroy_ads1014, new_ads1014, BitFlags as BF, Config, Register, DEVICE_ADDRESS as DEV_ADDR, +}; macro_rules! set_value_test { ($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => { #[test] fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::$reg, $msb, $lsb]) ]; + let transactions = [I2cTrans::write(DEV_ADDR, vec![Register::$reg, $msb, $lsb])]; let mut dev = new_ads1014(&transactions); dev.$method($value).unwrap(); destroy_ads1014(dev); } - } + }; } macro_rules! config_test { ($name:ident, $method:ident, $value:expr, $config:expr) => { set_value_test!($name, $method, $value, CONFIG, $config.msb(), $config.lsb()); - } + }; } mod can_set_comparator_thresholds { use super::*; - set_value_test!(low, set_low_threshold_raw, 2047, LOW_TH, 0x7F, 0xF0); + set_value_test!(low, set_low_threshold_raw, 2047, LOW_TH, 0x7F, 0xF0); set_value_test!(high, set_high_threshold_raw, 2047, HIGH_TH, 0x7F, 0xF0); } mod can_set_comparator_mode { use super::*; - config_test!(traditional, set_comparator_mode, ComparatorMode::Traditional, Config::default().with_low( BitFlags::COMP_MODE)); - config_test!(window, set_comparator_mode, ComparatorMode::Window, Config::default().with_high(BitFlags::COMP_MODE)); + config_test!( + traditional, + set_comparator_mode, + ComparatorMode::Traditional, + Config::default().with_low(BF::COMP_MODE) + ); + config_test!( + window, + set_comparator_mode, + ComparatorMode::Window, + Config::default().with_high(BF::COMP_MODE) + ); } mod can_set_comparator_polarity { use super::*; - config_test!(low, set_comparator_polarity, ComparatorPolarity::ActiveLow, Config::default().with_low( BitFlags::COMP_POL)); - config_test!(high, set_comparator_polarity, ComparatorPolarity::ActiveHigh, Config::default().with_high(BitFlags::COMP_POL)); + config_test!( + low, + set_comparator_polarity, + ComparatorPolarity::ActiveLow, + Config::default().with_low(BF::COMP_POL) + ); + config_test!( + high, + set_comparator_polarity, + ComparatorPolarity::ActiveHigh, + Config::default().with_high(BF::COMP_POL) + ); } mod can_set_comparator_latching { use super::*; - config_test!(non, set_comparator_latching, ComparatorLatching::Nonlatching, Config::default().with_low( BitFlags::COMP_LAT)); - config_test!(lat, set_comparator_latching, ComparatorLatching::Latching, Config::default().with_high(BitFlags::COMP_LAT)); + config_test!( + non, + set_comparator_latching, + ComparatorLatching::Nonlatching, + Config::default().with_low(BF::COMP_LAT) + ); + config_test!( + lat, + set_comparator_latching, + ComparatorLatching::Latching, + Config::default().with_high(BF::COMP_LAT) + ); } #[test] fn can_disable_comparator() { - let config = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; + let config = Config::default() + .with_high(BF::COMP_QUE1) + .with_high(BF::COMP_QUE0); + let transactions = [I2cTrans::write( + DEV_ADDR, + vec![Register::CONFIG, config.msb(), config.lsb()], + )]; let mut dev = new_ads1014(&transactions); dev.disable_comparator().unwrap(); destroy_ads1014(dev); @@ -61,15 +98,38 @@ fn can_disable_comparator() { mod can_set_comparator_queue { use super::*; - config_test!(one, set_comparator_queue, ComparatorQueue::One, Config::default().with_low( BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); - config_test!(two, set_comparator_queue, ComparatorQueue::Two, Config::default().with_low( BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0)); - config_test!(four, set_comparator_queue, ComparatorQueue::Four, Config::default().with_high(BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); + config_test!( + one, + set_comparator_queue, + ComparatorQueue::One, + Config::default() + .with_low(BF::COMP_QUE1) + .with_low(BF::COMP_QUE0) + ); + config_test!( + two, + set_comparator_queue, + ComparatorQueue::Two, + Config::default() + .with_low(BF::COMP_QUE1) + .with_high(BF::COMP_QUE0) + ); + config_test!( + four, + set_comparator_queue, + ComparatorQueue::Four, + Config::default() + .with_high(BF::COMP_QUE1) + .with_low(BF::COMP_QUE0) + ); } #[test] fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), - I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let transactions = [ + I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), + ]; let mut dev = new_ads1014(&transactions); dev.use_alert_rdy_pin_as_ready().unwrap(); destroy_ads1014(dev); @@ -77,12 +137,25 @@ fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled( #[test] fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { - let config = Config::default().with_low(BitFlags::COMP_QUE1).with_low(BitFlags::COMP_QUE0); - let config_disabled_comp = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_disabled_comp.msb(), config_disabled_comp.lsb()]), - I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), - I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let config = Config::default() + .with_low(BF::COMP_QUE1) + .with_low(BF::COMP_QUE0); + let config_disabled_comp = Config::default() + .with_high(BF::COMP_QUE1) + .with_high(BF::COMP_QUE0); + let transactions = [ + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write( + DEV_ADDR, + vec![ + Register::CONFIG, + config_disabled_comp.msb(), + config_disabled_comp.lsb(), + ], + ), + I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), + ]; let mut dev = new_ads1014(&transactions); dev.set_comparator_queue(ComparatorQueue::One).unwrap(); dev.use_alert_rdy_pin_as_ready().unwrap(); @@ -91,10 +164,58 @@ fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { mod can_set_full_scale_range { use super::*; - config_test!(fsr6, set_full_scale_range, FullScaleRange::Within6_144V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr4, set_full_scale_range, FullScaleRange::Within4_096V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); - config_test!(fsr2, set_full_scale_range, FullScaleRange::Within2_048V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr1, set_full_scale_range, FullScaleRange::Within1_024V, Config::default().with_low( BitFlags::PGA2).with_high(BitFlags::PGA1).with_high(BitFlags::PGA0)); - config_test!(fsr0_5, set_full_scale_range, FullScaleRange::Within0_512V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); - config_test!(fsr0_2, set_full_scale_range, FullScaleRange::Within0_256V, Config::default().with_high(BitFlags::PGA2).with_low( BitFlags::PGA1).with_high(BitFlags::PGA0)); + config_test!( + fsr6, + set_full_scale_range, + FullScaleRange::Within6_144V, + Config::default() + .with_low(BF::PGA2) + .with_low(BF::PGA1) + .with_low(BF::PGA0) + ); + config_test!( + fsr4, + set_full_scale_range, + FullScaleRange::Within4_096V, + Config::default() + .with_low(BF::PGA2) + .with_low(BF::PGA1) + .with_high(BF::PGA0) + ); + config_test!( + fsr2, + set_full_scale_range, + FullScaleRange::Within2_048V, + Config::default() + .with_low(BF::PGA2) + .with_high(BF::PGA1) + .with_low(BF::PGA0) + ); + config_test!( + fsr1, + set_full_scale_range, + FullScaleRange::Within1_024V, + Config::default() + .with_low(BF::PGA2) + .with_high(BF::PGA1) + .with_high(BF::PGA0) + ); + config_test!( + fsr0_5, + set_full_scale_range, + FullScaleRange::Within0_512V, + Config::default() + .with_high(BF::PGA2) + .with_low(BF::PGA1) + .with_low(BF::PGA0) + ); + config_test!( + fsr0_2, + set_full_scale_range, + FullScaleRange::Within0_256V, + Config::default() + .with_high(BF::PGA2) + .with_low(BF::PGA1) + .with_high(BF::PGA0) + ); } |