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
#![no_std]
use embedded_hal as hal;
use display_interface::{DisplayError, WriteOnlyDataCommand};
pub struct I2CInterface<I2C> {
i2c: I2C,
addr: u8,
data_byte: u8,
}
impl<I2C> I2CInterface<I2C>
where
I2C: hal::blocking::i2c::Write,
{
pub fn new(i2c: I2C, addr: u8, data_byte: u8) -> Self {
Self {
i2c,
addr,
data_byte,
}
}
}
impl<I2C> WriteOnlyDataCommand<u8> for I2CInterface<I2C>
where
I2C: hal::blocking::i2c::Write,
{
fn send_commands(&mut self, cmds: &[u8]) -> Result<(), DisplayError> {
let mut writebuf: [u8; 8] = [0; 8];
writebuf[1..=cmds.len()].copy_from_slice(&cmds[0..cmds.len()]);
self.i2c
.write(self.addr, &writebuf[..=cmds.len()])
.map_err(|_| DisplayError::BusWriteError)
}
fn send_data(&mut self, buf: &[u8]) -> Result<(), DisplayError> {
if buf.is_empty() {
return Ok(());
}
let mut writebuf: [u8; 17] = [0; 17];
writebuf[0] = self.data_byte;
buf.chunks(16)
.try_for_each(|c| {
let chunk_len = c.len();
writebuf[1..=chunk_len].copy_from_slice(c);
self.i2c.write(self.addr, &writebuf[0..=chunk_len])
})
.map_err(|_| DisplayError::BusWriteError)
}
}