1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

use linux_embedded_hal::{I2cdev, i2cdev::linux::LinuxI2CError};

use crate::error::Error;

pub struct I2c {
    dev: I2cdev,
}

impl I2c {
    pub fn new(path: &str) -> Result<Self, Error> {
        let dev = I2cdev::new(path)
            .map_err(|e| Error::Remote(format!("{:?}", e)) )?;

        Ok(Self{dev})
    }
}

use embedded_hal::blocking::i2c;

impl i2c::Read for I2c {
    type Error = LinuxI2CError;

    fn read(&mut self, addr: u8, buff: &mut [u8]) -> Result<(), Self::Error> {
        self.dev.read(addr, buff)
    }
}

impl i2c::Write for I2c {
    type Error = LinuxI2CError;

    fn write(&mut self, addr: u8, data: &[u8]) -> Result<(), Self::Error> {
        self.dev.write(addr, data)
    }
}


impl i2c::WriteRead for I2c {
    type Error = LinuxI2CError;

    fn write_read(&mut self, addr: u8, data: &[u8], buff: &mut [u8]) -> Result<(), Self::Error> {
        self.dev.write_read(addr, data, buff)
    }
}