diff options
author | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-10 09:34:00 +0100 |
---|---|---|
committer | Diego Barrios Romero <eldruin@gmail.com> | 2018-11-10 09:34:00 +0100 |
commit | bad297c7e4c1f39842aa55ac44506e22d154bce0 (patch) | |
tree | d4b48ab8b25cecad2e77c034dda1716c34c93da9 | |
parent | df8e200554dadee3f4addc887f775be03b7467cf (diff) | |
download | ads1x1x-async-bad297c7e4c1f39842aa55ac44506e22d154bce0.tar.gz ads1x1x-async-bad297c7e4c1f39842aa55ac44506e22d154bce0.tar.xz ads1x1x-async-bad297c7e4c1f39842aa55ac44506e22d154bce0.zip |
Add support for setting the comparator polarity
-rw-r--r-- | src/devices/ads1x1x/features/tier2.rs | 15 | ||||
-rw-r--r-- | src/lib.rs | 10 | ||||
-rw-r--r-- | tests/common/mod.rs | 1 | ||||
-rw-r--r-- | tests/tier2_i2c.rs | 20 |
4 files changed, 43 insertions, 3 deletions
diff --git a/src/devices/ads1x1x/features/tier2.rs b/src/devices/ads1x1x/features/tier2.rs index d238691..7d22796 100644 --- a/src/devices/ads1x1x/features/tier2.rs +++ b/src/devices/ads1x1x/features/tier2.rs @@ -2,7 +2,8 @@ //! //! These are the features included only in ADS1x14, ADS1x15 -use { Ads1x1x, Error, interface, ic, ComparatorMode, Register, BitFlags }; +use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity, + Register, BitFlags }; impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE> where @@ -20,4 +21,16 @@ where self.config = config; Ok(()) } + + /// Set comparator polarity + pub fn set_comparator_polarity(&mut self, polarity: ComparatorPolarity) -> Result<(), Error<E>> { + let config; + match polarity { + ComparatorPolarity::ActiveLow => config = self.config.with_low( BitFlags::COMP_POL), + ComparatorPolarity::ActiveHigh => config = self.config.with_high(BitFlags::COMP_POL) + } + self.iface.write_register(Register::CONFIG, config.bits)?; + self.config = config; + Ok(()) + } } @@ -124,6 +124,15 @@ pub enum ComparatorMode { Window } +/// Comparator polarity (only for ADS1x14, ADS1x15) +#[derive(Debug, Clone)] +pub enum ComparatorPolarity { + /// Active low (default) + ActiveLow, + /// Active high + ActiveHigh +} + /// Possible slave addresses #[derive(Debug, Clone)] pub enum SlaveAddr { @@ -168,6 +177,7 @@ impl BitFlags { 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; } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 89b81c9..8ffd50e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -22,6 +22,7 @@ impl BitFlags { 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 struct Config { diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs index 1eb2871..5a3c950 100644 --- a/tests/tier2_i2c.rs +++ b/tests/tier2_i2c.rs @@ -2,7 +2,7 @@ extern crate embedded_hal; extern crate embedded_hal_mock as hal; use hal::i2c::Transaction as I2cTrans; extern crate ads1x1x; -use ads1x1x::ComparatorMode; +use ads1x1x::{ ComparatorMode, ComparatorPolarity }; #[macro_use] mod common; @@ -21,12 +21,28 @@ macro_rules! test_set_comparator_mode { } } - 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)); } +macro_rules! test_set_comparator_pol { + ($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_polarity(ComparatorPolarity::$variant).unwrap(); + destroy_ads1014(dev); + } + } +} + +mod can_set_comparator_polarity { + use super::*; + test_set_comparator_pol!(low, ActiveLow, Config::default().with_low( BitFlags::COMP_POL)); + test_set_comparator_pol!(high, ActiveHigh, Config::default().with_high(BitFlags::COMP_POL)); +} |