diff options
-rw-r--r-- | src/devices/features/tier2.rs | 18 | ||||
-rw-r--r-- | src/lib.rs | 13 | ||||
-rw-r--r-- | tests/tier2_i2c.rs | 11 |
3 files changed, 40 insertions, 2 deletions
diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 17502e6..25b618a 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -3,7 +3,7 @@ //! These are the features included only in ADS1x14, ADS1x15 use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity, - ComparatorLatching, Register, BitFlags, conversion }; + ComparatorLatching, ComparatorQueue, Register, BitFlags, conversion }; impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> where @@ -58,6 +58,22 @@ where self.config = config; Ok(()) } + + /// Activate comparator and set the alert queue + /// + /// The comparator can be disabled with [`disable_comparator()`](struct.Ads1x1x.html#method.disable_comparator) + pub fn set_comparator_queue(&mut self, queue: ComparatorQueue) -> Result<(), Error<E>> { + let config; + match queue { + ComparatorQueue::One => config = self.config.with_low( BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0), + ComparatorQueue::Two => config = self.config.with_low( BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0), + ComparatorQueue::Four => config = self.config.with_high(BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0) + } + self.iface.write_register(Register::CONFIG, config.bits)?; + self.config = config; + Ok(()) + } + /// Disable comparator (default) /// /// This will set the ALERT/RDY pin to high-impedance. @@ -12,6 +12,7 @@ //! - Set the comparator mode. See: [`set_comparator_mode()`]. //! - Set the comparator polarity. See: [`set_comparator_polarity()`]. //! - Set the comparator latching. See: [`set_comparator_latching()`]. +//! - Set the comparator queue. See: [`set_comparator_queue()`]. //! - Disable the comparator. See: [`disable_comparator()`]. //! //! [`into_continuous()`]: struct.Ads1x1x.html#method.into_continuous @@ -21,6 +22,7 @@ //! [`set_comparator_mode()`]: struct.Ads1x1x.html#method.set_comparator_mode //! [`set_comparator_polarity()`]: struct.Ads1x1x.html#method.set_comparator_polarity //! [`set_comparator_latching()`]: struct.Ads1x1x.html#method.set_comparator_latching +//! [`set_comparator_queue()`]: struct.Ads1x1x.html#method.set_comparator_queue //! [`disable_comparator()`]: struct.Ads1x1x.html#method.disable_comparator //! //! ## The devices @@ -220,6 +222,17 @@ pub enum ComparatorLatching { Latching } +/// Comparator alert queue (only for ADS1x14, ADS1x15) +#[derive(Debug, Clone, PartialEq)] +pub enum ComparatorQueue { + /// Activate comparator and assert after one conversion exceeding thresholds + One, + /// Activate comparator and assert after two consecutive conversions exceeding thresholds + Two, + /// Activate comparator and assert after four consecutive conversions exceeding thresholds + Four, +} + /// Possible slave addresses #[derive(Debug, Clone)] pub enum SlaveAddr { diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs index 75c4b5f..a4c4df3 100644 --- a/tests/tier2_i2c.rs +++ b/tests/tier2_i2c.rs @@ -1,7 +1,8 @@ extern crate embedded_hal_mock as hal; use hal::i2c::Transaction as I2cTrans; extern crate ads1x1x; -use ads1x1x::{ ComparatorMode, ComparatorPolarity, ComparatorLatching}; +use ads1x1x::{ ComparatorMode, ComparatorPolarity, ComparatorLatching, + ComparatorQueue }; #[macro_use] mod common; @@ -58,3 +59,11 @@ fn can_disable_comparator() { dev.disable_comparator().unwrap(); destroy_ads1014(dev); } + +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)); +} + |