diff options
Diffstat (limited to 'src/devices/mode/oneshot.rs')
-rw-r--r-- | src/devices/mode/oneshot.rs | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index fa4d3b8..0663e4b 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,10 +1,9 @@ //! Common functions use crate::{ - conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelSelection, - Config, DynamicOneShot, Error, ModeChangeError, Register, + conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelId, + ChannelSelection, Config, DynamicOneShot, Error, ModeChangeError, Register, }; use core::marker::PhantomData; -use embedded_hal::adc; impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> where @@ -35,14 +34,35 @@ where } } -impl<DI, IC, CONV, E, CH> adc::OneShot<Ads1x1x<DI, IC, CONV, mode::OneShot>, i16, CH> - for Ads1x1x<DI, IC, CONV, mode::OneShot> +impl<DI, IC, CONV, E> Ads1x1x<DI, IC, CONV, mode::OneShot> where DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>, CONV: conversion::ConvertMeasurement, - CH: adc::Channel<Ads1x1x<DI, IC, CONV, mode::OneShot>, ID = ChannelSelection>, { - type Error = Error<E>; + fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result<i16, Error<E>> { + if self + .is_measurement_in_progress() + .map_err(nb::Error::Other)? + { + return Err(nb::Error::WouldBlock); + } + let config = self.config.with_mux_bits(channel); + let same_channel = self.config == config; + if self.a_conversion_was_started && same_channel { + // result is ready + let value = self + .iface + .read_register(Register::CONVERSION) + .map_err(nb::Error::Other)?; + self.a_conversion_was_started = false; + return Ok(CONV::convert_measurement(value)); + } + self.trigger_measurement(&config) + .map_err(nb::Error::Other)?; + self.config = config; + self.a_conversion_was_started = true; + Err(nb::Error::WouldBlock) + } /// Request that the ADC begin a conversion on the specified channel. /// @@ -57,8 +77,9 @@ where /// In case a measurement was requested and after is it is finished a /// measurement on a different channel is requested, a new measurement on /// using the new channel selection is triggered. - fn read(&mut self, _channel: &mut CH) -> nb::Result<i16, Self::Error> { - <Self as DynamicOneShot>::read(self, CH::channel()) + #[allow(unused_variables)] + pub fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> { + self.read_inner(CH::channel_id()) } } @@ -70,27 +91,6 @@ where type Error = Error<E>; fn read(&mut self, channel: ChannelSelection) -> nb::Result<i16, Self::Error> { - if self - .is_measurement_in_progress() - .map_err(nb::Error::Other)? - { - return Err(nb::Error::WouldBlock); - } - let same_channel = self.config == self.config.with_mux_bits(channel); - if self.a_conversion_was_started && same_channel { - // result is ready - let value = self - .iface - .read_register(Register::CONVERSION) - .map_err(nb::Error::Other)?; - self.a_conversion_was_started = false; - return Ok(CONV::convert_measurement(value)); - } - let config = self.config.with_mux_bits(channel); - self.trigger_measurement(&config) - .map_err(nb::Error::Other)?; - self.config = config; - self.a_conversion_was_started = true; - Err(nb::Error::WouldBlock) + self.read_inner(channel) } } |