diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 12:01:31 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2024-12-08 12:01:31 +0000 |
commit | cd403a91e6078956445aeb21d6509e863b0592ae (patch) | |
tree | b1bb59ed526081be31494544f8920c39f0588711 /src/devices/mode | |
parent | 0edd5527161809dfbc0c76e39c462e3a4f00beb7 (diff) | |
download | ads1x1x-async-cd403a91e6078956445aeb21d6509e863b0592ae.tar.gz ads1x1x-async-cd403a91e6078956445aeb21d6509e863b0592ae.tar.xz ads1x1x-async-cd403a91e6078956445aeb21d6509e863b0592ae.zip |
Modify to use asyncasync
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?
Diffstat (limited to 'src/devices/mode')
-rw-r--r-- | src/devices/mode/continuous.rs | 17 | ||||
-rw-r--r-- | src/devices/mode/oneshot.rs | 17 |
2 files changed, 20 insertions, 14 deletions
diff --git a/src/devices/mode/continuous.rs b/src/devices/mode/continuous.rs index d514a9a..a7a94b3 100644 --- a/src/devices/mode/continuous.rs +++ b/src/devices/mode/continuous.rs @@ -7,14 +7,14 @@ use core::marker::PhantomData; impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::Continuous> where - I2C: embedded_hal::i2c::I2c<Error = E>, + I2C: embedded_hal_async::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { /// Changes to one-shot operating mode. - pub fn into_one_shot( + pub async fn into_one_shot( mut self, ) -> Result<Ads1x1x<I2C, IC, CONV, mode::OneShot>, ModeChangeError<E, Self>> { - if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::OneShot) { + if let Err(Error::I2C(e)) = self.set_operating_mode(OperatingMode::OneShot).await { return Err(ModeChangeError::I2C(e, self)); } Ok(Ads1x1x { @@ -30,8 +30,8 @@ where } /// Reads the most recent measurement. - pub fn read(&mut self) -> Result<i16, Error<E>> { - let value = self.read_register(Register::CONVERSION)?; + pub async fn read(&mut self) -> Result<i16, Error<E>> { + let value = self.read_register(Register::CONVERSION).await?; Ok(CONV::convert_measurement(value)) } @@ -41,9 +41,12 @@ where /// ongoing conversion will be completed. /// The following conversions will use the new channel configuration. #[allow(unused_variables)] - pub fn select_channel<CH: ChannelId<Self>>(&mut self, channel: CH) -> Result<(), Error<E>> { + pub async fn select_channel<CH: ChannelId<Self>>( + &mut self, + channel: CH, + ) -> Result<(), Error<E>> { let config = self.config.with_mux_bits(CH::channel_id()); - self.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits).await?; self.config = config; Ok(()) } 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<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::OneShot> where - I2C: embedded_hal::i2c::I2c<Error = E>, + I2C: embedded_hal_async::i2c::I2c<Error = E>, CONV: conversion::ConvertMeasurement, { /// Changes to continuous operating mode. - pub fn into_continuous( + pub async fn into_continuous( mut self, ) -> Result<Ads1x1x<I2C, IC, CONV, mode::Continuous>, ModeChangeError<E, Self>> { - 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<E>> { + async fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> { let config = config.with_high(BitFlags::OS); - self.write_register(Register::CONFIG, config.bits) + self.write_register(Register::CONFIG, config.bits).await } } impl<I2C, IC, CONV, E> Ads1x1x<I2C, IC, CONV, mode::OneShot> where - I2C: embedded_hal::i2c::I2c<Error = E>, + I2C: embedded_hal_async::i2c::I2c<Error = E>, 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<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> { + pub async fn read<CH: ChannelId<Self>>(&mut self, channel: CH) -> nb::Result<i16, Error<E>> { 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; |