From cd403a91e6078956445aeb21d6509e863b0592ae Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sun, 8 Dec 2024 12:01:31 +0000 Subject: Modify to use async A changeover from embedded_hal::i2c::I2c to embedded_hal_async::i2c::I2c including changes to all the relevant functions into async functions. Tests have been updated to work using futures-test and embedded-hal-mock with the embedded-hal-async feature. Examples have been kept the same meaning they no longer compile. Currently it doesn't _seem_ like the linux embedded hal can do async i2c so maybe these should be re-written to use embassy? --- src/devices/mode/oneshot.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/devices/mode/oneshot.rs') diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 22b6d51..ba78c84 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -9,14 +9,14 @@ use crate::{ impl Ads1x1x where - I2C: embedded_hal::i2c::I2c, + I2C: embedded_hal_async::i2c::I2c, CONV: conversion::ConvertMeasurement, { /// Changes to continuous operating mode. - pub fn into_continuous( + pub async fn into_continuous( mut self, ) -> Result, ModeChangeError> { - if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::Continuous) { + if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::Continuous).await { return Err(ModeChangeError::I2C(e, self)); } Ok(Ads1x1x { @@ -31,15 +31,15 @@ where }) } - fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error> { + async fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error> { let config = config.with_high(BitFlags::OS); - self.write_register(Register::CONFIG, config.bits) + self.write_register(Register::CONFIG, config.bits).await } } impl Ads1x1x where - I2C: embedded_hal::i2c::I2c, + I2C: embedded_hal_async::i2c::I2c, CONV: conversion::ConvertMeasurement, { /// Requests that the ADC begins a conversion on the specified channel. @@ -55,9 +55,10 @@ where /// measurement on a different channel is requested, a new measurement on /// using the new channel selection is triggered. #[allow(unused_variables)] - pub fn read>(&mut self, channel: CH) -> nb::Result> { + pub async fn read>(&mut self, channel: CH) -> nb::Result> { if self .is_measurement_in_progress() + .await .map_err(nb::Error::Other)? { return Err(nb::Error::WouldBlock); @@ -68,11 +69,13 @@ where // result is ready let value = self .read_register(Register::CONVERSION) + .await .map_err(nb::Error::Other)?; self.a_conversion_was_started = false; return Ok(CONV::convert_measurement(value)); } self.trigger_measurement(&config) + .await .map_err(nb::Error::Other)?; self.config = config; self.a_conversion_was_started = true; -- cgit v1.2.3-54-g00ecf