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 | |
| parent | 0edd5527161809dfbc0c76e39c462e3a4f00beb7 (diff) | |
| download | ads1x1x-async-async.tar.gz ads1x1x-async-async.tar.xz ads1x1x-async-async.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')
| -rw-r--r-- | src/construction.rs | 2 | ||||
| -rw-r--r-- | src/devices/common.rs | 20 | ||||
| -rw-r--r-- | src/devices/features/tier1.rs | 12 | ||||
| -rw-r--r-- | src/devices/features/tier2.rs | 43 | ||||
| -rw-r--r-- | src/devices/mode/continuous.rs | 17 | ||||
| -rw-r--r-- | src/devices/mode/oneshot.rs | 17 | 
6 files changed, 61 insertions, 50 deletions
| diff --git a/src/construction.rs b/src/construction.rs index 84ee8ad..cd9d779 100644 --- a/src/construction.rs +++ b/src/construction.rs @@ -7,7 +7,7 @@ macro_rules! impl_new_destroy {      ( $IC:ident, $create:ident, $destroy:ident, $conv:ty ) => {          impl<I2C, E> Ads1x1x<I2C, ic::$IC, $conv, mode::OneShot>          where -            I2C: embedded_hal::i2c::I2c<Error = E>, +            I2C: embedded_hal_async::i2c::I2c<Error = E>,          {              /// Create a new instance of the device in OneShot mode.              pub fn $create(i2c: I2C, address: TargetAddr) -> Self { diff --git a/src/devices/common.rs b/src/devices/common.rs index 66a6d97..865d0c5 100644 --- a/src/devices/common.rs +++ b/src/devices/common.rs @@ -4,36 +4,40 @@ use crate::{devices::OperatingMode, Ads1x1x, BitFlags, Config, Error, Register};  impl<I2C, IC, CONV, MODE, E> Ads1x1x<I2C, IC, CONV, MODE>  where -    I2C: embedded_hal::i2c::I2c<Error = E>, +    I2C: embedded_hal_async::i2c::I2c<Error = E>,  { -    pub(super) fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { +    pub(super) async fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> {          let data = data.to_be_bytes();          let payload: [u8; 3] = [register, data[0], data[1]]; -        self.i2c.write(self.address, &payload).map_err(Error::I2C) +        self.i2c +            .write(self.address, &payload) +            .await +            .map_err(Error::I2C)      } -    pub(super) fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> { +    pub(super) async fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> {          let mut data = [0, 0];          self.i2c              .write_read(self.address, &[register], &mut data) +            .await              .map_err(Error::I2C)              .and(Ok(u16::from_be_bytes(data)))      } -    pub(super) fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> { +    pub(super) async fn set_operating_mode(&mut self, mode: OperatingMode) -> Result<(), Error<E>> {          let config = match mode {              OperatingMode::OneShot => self.config.with_high(BitFlags::OP_MODE),              OperatingMode::Continuous => self.config.with_low(BitFlags::OP_MODE),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      }      /// Checks whether a measurement is currently in progress. -    pub fn is_measurement_in_progress(&mut self) -> Result<bool, Error<E>> { +    pub async fn is_measurement_in_progress(&mut self) -> Result<bool, Error<E>> {          let config = Config { -            bits: self.read_register(Register::CONFIG)?, +            bits: self.read_register(Register::CONFIG).await?,          };          Ok(!config.is_high(BitFlags::OS))      } diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs index 3a8beb9..17da990 100644 --- a/src/devices/features/tier1.rs +++ b/src/devices/features/tier1.rs @@ -4,10 +4,10 @@ use crate::{ic, Ads1x1x, BitFlags as BF, DataRate12Bit, DataRate16Bit, Error, Re  impl<I2C, IC, MODE, E> Ads1x1x<I2C, IC, ic::Resolution12Bit, MODE>  where -    I2C: embedded_hal::i2c::I2c<Error = E>, +    I2C: embedded_hal_async::i2c::I2c<Error = E>,  {      /// Sets the data rate. -    pub fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error<E>> { +    pub async fn set_data_rate(&mut self, rate: DataRate12Bit) -> Result<(), Error<E>> {          use crate::DataRate12Bit as DR;          let cfg = self.config.clone();          let config = match rate { @@ -19,7 +19,7 @@ where              DR::Sps2400 => cfg.with_high(BF::DR2).with_low(BF::DR1).with_high(BF::DR0),              DR::Sps3300 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } @@ -27,10 +27,10 @@ where  impl<I2C, IC, MODE, E> Ads1x1x<I2C, IC, ic::Resolution16Bit, MODE>  where -    I2C: embedded_hal::i2c::I2c<Error = E>, +    I2C: embedded_hal_async::i2c::I2c<Error = E>,  {      /// Sets the data rate. -    pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error<E>> { +    pub async fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error<E>> {          use crate::DataRate16Bit as DR;          let cfg = self.config.clone();          let config = match rate { @@ -43,7 +43,7 @@ where              DR::Sps475 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_low(BF::DR0),              DR::Sps860 => cfg.with_high(BF::DR2).with_high(BF::DR1).with_high(BF::DR0),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index fe36a72..8a9a3cc 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -7,14 +7,14 @@ use crate::{  impl<I2C, IC, CONV, MODE, E> Ads1x1x<I2C, IC, CONV, MODE>  where -    I2C: embedded_hal::i2c::I2c<Error = E>, +    I2C: embedded_hal_async::i2c::I2c<Error = E>,      IC: ic::Tier2Features,      CONV: conversion::ConvertThreshold<E>,  {      /// Sets the input voltage measurable range.      ///      /// This configures the programmable gain amplifier (PGA) and determines the measurable input voltage range. -    pub fn set_full_scale_range(&mut self, range: FullScaleRange) -> Result<(), Error<E>> { +    pub async fn set_full_scale_range(&mut self, range: FullScaleRange) -> Result<(), Error<E>> {          use crate::FullScaleRange as FSR;          let cfg = self.config.clone();          let config = match range { @@ -40,7 +40,7 @@ where                  .with_low(BF::PGA1)                  .with_high(BF::PGA0),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } @@ -52,9 +52,9 @@ where      ///      /// The input value must be within `[2047..-2048]` for 12-bit devices (`ADS101x`)      /// and within `[32767..-32768]` for 16-bit devices (`ADS111x`). -    pub fn set_low_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> { +    pub async fn set_low_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> {          let register_value = CONV::convert_threshold(value)?; -        self.write_register(Register::LOW_TH, register_value) +        self.write_register(Register::LOW_TH, register_value).await      }      /// Sets the raw comparator upper threshold. @@ -64,24 +64,24 @@ where      ///      /// The input value must be within `[2047..-2048]` for 12-bit devices (`ADS101x`)      /// and within `[32767..-32768]` for 16-bit devices (`ADS111x`). -    pub fn set_high_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> { +    pub async fn set_high_threshold_raw(&mut self, value: i16) -> Result<(), Error<E>> {          let register_value = CONV::convert_threshold(value)?; -        self.write_register(Register::HIGH_TH, register_value) +        self.write_register(Register::HIGH_TH, register_value).await      }      /// Sets the comparator mode. -    pub fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> { +    pub async fn set_comparator_mode(&mut self, mode: ComparatorMode) -> Result<(), Error<E>> {          let config = match mode {              ComparatorMode::Traditional => self.config.with_low(BF::COMP_MODE),              ComparatorMode::Window => self.config.with_high(BF::COMP_MODE),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      }      /// Sets the comparator polarity. -    pub fn set_comparator_polarity( +    pub async fn set_comparator_polarity(          &mut self,          polarity: ComparatorPolarity,      ) -> Result<(), Error<E>> { @@ -89,13 +89,13 @@ where              ComparatorPolarity::ActiveLow => self.config.with_low(BF::COMP_POL),              ComparatorPolarity::ActiveHigh => self.config.with_high(BF::COMP_POL),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      }      /// Sets the comparator latching. -    pub fn set_comparator_latching( +    pub async fn set_comparator_latching(          &mut self,          latching: ComparatorLatching,      ) -> Result<(), Error<E>> { @@ -103,7 +103,7 @@ where              ComparatorLatching::Nonlatching => self.config.with_low(BF::COMP_LAT),              ComparatorLatching::Latching => self.config.with_high(BF::COMP_LAT),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } @@ -111,13 +111,13 @@ where      /// Activates the comparator and sets the alert queue.      ///      /// The comparator can be disabled with [`disable_comparator`](Self::disable_comparator). -    pub fn set_comparator_queue(&mut self, queue: ComparatorQueue) -> Result<(), Error<E>> { +    pub async fn set_comparator_queue(&mut self, queue: ComparatorQueue) -> Result<(), Error<E>> {          let config = match queue {              ComparatorQueue::One => self.config.with_low(BF::COMP_QUE1).with_low(BF::COMP_QUE0),              ComparatorQueue::Two => self.config.with_low(BF::COMP_QUE1).with_high(BF::COMP_QUE0),              ComparatorQueue::Four => self.config.with_high(BF::COMP_QUE1).with_low(BF::COMP_QUE0),          }; -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } @@ -128,12 +128,12 @@ where      ///      /// The comparator can be enabled by setting the comparator queue using      /// the [`set_comparator_queue`](Self::set_comparator_queue) method. -    pub fn disable_comparator(&mut self) -> Result<(), Error<E>> { +    pub async fn disable_comparator(&mut self) -> Result<(), Error<E>> {          let config = self              .config              .with_high(BF::COMP_QUE1)              .with_high(BF::COMP_QUE0); -        self.write_register(Register::CONFIG, config.bits)?; +        self.write_register(Register::CONFIG, config.bits).await?;          self.config = config;          Ok(())      } @@ -144,16 +144,17 @@ where      /// in continuous-conversion mode, provides a continuous-conversion ready pulse.      ///      /// When calling this the comparator will be reset to default and any thresholds will be cleared. -    pub fn use_alert_rdy_pin_as_ready(&mut self) -> Result<(), Error<E>> { +    pub async fn use_alert_rdy_pin_as_ready(&mut self) -> Result<(), Error<E>> {          if self.config              != self                  .config                  .with_high(BF::COMP_QUE1)                  .with_high(BF::COMP_QUE0)          { -            self.set_comparator_queue(ComparatorQueue::default())?; +            self.set_comparator_queue(ComparatorQueue::default()) +                .await?;          } -        self.write_register(Register::HIGH_TH, 0x8000)?; -        self.write_register(Register::LOW_TH, 0) +        self.write_register(Register::HIGH_TH, 0x8000).await?; +        self.write_register(Register::LOW_TH, 0).await      }  } 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; | 
