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
extern crate libm;
use hal;
use command::{BufCommand, Command, DeepSleepMode};
use config::Config;
use interface::DisplayInterface;
pub const MAX_GATE_OUTPUTS: u16 = 296;
pub const MAX_SOURCE_OUTPUTS: u8 = 160;
const ANALOG_BLOCK_CONTROL_MAGIC: u8 = 0x54;
const DIGITAL_BLOCK_CONTROL_MAGIC: u8 = 0x3B;
pub struct Dimensions {
pub rows: u16,
pub cols: u8,
}
#[derive(Clone, Copy)]
pub enum Rotation {
Rotate0,
Rotate90,
Rotate180,
Rotate270,
}
impl Default for Rotation {
fn default() -> Self {
Rotation::Rotate0
}
}
pub struct Display<'a, I>
where
I: DisplayInterface,
{
interface: I,
config: Config<'a>,
}
impl<'a, I> Display<'a, I>
where
I: DisplayInterface,
{
pub fn new(interface: I, config: Config<'a>) -> Self {
Self { interface, config }
}
pub fn reset<D: hal::blocking::delay::DelayMs<u8>>(
&mut self,
delay: &mut D,
) -> Result<(), I::Error> {
self.interface.reset(delay);
Command::SoftReset.execute(&mut self.interface)?;
self.interface.busy_wait();
self.init()
}
fn init(&mut self) -> Result<(), I::Error> {
Command::AnalogBlockControl(ANALOG_BLOCK_CONTROL_MAGIC).execute(&mut self.interface)?;
Command::DigitalBlockControl(DIGITAL_BLOCK_CONTROL_MAGIC).execute(&mut self.interface)?;
Command::DriverOutputControl(self.config.dimensions.rows, 0x00)
.execute(&mut self.interface)?;
self.config.dummy_line_period.execute(&mut self.interface)?;
self.config.gate_line_width.execute(&mut self.interface)?;
self.config.write_vcom.execute(&mut self.interface)?;
if let Some(ref write_lut) = self.config.write_lut {
write_lut.execute(&mut self.interface)?;
}
self.config.data_entry_mode.execute(&mut self.interface)?;
let end = self.config.dimensions.cols / 8 - 1;
Command::StartEndXPosition(0, end).execute(&mut self.interface)?;
Command::StartEndYPosition(0, self.config.dimensions.rows).execute(&mut self.interface)?;
Ok(())
}
pub fn update<D: hal::blocking::delay::DelayMs<u8>>(
&mut self,
black: &[u8],
red: &[u8],
delay: &mut D,
) -> Result<(), I::Error> {
let buf_limit = libm::ceilf((self.rows() * self.cols() as u16) as f32 / 8.) as usize;
Command::XAddress(0).execute(&mut self.interface)?;
Command::YAddress(0).execute(&mut self.interface)?;
BufCommand::WriteBlackData(&black[..buf_limit]).execute(&mut self.interface)?;
Command::XAddress(0).execute(&mut self.interface)?;
Command::YAddress(0).execute(&mut self.interface)?;
BufCommand::WriteRedData(&red[..buf_limit]).execute(&mut self.interface)?;
Command::UpdateDisplayOption2(0xC7).execute(&mut self.interface)?;
Command::UpdateDisplay.execute(&mut self.interface)?;
delay.delay_ms(50);
self.interface.busy_wait();
Ok(())
}
pub fn deep_sleep(&mut self) -> Result<(), I::Error> {
Command::DeepSleepMode(DeepSleepMode::PreserveRAM).execute(&mut self.interface)
}
pub fn rows(&self) -> u16 {
self.config.dimensions.rows
}
pub fn cols(&self) -> u8 {
self.config.dimensions.cols
}
pub fn rotation(&self) -> Rotation {
self.config.rotation
}
}