From 483221e740ffaebe406ee428beb59fa136d70c7f Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 7 Nov 2018 20:22:59 +0100 Subject: Add support for one-shot measurements --- src/devices/ads1x1x/mode/oneshot.rs | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/devices/ads1x1x/mode/oneshot.rs (limited to 'src/devices/ads1x1x/mode/oneshot.rs') diff --git a/src/devices/ads1x1x/mode/oneshot.rs b/src/devices/ads1x1x/mode/oneshot.rs new file mode 100644 index 0000000..61d4e70 --- /dev/null +++ b/src/devices/ads1x1x/mode/oneshot.rs @@ -0,0 +1,61 @@ +//! Common functions + +use core::marker::PhantomData; +use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic }; +use { interface, hal }; +use super::super::OperatingMode; +use super::convert_measurement; + +impl Ads1x1x +where + DI: interface::WriteData + interface::ReadData +{ + /// Change operating mode to Continuous + pub fn into_continuous(mut self) -> Result, Error> { + self.set_operating_mode(OperatingMode::Continuous)?; + Ok(Ads1x1x { + iface: self.iface, + config: self.config, + a_conversion_was_started: self.a_conversion_was_started, + _ic: PhantomData, + _mode: PhantomData + }) + } + + fn is_measurement_in_progress(&mut self) -> Result> { + let config = Config { + bits: self.iface.read_register(Register::CONFIG)? + }; + Ok(!config.is_high(BitFlags::OS)) + } + + fn trigger_measurement(&mut self) -> Result<(), Error> { + let config = self.config.with_high(BitFlags::OS); + self.iface.write_register(Register::CONFIG, config.bits) + } +} + +impl hal::adc::OneShot, i16, CH> for Ads1x1x +where + DI: interface::ReadData + interface::WriteData, + IC: ic::Resolution, + CH: hal::adc::Channel> +{ + type Error = Error; + + fn read(&mut self, _channel: &mut CH) -> nb::Result { + //TODO for devices with MUX select channel, if it is the wrong one, return AlreadyInProgress or WrongChannel error + if self.is_measurement_in_progress().map_err(nb::Error::Other)? { + return Err(nb::Error::WouldBlock); + } + if self.a_conversion_was_started { + // 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::(value)); + } + self.trigger_measurement().map_err(nb::Error::Other)?; + self.a_conversion_was_started = true; + Err(nb::Error::WouldBlock) + } +} -- cgit v1.2.3-54-g00ecf