diff options
-rw-r--r-- | src/devices/ads1x1x/features/mod.rs | 2 | ||||
-rw-r--r-- | src/devices/ads1x1x/features/tier2.rs | 23 | ||||
-rw-r--r-- | src/lib.rs | 19 | ||||
-rw-r--r-- | tests/common/mod.rs | 1 | ||||
-rw-r--r-- | tests/tier2_i2c.rs | 32 |
5 files changed, 76 insertions, 1 deletions
diff --git a/src/devices/ads1x1x/features/mod.rs b/src/devices/ads1x1x/features/mod.rs index ad26258..c24b7b4 100644 --- a/src/devices/ads1x1x/features/mod.rs +++ b/src/devices/ads1x1x/features/mod.rs @@ -2,4 +2,4 @@ //! support. mod tier1; - +mod tier2; diff --git a/src/devices/ads1x1x/features/tier2.rs b/src/devices/ads1x1x/features/tier2.rs new file mode 100644 index 0000000..d238691 --- /dev/null +++ b/src/devices/ads1x1x/features/tier2.rs @@ -0,0 +1,23 @@ +//! Tier 2 features. +//! +//! These are the features included only in ADS1x14, ADS1x15 + +use { Ads1x1x, Error, interface, ic, ComparatorMode, Register, BitFlags }; + +impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE> +where + DI: interface::WriteData<Error = E>, + IC: ic::Resolution + ic::Tier2Features +{ + /// Set comparator mode + pub fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> { + let config; + match mode { + ComparatorMode::Traditional => config = self.config.with_low(BitFlags::COMP_MODE), + ComparatorMode::Window => config = self.config.with_high(BitFlags::COMP_MODE) + } + self.iface.write_register(Register::CONFIG, config.bits)?; + self.config = config; + Ok(()) + } +} @@ -98,6 +98,24 @@ pub enum DataRate { Sps3300 } +/// Comparator mode (only for ADS1x14, ADS1x15) +#[derive(Debug, Clone)] +pub enum ComparatorMode { + /// Traditional comparator (default) + /// + /// In this mode the ALERT/RDY pin asserts (according to selected active + /// polarity) when the conversion data exceeds the limit set as *high* + /// threshold and remains active until the conversion data falls below the + /// value set as *low* threshold. + Traditional, + /// Window comparator + /// + /// In this mode the ALERT/RDY pin asserts (according to selected active + /// polarity) when the conversion data exceeds the value set as *high* + /// threshold or goes below the value set as *low* temperature threshold. + Window +} + /// Possible slave addresses #[derive(Debug, Clone)] pub enum SlaveAddr { @@ -141,6 +159,7 @@ impl BitFlags { 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; } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 7f55942..89b81c9 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -21,6 +21,7 @@ impl BitFlags { 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 struct Config { diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs new file mode 100644 index 0000000..1eb2871 --- /dev/null +++ b/tests/tier2_i2c.rs @@ -0,0 +1,32 @@ +extern crate embedded_hal; +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +extern crate ads1x1x; +use ads1x1x::ComparatorMode; + +#[macro_use] +mod common; +use common::{ new_ads1014, destroy_ads1014, + DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; + +macro_rules! test_set_comparator_mode { + ($name:ident, $variant:ident, $config:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let mut dev = new_ads1014(&transactions); + dev.set_comparator_mode(ComparatorMode::$variant).unwrap(); + destroy_ads1014(dev); + } + } +} + + +mod can_set_comparator_mode { + use super::*; + test_set_comparator_mode!(traditional, Traditional, Config::default().with_low( BitFlags::COMP_MODE)); + test_set_comparator_mode!(window, Window, Config::default().with_high(BitFlags::COMP_MODE)); +} + + + |