From fd6fe2ceada168e33a01cbdf9503f180cb30c58b Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Tue, 20 Nov 2018 19:21:50 +0100 Subject: Add function to read last measurement in continuous mode --- src/devices/mode/continuous.rs | 14 +++++++-- src/lib.rs | 4 ++- 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 Ads1x1x where - DI: interface::WriteData, + DI: interface::ReadData + interface::WriteData, + CONV: conversion::ConvertMeasurement, { /// Change operating mode to OneShot pub fn into_one_shot(mut self) -> Result, Error> { @@ -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> { + 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 Ads1x1x diff --git a/src/lib.rs b/src/lib.rs index 4382e61..b825546 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,7 +203,9 @@ pub enum Error { /// 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(result: Result>) { + 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); + } } -- cgit v1.2.3-54-g00ecf