diff options
Diffstat (limited to 'src/interface.rs')
-rw-r--r-- | src/interface.rs | 53 |
1 files changed, 0 insertions, 53 deletions
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<I2C> { - 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<Self::Error>>; -} - -impl<I2C, E> WriteData for I2cInterface<I2C> -where - I2C: embedded_hal::i2c::I2c<Error = E>, -{ - type Error = E; - fn write_register(&mut self, register: u8, data: u16) -> Result<(), Error<E>> { - 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<u16, Error<Self::Error>>; -} - -impl<I2C, E> ReadData for I2cInterface<I2C> -where - I2C: embedded_hal::i2c::I2c<Error = E>, -{ - type Error = E; - 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(data[0]) << 8) | u16::from(data[1]))) - } -} |