From b3ca672f70c71194da40a7670549a8264c13d971 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 12 Jan 2024 20:24:51 +0100 Subject: Update to `embedded-hal` 1.0. --- src/devices/common.rs | 9 +++--- src/devices/mode/continuous.rs | 13 ++++----- src/devices/mode/oneshot.rs | 62 +++++++++++++++++++++--------------------- 3 files changed, 40 insertions(+), 44 deletions(-) (limited to 'src/devices') diff --git a/src/devices/common.rs b/src/devices/common.rs index 2234d93..f1509d3 100644 --- a/src/devices/common.rs +++ b/src/devices/common.rs @@ -7,11 +7,10 @@ where DI: interface::WriteData + interface::ReadData, { pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error> { - let config; - match mode { - OperatingMode::OneShot => config = self.config.with_high(BitFlags::OP_MODE), - OperatingMode::Continuous => config = self.config.with_low(BitFlags::OP_MODE), - } + let config = match mode { + OperatingMode::OneShot => self.config.with_high(BitFlags::OP_MODE), + OperatingMode::Continuous => self.config.with_low(BitFlags::OP_MODE), + }; self.iface.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index 64aa179..b3b4807 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -1,11 +1,10 @@ //! Continuous measurement mode use crate::{ - channels::ChannelSelection, conversion, devices::OperatingMode, interface, mode, Ads1x1x, - Error, ModeChangeError, Register, + conversion, devices::OperatingMode, interface, mode, Ads1x1x, ChannelId, Error, + ModeChangeError, Register, }; use core::marker::PhantomData; -use embedded_hal::adc; impl Ads1x1x where @@ -41,11 +40,9 @@ where /// Note that when changing the channel in continuous conversion mode, the /// ongoing conversion will be completed. /// The following conversions will use the new channel configuration. - pub fn select_channel(&mut self, _channel: &mut CH) -> Result<(), Error> - where - CH: adc::Channel, ID = ChannelSelection>, - { - let config = self.config.with_mux_bits(CH::channel()); + #[allow(unused_variables)] + pub fn select_channel>(&mut self, channel: CH) -> Result<(), Error> { + let config = self.config.with_mux_bits(CH::channel_id()); self.iface.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) 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 Ads1x1x where @@ -35,14 +34,35 @@ where } } -impl adc::OneShot, i16, CH> - for Ads1x1x +impl Ads1x1x where DI: interface::ReadData + interface::WriteData, CONV: conversion::ConvertMeasurement, - CH: adc::Channel, ID = ChannelSelection>, { - type Error = Error; + fn read_inner(&mut self, channel: ChannelSelection) -> nb::Result> { + 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 { - ::read(self, CH::channel()) + #[allow(unused_variables)] + pub fn read>(&mut self, channel: CH) -> nb::Result> { + self.read_inner(CH::channel_id()) } } @@ -70,27 +91,6 @@ where type Error = Error; fn read(&mut self, channel: ChannelSelection) -> nb::Result { - 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) } } -- cgit v1.2.3-54-g00ecf