summaryrefslogtreecommitdiffstats
path: root/src/devices
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices')
-rw-r--r--src/devices/common.rs7
-rw-r--r--src/devices/features/tier1.rs7
-rw-r--r--src/devices/features/tier2.rs56
-rw-r--r--src/devices/mode/continuous.rs8
-rw-r--r--src/devices/mode/mod.rs43
-rw-r--r--src/devices/mode/oneshot.rs21
6 files changed, 27 insertions, 115 deletions
diff --git a/src/devices/common.rs b/src/devices/common.rs
index 5abb99c..1121265 100644
--- a/src/devices/common.rs
+++ b/src/devices/common.rs
@@ -1,12 +1,11 @@
//! Common functions
-use { Ads1x1x, Error, Register, BitFlags, interface, Config, ic };
+use { Ads1x1x, Error, Register, BitFlags, interface, Config };
use super::OperatingMode;
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
+impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE>
where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
+ DI: interface::WriteData<Error = E>
{
pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {
let config;
diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs
index 4bbf7d1..30e900e 100644
--- a/src/devices/features/tier1.rs
+++ b/src/devices/features/tier1.rs
@@ -1,11 +1,10 @@
//! Common functions
-use { Ads1x1x, DataRate, Error, Register, BitFlags, interface, ic };
+use { Ads1x1x, DataRate, Error, Register, BitFlags, interface };
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
+impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE>
where
- DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
+ DI: interface::WriteData<Error = E>
{
/// Set data rate
pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<E>> {
diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs
index 2aa649f..5d7ec92 100644
--- a/src/devices/features/tier2.rs
+++ b/src/devices/features/tier2.rs
@@ -3,22 +3,23 @@
//! These are the features included only in ADS1x14, ADS1x15
use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity,
- ComparatorLatching, Register, BitFlags };
+ ComparatorLatching, Register, BitFlags, conversion };
-impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
+impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE>
where
DI: interface::WriteData<Error = E>,
- IC: ic::Resolution + ic::Tier2Features
+ IC: ic::Tier2Features,
+ CONV: conversion::ConvertThreshold<E>
{
/// Set comparator lower threshold
pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error<E>> {
- let register_value = convert_threshold::<IC, E>(value)?;
+ let register_value = CONV::convert_threshold(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)?;
+ let register_value = CONV::convert_threshold(value)?;
self.iface.write_register(Register::HIGH_TH, register_value)
}
@@ -58,48 +59,3 @@ 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/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs
index 1472478..a06df60 100644
--- a/src/devices/mode/continuous.rs
+++ b/src/devices/mode/continuous.rs
@@ -1,21 +1,21 @@
//! Common functions
use core::marker::PhantomData;
-use { Ads1x1x, mode, Error, interface, ic };
+use { Ads1x1x, mode, Error, interface };
use super::super::OperatingMode;
-impl<DI, IC, E> Ads1x1x<DI, IC, mode::Continuous>
+impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous>
where
DI: interface::WriteData<Error = E>,
- IC: ic::Resolution
{
/// Change operating mode to OneShot
- pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, mode::OneShot>, Error<E>> {
+ pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, CONV, mode::OneShot>, Error<E>> {
self.set_operating_mode(OperatingMode::OneShot)?;
Ok(Ads1x1x {
iface: self.iface,
config: self.config,
a_conversion_was_started: self.a_conversion_was_started,
+ converter: self.converter,
_ic: PhantomData,
_mode: PhantomData
})
diff --git a/src/devices/mode/mod.rs b/src/devices/mode/mod.rs
index 8e0e012..9625465 100644
--- a/src/devices/mode/mod.rs
+++ b/src/devices/mode/mod.rs
@@ -1,47 +1,4 @@
//! Functions for all devices specific to each operating mode
-use ic;
mod oneshot;
mod continuous;
-
-fn convert_measurement<IC>(register_data: u16) -> i16
-where
- IC: ic::Resolution
-{
- let value = register_data;
- if IC::BITS == ic::ResolutionBits::_12 {
- let is_negative = (value & 0b1000_0000_0000_0000) != 0;
- if is_negative {
- let value = 0b1111_0000_0000_0000 | (value >> 4);
- value as i16
- }
- else {
- (value >> 4) as i16
- }
- }
- else {
- value as i16
- }
-}
-
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn convert_12_bits() {
- assert_eq!( 0, convert_measurement::<ic::Ads1013>(0));
- assert_eq!( 2047, convert_measurement::<ic::Ads1013>(0x7FFF));
- assert_eq!(-2048, convert_measurement::<ic::Ads1013>(0x8000));
- assert_eq!( -1, convert_measurement::<ic::Ads1013>(0xFFFF));
- }
-
- #[test]
- fn convert_16_bits() {
- assert_eq!( 0, convert_measurement::<ic::Ads1113>(0));
- assert_eq!( 32767, convert_measurement::<ic::Ads1113>(0x7FFF));
- assert_eq!(-32768, convert_measurement::<ic::Ads1113>(0x8000));
- assert_eq!( -1, convert_measurement::<ic::Ads1113>(0xFFFF));
- }
-}
diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs
index 57e72a0..e8aa533 100644
--- a/src/devices/mode/oneshot.rs
+++ b/src/devices/mode/oneshot.rs
@@ -1,24 +1,24 @@
//! Common functions
use core::marker::PhantomData;
-use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic };
-use { interface, hal, nb };
+use { Ads1x1x, mode, Error, Register, BitFlags, Config };
+use { interface, conversion, hal, nb };
use devices::OperatingMode;
use channels::ChannelSelection;
-use super::convert_measurement;
-impl<DI, IC, E> Ads1x1x<DI, IC, mode::OneShot>
+impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>,
- IC: ic::Resolution
+ CONV: conversion::ConvertMeasurement
{
/// Change operating mode to Continuous
- pub fn into_continuous(mut self) -> Result<Ads1x1x<DI, IC, mode::Continuous>, Error<E>> {
+ pub fn into_continuous(mut self) -> Result<Ads1x1x<DI, IC, CONV, mode::Continuous>, Error<E>> {
self.set_operating_mode(OperatingMode::Continuous)?;
Ok(Ads1x1x {
iface: self.iface,
config: self.config,
a_conversion_was_started: self.a_conversion_was_started,
+ converter: self.converter,
_ic: PhantomData,
_mode: PhantomData
})
@@ -37,11 +37,12 @@ where
}
}
-impl<DI, IC, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, mode::OneShot>, i16, CH> for Ads1x1x<DI, IC, mode::OneShot>
+impl<DI, IC, CONV, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, CONV, mode::OneShot>, i16, CH>
+ for Ads1x1x<DI, IC, CONV, mode::OneShot>
where
DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,
- IC: ic::Resolution,
- CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>, ID = ChannelSelection>
+ CONV: conversion::ConvertMeasurement,
+ CH: hal::adc::Channel<Ads1x1x<DI, IC, CONV, mode::OneShot>, ID = ChannelSelection>
{
type Error = Error<E>;
@@ -61,7 +62,7 @@ where
// result is ready
let value = self.iface.read_register(Register::CONVERSION).map_err(nb::Error::Other)?;
self.a_conversion_was_started = false;
- return Ok(convert_measurement::<IC>(value));
+ return Ok(CONV::convert_measurement(value));
}
let config = self.config.with_mux_bits(CH::channel());
self.trigger_measurement(&config).map_err(nb::Error::Other)?;