rsiot/components/cmp_raspberrypi_i2c_master/
rsiot_i2c_driver.rs

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::time::Duration;

use async_trait::async_trait;
use rppal::i2c::I2c;
use tokio::time::sleep;

use crate::drivers_i2c::RsiotI2cDriverBase;

const DELAY_BETWEEN_REQUESTS: Duration = Duration::from_millis(10);

pub struct RsiotI2cDriver {
    i2c: I2c,
}

impl RsiotI2cDriver {
    pub fn new() -> Result<Self, String> {
        let i2c = I2c::new().map_err(|e| e.to_string())?;
        let i2c = Self { i2c };
        Ok(i2c)
    }
}

#[async_trait]
impl RsiotI2cDriverBase for RsiotI2cDriver {
    async fn read_platform(
        &mut self,
        address: u8,
        response_size: usize,
        timeout: Duration,
    ) -> Result<Vec<u8>, String> {
        sleep(DELAY_BETWEEN_REQUESTS).await;
        self.i2c
            .set_slave_address(address as u16)
            .map_err(|e| e.to_string())?;
        self.i2c
            .set_timeout(timeout.as_millis() as u32)
            .map_err(|e| e.to_string())?;
        let mut response = vec![0; response_size];
        self.i2c.read(&mut response).map_err(|e| e.to_string())?;
        Ok(response)
    }

    async fn write_platform(
        &mut self,
        address: u8,
        request: &[u8],
        timeout: Duration,
    ) -> Result<(), String> {
        sleep(DELAY_BETWEEN_REQUESTS).await;
        self.i2c
            .set_slave_address(address as u16)
            .map_err(|e| e.to_string())?;
        self.i2c
            .set_timeout(timeout.as_millis() as u32)
            .map_err(|e| e.to_string())?;
        self.i2c.write(request).map_err(|e| e.to_string())?;
        Ok(())
    }

    async fn write_read_platform(
        &mut self,
        address: u8,
        request: &[u8],
        response_size: usize,
        timeout: Duration,
    ) -> Result<Vec<u8>, String> {
        sleep(DELAY_BETWEEN_REQUESTS).await;
        self.i2c
            .set_slave_address(address as u16)
            .map_err(|e| e.to_string())?;
        self.i2c
            .set_timeout(timeout.as_millis() as u32)
            .map_err(|e| e.to_string())?;
        let mut response = vec![0; response_size];
        self.i2c
            .write_read(request, &mut response)
            .map_err(|e| e.to_string())?;
        Ok(response)
    }
}