diff options
author | Markus Reiter <me@reitermark.us> | 2024-01-19 15:09:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-19 15:09:19 +0100 |
commit | 5cd361340635aafe8806c82805622bc6afc4c4d7 (patch) | |
tree | e71f39a729a95020ca299925b93d0990416666b7 /src/devices/common.rs | |
parent | 6f417d833e2a4041f32a6578332b050d79028910 (diff) | |
download | ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.tar.gz ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.tar.xz ads1x1x-async-5cd361340635aafe8806c82805622bc6afc4c4d7.zip |
Remove `I2cInterface`. (#20)
* Merge `ReadData` and `WriteData` traits.
* Remove `I2cInterface`.
* Update changelog.
* Fix links.
Diffstat (limited to 'src/devices/common.rs')
-rw-r--r-- | src/devices/common.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/devices/common.rs b/src/devices/common.rs index f1509d3..5fed33f 100644 --- a/src/devices/common.rs +++ b/src/devices/common.rs @@ -1,17 +1,31 @@ //! Common functions -use crate::{devices::OperatingMode, interface, Ads1x1x, BitFlags, Config, Error, Register}; +use crate::{devices::OperatingMode, Ads1x1x, BitFlags, Config, Error, Register}; -impl<DI, IC, CONV, MODE, E> Ads1x1x<DI, IC, CONV, MODE> +impl<I2C, IC, CONV, MODE, E> Ads1x1x<I2C, IC, CONV, MODE> where - DI: interface::WriteData<Error = E> + interface::ReadData<Error = E>, + I2C: embedded_hal::i2c::I2c<Error = E>, { + pub(super) 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) + } + + pub(super) 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) + .map_err(Error::I2C) + .and(Ok(u16::from_be_bytes(data))) + } + pub(super) 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.iface.write_register(Register::CONFIG, config.bits)?; + self.write_register(Register::CONFIG, config.bits)?; self.config = config; Ok(()) } @@ -19,7 +33,7 @@ where /// Read whether a measurement is currently in progress. pub fn is_measurement_in_progress(&mut self) -> Result<bool, Error<E>> { let config = Config { - bits: self.iface.read_register(Register::CONFIG)?, + bits: self.read_register(Register::CONFIG)?, }; Ok(!config.is_high(BitFlags::OS)) } |