diff options
-rw-r--r-- | src/devices/mode/continuous.rs | 14 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | tests/tier1_i2c.rs | 65 |
3 files changed, 68 insertions, 15 deletions
diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index cd3edee..7998a46 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -7,7 +7,8 @@ use super::super::OperatingMode; impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous> where - DI: interface::WriteData<Error = E>, + DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, + CONV: conversion::ConvertMeasurement, { /// Change operating mode to OneShot pub fn into_one_shot(mut self) -> Result<Ads1x1x<DI, IC, CONV, mode::OneShot>, Error<E>> { @@ -16,7 +17,7 @@ where iface: self.iface, config: self.config, fsr: self.fsr, - a_conversion_was_started: self.a_conversion_was_started, + a_conversion_was_started: false, _conv: PhantomData, _ic: PhantomData, _mode: PhantomData @@ -29,6 +30,15 @@ where self.a_conversion_was_started = true; Ok(()) } + + /// Read the most recent measurement + pub fn read(&mut self) -> Result<i16, Error<E>> { + if !self.a_conversion_was_started { + return Err(Error::NotStarted); + } + let value = self.iface.read_register(Register::CONVERSION)?; + return Ok(CONV::convert_measurement(value)); + } } impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::Continuous> @@ -203,7 +203,9 @@ pub enum Error<E> { /// I²C/SPI bus error Comm(E), /// Invalid input data provided - InvalidInputData + InvalidInputData, + /// Continuous measurement was not started + NotStarted, } const DEVICE_BASE_ADDRESS : u8 = 0b100_1000; diff --git a/tests/tier1_i2c.rs b/tests/tier1_i2c.rs index 9437487..b26312f 100644 --- a/tests/tier1_i2c.rs +++ b/tests/tier1_i2c.rs @@ -4,14 +4,14 @@ extern crate embedded_hal; extern crate embedded_hal_mock as hal; use hal::i2c::Transaction as I2cTrans; extern crate ads1x1x; -use ads1x1x::{ channel, DataRate12Bit, DataRate16Bit }; +use ads1x1x::{ channel, DataRate12Bit, DataRate16Bit, Error }; #[macro_use] mod common; use common::{ new_ads1013, destroy_ads1013, new_ads1113, destroy_ads1113, DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags, Config }; -macro_rules! impl_tests { +macro_rules! measure_tests { ($IC:ident, $create:ident, $destroy:ident, $expected:expr) => { mod $IC { use embedded_hal::adc::OneShot; @@ -43,12 +43,25 @@ macro_rules! impl_tests { assert_eq!($expected, measurement); $destroy(dev); } + + #[test] + fn can_measure_continuous() { + let config = Config::default().with_low(BitFlags::OP_MODE); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let dev = $create(&transactions); + let mut dev = dev.into_continuous().unwrap(); + dev.start().unwrap(); + let measurement = dev.read().unwrap(); + assert_eq!($expected, measurement); + $destroy(dev); + } } } } -impl_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); -impl_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); +measure_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); +measure_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); mod data_rate_12bit { @@ -115,12 +128,40 @@ fn can_convert_to_one_shot() { destroy_ads1013(dev); } -#[test] -fn can_start_in_continuous() { - let config = Config::default().with_low(BitFlags::OP_MODE); - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; - let dev = new_ads1013(&transactions); - let mut dev = dev.into_continuous().unwrap(); - dev.start().unwrap(); - destroy_ads1013(dev); +mod continuous { + use super::*; + #[test] + fn can_start() { + let config = Config::default().with_low(BitFlags::OP_MODE); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]) ]; + let dev = new_ads1013(&transactions); + let mut dev = dev.into_continuous().unwrap(); + dev.start().unwrap(); + destroy_ads1013(dev); + } + + fn assert_not_started<T, E>(result: Result<T, Error<E>>) { + match result { + Err(Error::NotStarted) => (), + _ => panic!("Error::NotStarted not returned.") + } + } + #[test] + fn check_assert_matches() { + assert_not_started::<(), ()>(Err(Error::NotStarted)); + } + + #[test] + #[should_panic] + fn check_assert_fails() { + assert_not_started::<(), ()>(Ok(())); + } + + #[test] + fn cannot_read_if_not_started() { + let dev = new_ads1013(&[]); + let mut dev = dev.into_continuous().unwrap(); + assert_not_started(dev.read()); + destroy_ads1013(dev); + } } |