summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/devices/features/tier1.rs57
-rw-r--r--src/devices/features/tier2.rs57
-rw-r--r--tests/ads1x1x_i2c.rs16
-rw-r--r--tests/tier2_i2c.rs7
4 files changed, 63 insertions, 74 deletions
diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs
index 9f72b77..4bbf7d1 100644
--- a/src/devices/features/tier1.rs
+++ b/src/devices/features/tier1.rs
@@ -23,61 +23,4 @@ where
self.config = config;
Ok(())
}
-
- /// Set comparator lower threshold
- pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error<E>> {
- let register_value = convert_threshold::<IC, E>(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<E>> {
- let register_value = convert_threshold::<IC, E>(value)?;
- self.iface.write_register(Register::HIGH_TH, register_value)
- }
-}
-
-fn convert_threshold<IC, E>(value: i16) -> Result<u16, Error<E>>
-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<E>(result: Result<u16, Error<E>>) {
- match result {
- Err(Error::InvalidInputData) => (),
- _ => panic!("InvalidInputData error was not returned.")
- }
- }
-
- #[test]
- fn convert_12_bits() {
- assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(2048));
- assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(-2049));
-
- assert_eq!( 0, convert_threshold::<ic::Ads1013, ()>(0).unwrap());
- assert_eq!(0x7FF0, convert_threshold::<ic::Ads1013, ()>(2047).unwrap());
- assert_eq!(0x8000, convert_threshold::<ic::Ads1013, ()>(-2048).unwrap());
- assert_eq!(0xFFF0, convert_threshold::<ic::Ads1013, ()>(-1).unwrap());
- }
-
- #[test]
- fn convert_16_bits() {
- assert_eq!(0x7FFF, convert_threshold::<ic::Ads1113, ()>(32767).unwrap());
- assert_eq!(0x8000, convert_threshold::<ic::Ads1113,()>(-32768).unwrap());
- }
-}
-
diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs
index 43dce5c..2aa649f 100644
--- a/src/devices/features/tier2.rs
+++ b/src/devices/features/tier2.rs
@@ -10,6 +10,18 @@ where
DI: interface::WriteData<Error = E>,
IC: ic::Resolution + ic::Tier2Features
{
+ /// Set comparator lower threshold
+ pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error<E>> {
+ let register_value = convert_threshold::<IC, E>(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<E>> {
+ let register_value = convert_threshold::<IC, E>(value)?;
+ self.iface.write_register(Register::HIGH_TH, register_value)
+ }
+
/// Set comparator mode
pub fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> {
let config;
@@ -46,3 +58,48 @@ where
Ok(())
}
}
+
+
+fn convert_threshold<IC, E>(value: i16) -> Result<u16, Error<E>>
+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<E>(result: Result<u16, Error<E>>) {
+ match result {
+ Err(Error::InvalidInputData) => (),
+ _ => panic!("InvalidInputData error was not returned.")
+ }
+ }
+
+ #[test]
+ fn convert_12_bits() {
+ assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(2048));
+ assert_invalid_input_data(convert_threshold::<ic::Ads1013, ()>(-2049));
+
+ assert_eq!( 0, convert_threshold::<ic::Ads1013, ()>(0).unwrap());
+ assert_eq!(0x7FF0, convert_threshold::<ic::Ads1013, ()>(2047).unwrap());
+ assert_eq!(0x8000, convert_threshold::<ic::Ads1013, ()>(-2048).unwrap());
+ assert_eq!(0xFFF0, convert_threshold::<ic::Ads1013, ()>(-1).unwrap());
+ }
+
+ #[test]
+ fn convert_16_bits() {
+ assert_eq!(0x7FFF, convert_threshold::<ic::Ads1113, ()>(32767).unwrap());
+ assert_eq!(0x8000, convert_threshold::<ic::Ads1113,()>(-32768).unwrap());
+ }
+}
diff --git a/tests/ads1x1x_i2c.rs b/tests/ads1x1x_i2c.rs
index 2d8e7e2..e3dbf21 100644
--- a/tests/ads1x1x_i2c.rs
+++ b/tests/ads1x1x_i2c.rs
@@ -9,22 +9,6 @@ mod common;
use common::{ new_ads1013, destroy_ads1013,
DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config };
-#[test]
-fn can_set_low_threshold() {
- let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0x7F, 0xF0]) ];
- let mut dev = new_ads1013(&transactions);
- dev.set_low_threshold(2047).unwrap();
- destroy_ads1013(dev);
-}
-
-#[test]
-fn can_set_high_threshold() {
- let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0x7F, 0xF0]) ];
- let mut dev = new_ads1013(&transactions);
- dev.set_high_threshold(2047).unwrap();
- destroy_ads1013(dev);
-}
-
macro_rules! test_set_data_rate {
($name:ident, $variant:ident, $config:expr) => {
diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs
index b8b15fe..1b01791 100644
--- a/tests/tier2_i2c.rs
+++ b/tests/tier2_i2c.rs
@@ -9,7 +9,6 @@ mod common;
use common::{ new_ads1014, destroy_ads1014,
DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config };
-
macro_rules! set_value_test {
($name:ident, $method:ident, $value:expr, $reg:ident, $msb:expr, $lsb:expr) => {
#[test]
@@ -28,6 +27,12 @@ macro_rules! config_test {
}
}
+mod can_set_comparator_thresholds {
+ use super::*;
+ set_value_test!(low, set_low_threshold, 2047, LOW_TH, 0x7F, 0xF0);
+ set_value_test!(high, set_high_threshold, 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));