-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathmod.rs
More file actions
266 lines (223 loc) · 7.52 KB
/
mod.rs
File metadata and controls
266 lines (223 loc) · 7.52 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! A simple Driver for the Waveshare 3.7" E-Ink Display via SPI
//!
//!
//! Build with the help of documentation/code from [Waveshare](https://www.waveshare.com/wiki/3.7inch_e-Paper_HAT),
use embedded_hal::{
blocking::{delay::DelayUs, spi::Write},
digital::v2::{InputPin, OutputPin},
};
pub(crate) mod command;
mod constants;
use self::command::Command;
use self::constants::*;
use crate::buffer_len;
use crate::color::Color;
use crate::interface::DisplayInterface;
use crate::traits::{InternalWiAdditions, RefreshLut, WaveshareDisplay};
/// Width of the display.
pub const WIDTH: u32 = 280;
/// Height of the display
pub const HEIGHT: u32 = 480;
/// Default Background Color
pub const DEFAULT_BACKGROUND_COLOR: Color = Color::White;
const IS_BUSY_LOW: bool = false;
/// Display with Fullsize buffer for use with the 3in7 EPD
#[cfg(feature = "graphics")]
pub type Display3in7 = crate::graphics::Display<
WIDTH,
HEIGHT,
{ crate::graphics::DisplayMode::BwrBitOff as u8 },
{ buffer_len(WIDTH as usize, HEIGHT as usize) },
Color,
>;
/// EPD3in7 driver
pub struct EPD3in7<SPI, CS, BUSY, DC, RST, DELAY> {
/// Connection Interface
interface: DisplayInterface<SPI, CS, BUSY, DC, RST, DELAY>,
/// Background Color
background_color: Color,
}
impl<SPI, CS, BUSY, DC, RST, DELAY> InternalWiAdditions<SPI, CS, BUSY, DC, RST, DELAY>
for EPD3in7<SPI, CS, BUSY, DC, RST, DELAY>
where
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
DELAY: DelayUs<u32>,
{
fn init(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
// reset the device
self.interface.reset(delay, 30, 10);
self.interface.cmd(spi, Command::SwReset)?;
delay.delay_us(300000u32);
self.interface
.cmd_with_data(spi, Command::AutoWriteRedRamRegularPattern, &[0xF7])?;
self.interface.wait_until_idle(delay, IS_BUSY_LOW);
self.interface
.cmd_with_data(spi, Command::AutoWriteBwRamRegularPattern, &[0xF7])?;
self.interface.wait_until_idle(delay, IS_BUSY_LOW);
self.interface
.cmd_with_data(spi, Command::GateSetting, &[0xDF, 0x01, 0x00])?;
self.interface
.cmd_with_data(spi, Command::GateVoltage, &[0x00])?;
self.interface
.cmd_with_data(spi, Command::GateVoltageSource, &[0x41, 0xA8, 0x32])?;
self.interface
.cmd_with_data(spi, Command::DataEntrySequence, &[0x03])?;
self.interface
.cmd_with_data(spi, Command::BorderWaveformControl, &[0x03])?;
self.interface.cmd_with_data(
spi,
Command::BoosterSoftStartControl,
&[0xAE, 0xC7, 0xC3, 0xC0, 0xC0],
)?;
self.interface
.cmd_with_data(spi, Command::TemperatureSensorSelection, &[0x80])?;
self.interface
.cmd_with_data(spi, Command::WriteVcomRegister, &[0x44])?;
self.interface.cmd_with_data(
spi,
Command::DisplayOption,
&[0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x4F, 0xFF, 0xFF, 0xFF, 0xFF],
)?;
self.interface.cmd_with_data(
spi,
Command::SetRamXAddressStartEndPosition,
&[0x00, 0x00, 0x17, 0x01],
)?;
self.interface.cmd_with_data(
spi,
Command::SetRamYAddressStartEndPosition,
&[0x00, 0x00, 0xDF, 0x01],
)?;
self.interface
.cmd_with_data(spi, Command::DisplayUpdateSequenceSetting, &[0xCF])?;
self.set_lut(spi, delay, Some(RefreshLut::Full))?;
Ok(())
}
}
impl<SPI, CS, BUSY, DC, RST, DELAY> WaveshareDisplay<SPI, CS, BUSY, DC, RST, DELAY>
for EPD3in7<SPI, CS, BUSY, DC, RST, DELAY>
where
SPI: Write<u8>,
CS: OutputPin,
BUSY: InputPin,
DC: OutputPin,
RST: OutputPin,
DELAY: DelayUs<u32>,
{
type DisplayColor = Color;
fn new(
spi: &mut SPI,
cs: CS,
busy: BUSY,
dc: DC,
rst: RST,
delay: &mut DELAY,
delay_us: Option<u32>,
) -> Result<Self, SPI::Error> {
let mut epd = EPD3in7 {
interface: DisplayInterface::new(cs, busy, dc, rst, delay_us),
background_color: DEFAULT_BACKGROUND_COLOR,
};
epd.init(spi, delay)?;
Ok(epd)
}
fn wake_up(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
self.init(spi, delay)
}
fn sleep(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
self.interface.cmd_with_data(spi, Command::Sleep, &[0xF7])?;
self.interface.cmd(spi, Command::PowerOff)?;
self.interface
.cmd_with_data(spi, Command::Sleep2, &[0xA5])?;
Ok(())
}
fn set_background_color(&mut self, color: Self::DisplayColor) {
self.background_color = color;
}
fn background_color(&self) -> &Self::DisplayColor {
&self.background_color
}
fn width(&self) -> u32 {
WIDTH
}
fn height(&self) -> u32 {
HEIGHT
}
fn update_frame(
&mut self,
spi: &mut SPI,
buffer: &[u8],
_delay: &mut DELAY,
) -> Result<(), SPI::Error> {
assert!(buffer.len() == buffer_len(WIDTH as usize, HEIGHT as usize));
self.interface
.cmd_with_data(spi, Command::SetRamXAddressCounter, &[0x00, 0x00])?;
self.interface
.cmd_with_data(spi, Command::SetRamYAddressCounter, &[0x00, 0x00])?;
self.interface
.cmd_with_data(spi, Command::WriteRam, buffer)?;
Ok(())
}
#[allow(unused)]
fn update_partial_frame(
&mut self,
spi: &mut SPI,
delay: &mut DELAY,
buffer: &[u8],
x: u32,
y: u32,
width: u32,
height: u32,
) -> Result<(), SPI::Error> {
todo!()
}
fn display_frame(&mut self, spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
//self.interface
// .cmd_with_data(spi, Command::WRITE_LUT_REGISTER, &LUT_1GRAY_GC)?;
self.interface.cmd(spi, Command::DisplayUpdateSequence)?;
self.interface.wait_until_idle(delay, IS_BUSY_LOW);
Ok(())
}
fn update_and_display_frame(
&mut self,
spi: &mut SPI,
buffer: &[u8],
delay: &mut DELAY,
) -> Result<(), SPI::Error> {
self.update_frame(spi, buffer, delay)?;
self.display_frame(spi, delay)?;
Ok(())
}
fn clear_frame(&mut self, spi: &mut SPI, _delay: &mut DELAY) -> Result<(), SPI::Error> {
self.interface
.cmd_with_data(spi, Command::SetRamXAddressCounter, &[0x00, 0x00])?;
self.interface
.cmd_with_data(spi, Command::SetRamYAddressCounter, &[0x00, 0x00])?;
let color = self.background_color.get_byte_value();
self.interface.cmd(spi, Command::WriteRam)?;
self.interface.data_x_times(spi, color, WIDTH * HEIGHT)?;
Ok(())
}
fn set_lut(
&mut self,
spi: &mut SPI,
_delay: &mut DELAY,
refresh_rate: Option<RefreshLut>,
) -> Result<(), SPI::Error> {
let buffer = match refresh_rate {
Some(RefreshLut::Full) | None => &LUT_1GRAY_GC,
Some(RefreshLut::Quick) => &LUT_1GRAY_DU,
};
self.interface
.cmd_with_data(spi, Command::WriteLutRegister, buffer)?;
Ok(())
}
fn wait_until_idle(&mut self, _spi: &mut SPI, delay: &mut DELAY) -> Result<(), SPI::Error> {
self.interface.wait_until_idle(delay, IS_BUSY_LOW);
Ok(())
}
}