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/interface.rs | 53 ----------------------------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 src/interface.rs (limited to 'src/interface.rs') diff --git a/src/interface.rs b/src/interface.rs deleted file mode 100644 index 0e665a3..0000000 --- a/src/interface.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! I2C interface - -use crate::{private, Error}; - -/// I2C interface -#[derive(Debug, Default)] -pub struct I2cInterface { - pub(crate) i2c: I2C, - pub(crate) address: u8, -} - -/// Write data -pub trait WriteData: private::Sealed { - /// Error type - type Error; - - /// Write to an u16 register - fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error>; -} - -impl WriteData for I2cInterface -where - I2C: embedded_hal::i2c::I2c, -{ - type Error = E; - fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error> { - let payload: [u8; 3] = [register, (data >> 8) as u8, data as u8]; - self.i2c.write(self.address, &payload).map_err(Error::I2C) - } -} - -/// Read data -pub trait ReadData: private::Sealed { - /// Error type - type Error; - - /// Read an u16 register - fn read_register(&mut self, register: u8) -> Result>; -} - -impl ReadData for I2cInterface -where - I2C: embedded_hal::i2c::I2c, -{ - type Error = E; - 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(data[0]) << 8) | u16::from(data[1]))) - } -} -- cgit v1.2.3-54-g00ecf