From 5cd361340635aafe8806c82805622bc6afc4c4d7 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 19 Jan 2024 15:09:19 +0100 Subject: Remove `I2cInterface`. (#20) * Merge `ReadData` and `WriteData` traits. * Remove `I2cInterface`. * Update changelog. * Fix links. --- src/devices/common.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/devices/common.rs') 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 Ads1x1x +impl Ads1x1x where - DI: interface::WriteData + interface::ReadData, + I2C: embedded_hal::i2c::I2c, { + pub(super) fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error> { + 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> { + 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> { 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> { let config = Config { - bits: self.iface.read_register(Register::CONFIG)?, + bits: self.read_register(Register::CONFIG)?, }; Ok(!config.is_high(BitFlags::OS)) } -- cgit v1.2.3-54-g00ecf