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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use nb;
pub trait DisplayInterface {
type Error;
fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error>;
fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error>;
fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error>;
}
pub mod spi {
use embedded_hal as hal;
use super::DisplayInterface;
use nb;
#[derive(Debug)]
pub enum SpiInterfaceError<DCE, SPIE> {
DCError(DCE),
SPIError(SPIE),
}
impl<DCE, SPIE> SpiInterfaceError<DCE, SPIE> {
fn from_dc(e: DCE) -> Self {
Self::DCError(e)
}
fn from_spi(e: SPIE) -> Self {
Self::SPIError(e)
}
}
pub struct SpiInterface<SPI, DC> {
spi: SPI,
dc: DC,
}
impl<SPI, DC> SpiInterface<SPI, DC>
where
SPI: hal::spi::FullDuplex<u8>,
DC: hal::digital::v2::OutputPin,
{
pub fn new(spi: SPI, dc: DC) -> Self {
Self { spi: spi, dc: dc }
}
}
impl<SPI, DC> DisplayInterface for SpiInterface<SPI, DC>
where
SPI: hal::spi::FullDuplex<u8>,
DC: hal::digital::v2::OutputPin,
{
type Error = SpiInterfaceError<
<DC as hal::digital::v2::OutputPin>::Error,
<SPI as hal::spi::FullDuplex<u8>>::Error,
>;
fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error> {
while let Ok(_) = self.spi.read() {
self.dc.set_high().map_err(Self::Error::from_dc)?;
}
self.dc.set_low().map_err(Self::Error::from_dc)?;
let bus_op = nb::block!(self.spi.send(cmd))
.and_then(|_| nb::block!(self.spi.read()))
.map_err(Self::Error::from_spi)
.map(core::mem::drop);
self.dc.set_high().map_err(Self::Error::from_dc)?;
bus_op
}
fn send_data(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
for word in buf {
nb::block!(self.spi.send(word.clone())).map_err(Self::Error::from_spi)?;
nb::block!(self.spi.read()).map_err(Self::Error::from_spi)?;
}
Ok(())
}
fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error> {
match self.spi.send(word) {
Ok(()) => {
let _ = self.spi.read();
Ok(())
}
Err(nb::Error::Other(e)) => Err(nb::Error::Other(Self::Error::from_spi(e))),
Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
}
}
}
}
#[cfg(test)]
pub mod test_spy {
use super::DisplayInterface;
use nb;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone, Debug, PartialEq)]
pub enum Sent {
Cmd(u8),
Data(Vec<u8>),
}
pub struct TestSpyInterface {
sent: Rc<RefCell<Vec<Sent>>>,
}
impl TestSpyInterface {
pub fn new() -> Self {
TestSpyInterface {
sent: Rc::new(RefCell::new(Vec::new())),
}
}
pub fn split(&self) -> Self {
Self {
sent: self.sent.clone(),
}
}
pub fn check(&self, cmd: u8, data: &[u8]) {
let sent = self.sent.borrow();
if data.len() == 0 {
assert_eq!(sent.len(), 1);
} else {
assert_eq!(sent.len(), 2);
assert_eq!(sent[1], Sent::Data(data.to_vec()));
}
assert_eq!(sent[0], Sent::Cmd(cmd));
}
pub fn check_multi(&self, expect: &[Sent]) {
assert_eq!(*self.sent.borrow(), expect);
}
pub fn clear(&mut self) {
self.sent.borrow_mut().clear()
}
}
impl DisplayInterface for TestSpyInterface {
type Error = core::convert::Infallible;
fn send_command(&mut self, cmd: u8) -> Result<(), Self::Error> {
self.sent.borrow_mut().push(Sent::Cmd(cmd));
Ok(())
}
fn send_data(&mut self, data: &[u8]) -> Result<(), Self::Error> {
self.sent.borrow_mut().push(Sent::Data(data.to_vec()));
Ok(())
}
fn send_data_async(&mut self, word: u8) -> nb::Result<(), Self::Error> {
let mut sent = self.sent.borrow_mut();
{
let last_idx = sent.len() - 1;
match &mut sent[last_idx] {
Sent::Cmd(_) => {}
Sent::Data(ref mut d) => {
d.push(word);
return Ok(());
}
};
}
sent.push(Sent::Data(vec![word]));
Ok(())
}
}
}