summaryrefslogtreecommitdiffstats
path: root/src/devices
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2018-11-08 18:56:15 +0100
committerDiego Barrios Romero <eldruin@gmail.com>2018-11-08 18:56:15 +0100
commit2f4284b914fb187ae485918b3f64c0ff2360ddb1 (patch)
tree353d282a81277e2bfb0a89b9babc02122cde1b55 /src/devices
parent4905c13ae1e77cad1a325d66000de930c1e4abbb (diff)
downloadads1x1x-async-2f4284b914fb187ae485918b3f64c0ff2360ddb1.tar.gz
ads1x1x-async-2f4284b914fb187ae485918b3f64c0ff2360ddb1.tar.xz
ads1x1x-async-2f4284b914fb187ae485918b3f64c0ff2360ddb1.zip
Split common functions into tier1 features module
Diffstat (limited to 'src/devices')
-rw-r--r--src/devices/ads1x1x/common.rs33
-rw-r--r--src/devices/ads1x1x/features/mod.rs5
-rw-r--r--src/devices/ads1x1x/features/tier1.rs83
-rw-r--r--src/devices/ads1x1x/mod.rs1
4 files changed, 105 insertions, 17 deletions
diff --git a/src/devices/ads1x1x/common.rs b/src/devices/ads1x1x/common.rs
index ce8a67a..5abb99c 100644
--- a/src/devices/ads1x1x/common.rs
+++ b/src/devices/ads1x1x/common.rs
@@ -1,11 +1,12 @@
//! Common functions
-use { Ads1x1x, DataRate, Error, Register, BitFlags, interface };
+use { Ads1x1x, Error, Register, BitFlags, interface, Config, ic };
use super::OperatingMode;
impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
where
- DI: interface::WriteData<Error = E>
+ DI: interface::WriteData<Error = E>,
+ IC: ic::Resolution
{
pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {
let config;
@@ -18,20 +19,18 @@ where
Ok(())
}
- /// Set data rate
- pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<E>> {
- let config;
- match rate {
- DataRate::Sps128 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps250 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps490 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps920 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps1600 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
- DataRate::Sps2400 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
- DataRate::Sps3300 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
- }
- self.iface.write_register(Register::CONFIG, config.bits)?;
- self.config = config;
- Ok(())
+ /// Reset the internal state of this driver to the default values.
+ ///
+ /// *Note:* This does not alter the state or configuration of the device.
+ ///
+ /// This resets the cached configuration register value in this driver to
+ /// the power-up (reset) configuration of the device.
+ ///
+ /// This needs to be called after performing a reset on the device, for
+ /// example through an I2C general-call Reset command, which was not done
+ /// through this driver to ensure that the configurations in the device
+ /// and in the driver match.
+ pub fn reset_internal_driver_state(&mut self) {
+ self.config = Config::default();
}
}
diff --git a/src/devices/ads1x1x/features/mod.rs b/src/devices/ads1x1x/features/mod.rs
new file mode 100644
index 0000000..ad26258
--- /dev/null
+++ b/src/devices/ads1x1x/features/mod.rs
@@ -0,0 +1,5 @@
+//! Implementation of IC features separated in tiers depending on the hardware
+//! support.
+
+mod tier1;
+
diff --git a/src/devices/ads1x1x/features/tier1.rs b/src/devices/ads1x1x/features/tier1.rs
new file mode 100644
index 0000000..9f72b77
--- /dev/null
+++ b/src/devices/ads1x1x/features/tier1.rs
@@ -0,0 +1,83 @@
+//! Common functions
+
+use { Ads1x1x, DataRate, Error, Register, BitFlags, interface, ic };
+
+impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
+where
+ DI: interface::WriteData<Error = E>,
+ IC: ic::Resolution
+{
+ /// Set data rate
+ pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<E>> {
+ let config;
+ match rate {
+ DataRate::Sps128 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
+ DataRate::Sps250 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
+ DataRate::Sps490 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
+ DataRate::Sps920 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0),
+ DataRate::Sps1600 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0),
+ DataRate::Sps2400 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0),
+ DataRate::Sps3300 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0),
+ }
+ self.iface.write_register(Register::CONFIG, config.bits)?;
+ 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/ads1x1x/mod.rs b/src/devices/ads1x1x/mod.rs
index b7146a9..95d8907 100644
--- a/src/devices/ads1x1x/mod.rs
+++ b/src/devices/ads1x1x/mod.rs
@@ -7,3 +7,4 @@ enum OperatingMode {
mod common;
mod mode;
+mod features;