From 28126fee8ce253e67ab737927ff0d89ebc24e419 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Thu, 29 Jul 2021 23:14:25 +0200 Subject: Add value-based one-shot trait to ease driver usage in functions --- src/devices/mode/oneshot.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/devices') diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 665ce33..fa4d3b8 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -1,7 +1,7 @@ //! Common functions use crate::{ - channels::ChannelSelection, conversion, devices::OperatingMode, interface, mode, Ads1x1x, - BitFlags, Config, Error, ModeChangeError, Register, + conversion, devices::OperatingMode, interface, mode, Ads1x1x, BitFlags, ChannelSelection, + Config, DynamicOneShot, Error, ModeChangeError, Register, }; use core::marker::PhantomData; use embedded_hal::adc; @@ -58,13 +58,25 @@ where /// 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()) + } +} + +impl DynamicOneShot for Ads1x1x +where + DI: interface::ReadData + interface::WriteData, + CONV: conversion::ConvertMeasurement, +{ + 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(CH::channel()); + 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 @@ -74,7 +86,7 @@ where self.a_conversion_was_started = false; return Ok(CONV::convert_measurement(value)); } - let config = self.config.with_mux_bits(CH::channel()); + let config = self.config.with_mux_bits(channel); self.trigger_measurement(&config) .map_err(nb::Error::Other)?; self.config = config; -- cgit v1.2.3-54-g00ecf