42 lines
886 B
Rust
42 lines
886 B
Rust
use super::Interface;
|
|
use embedded_hal_async::i2c::I2c;
|
|
|
|
const I2C_ADDRESS: u8 = 0x6A;
|
|
|
|
#[derive(Debug)]
|
|
pub enum InterfaceE<CommE> {
|
|
Comm(CommE),
|
|
}
|
|
|
|
/// I2C communication interface
|
|
pub struct I2cInterface<I2C> {
|
|
i2c: I2C,
|
|
}
|
|
|
|
impl<I2C> I2cInterface<I2C> {
|
|
pub fn new(i2c: I2C) -> Self {
|
|
Self { i2c }
|
|
}
|
|
}
|
|
|
|
impl<I2C, CommE> Interface for I2cInterface<I2C>
|
|
where
|
|
I2C: I2c<Error = CommE>,
|
|
{
|
|
type Error = InterfaceE<CommE>;
|
|
|
|
async fn write(&mut self, addr: u8, value: u8) -> Result<(), Self::Error> {
|
|
self.i2c
|
|
.write(I2C_ADDRESS, &[addr, value])
|
|
.await
|
|
.map_err(InterfaceE::Comm)
|
|
}
|
|
|
|
async fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
|
|
self.i2c
|
|
.write_read(I2C_ADDRESS, &[addr], buffer)
|
|
.await
|
|
.map_err(InterfaceE::Comm)
|
|
}
|
|
}
|