summaryrefslogtreecommitdiffstats
path: root/src/devices/common.rs
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2024-12-08 12:01:31 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2024-12-08 12:01:31 +0000
commitcd403a91e6078956445aeb21d6509e863b0592ae (patch)
treeb1bb59ed526081be31494544f8920c39f0588711 /src/devices/common.rs
parent0edd5527161809dfbc0c76e39c462e3a4f00beb7 (diff)
downloadads1x1x-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/common.rs')
-rw-r--r--src/devices/common.rs20
1 files changed, 12 insertions, 8 deletions
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))
}