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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Display models.
use crate::{
dcs::{self, InterfaceExt, SetAddressMode},
interface::Interface,
options::{self, ModelOptions, Rotation},
ConfigurationError,
};
use embedded_graphics_core::prelude::RgbColor;
use embedded_hal_async::delay::DelayNs;
// existing model implementations
mod gc9107;
mod gc9a01;
mod ili9225;
mod ili9341;
mod ili9342c;
mod ili934x;
mod ili9486;
mod ili9488;
mod ili948x;
mod rm67162;
mod st7735s;
mod st7789;
mod st7796;
pub use gc9107::*;
pub use gc9a01::*;
pub use ili9225::*;
pub use ili9341::*;
pub use ili9342c::*;
pub use ili9486::*;
pub use ili9488::*;
pub use rm67162::*;
pub use st7735s::*;
pub use st7789::*;
pub use st7796::*;
/// Display model.
pub trait Model {
/// The color format.
type ColorFormat: RgbColor;
/// The framebuffer size in pixels.
const FRAMEBUFFER_SIZE: (u16, u16);
/// Duration of the active low reset pulse in µs.
const RESET_DURATION: u32 = 10;
/// Initializes the display for this model with MADCTL from [crate::Display]
/// and returns the value of MADCTL set by init
fn init<DELAY, DI>(
&mut self,
di: &mut DI,
delay: &mut DELAY,
options: &ModelOptions,
) -> impl core::future::Future<Output = Result<SetAddressMode, ModelInitError<DI::Error>>>
where
DELAY: DelayNs,
DI: Interface;
/// Updates the address window of the display.
fn update_address_window<DI>(
di: &mut DI,
_rotation: Rotation,
sx: u16,
sy: u16,
ex: u16,
ey: u16,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move {
di.write_command(dcs::SetColumnAddress::new(sx, ex)).await?;
di.write_command(dcs::SetPageAddress::new(sy, ey)).await
}
}
///
/// Need to call [Self::wake] before issuing other commands
///
fn sleep<DI, DELAY>(
di: &mut DI,
delay: &mut DELAY,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
DELAY: DelayNs,
{
async move {
di.write_command(dcs::EnterSleepMode).await?;
// All supported models requires a 120ms delay before issuing other commands
delay.delay_us(120_000).await;
Ok(())
}
}
///
/// Wakes the display after it's been set to sleep via [Self::sleep]
///
fn wake<DI, DELAY>(
di: &mut DI,
delay: &mut DELAY,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
DELAY: DelayNs,
{
async move {
di.write_command(dcs::ExitSleepMode).await?;
// ST7789 and st7735s have the highest minimal delay of 120ms
delay.delay_us(120_000).await;
Ok(())
}
}
///
/// We need WriteMemoryStart befor write pixel
///
fn write_memory_start<DI>(
di: &mut DI,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move { di.write_command(dcs::WriteMemoryStart).await }
}
///
/// SoftReset
///
fn software_reset<DI>(di: &mut DI) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move { di.write_command(dcs::SoftReset).await }
}
///
/// This function will been called if user update options
///
fn update_options<DI>(
&self,
di: &mut DI,
options: &ModelOptions,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move {
let madctl = SetAddressMode::from(options);
di.write_command(madctl).await
}
}
///
/// Configures the tearing effect output.
///
fn set_tearing_effect<DI>(
di: &mut DI,
tearing_effect: options::TearingEffect,
_options: &ModelOptions,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move {
di.write_command(dcs::SetTearingEffect::new(tearing_effect))
.await
}
}
/// Sets the vertical scroll region.
///
/// The `top_fixed_area` and `bottom_fixed_area` arguments can be used to
/// define an area on the top and/or bottom of the display which won't be
/// affected by scrolling.
///
/// Note that this method is not affected by the current display orientation
/// and will always scroll vertically relative to the default display
/// orientation.
///
/// The combined height of the fixed area must not larger than the
/// height of the framebuffer height in the default orientation.
///
/// After the scrolling region is defined the [`set_vertical_scroll_offset`](Self::set_vertical_scroll_offset) can be
/// used to scroll the display.
fn set_vertical_scroll_region<DI>(
di: &mut DI,
top_fixed_area: u16,
bottom_fixed_area: u16,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move {
let rows = Self::FRAMEBUFFER_SIZE.1;
let vscrdef = if top_fixed_area + bottom_fixed_area > rows {
dcs::SetScrollArea::new(rows, 0, 0)
} else {
dcs::SetScrollArea::new(
top_fixed_area,
rows - top_fixed_area - bottom_fixed_area,
bottom_fixed_area,
)
};
di.write_command(vscrdef).await
}
}
/// Sets the vertical scroll offset.
///
/// Setting the vertical scroll offset shifts the vertical scroll region
/// upwards by `offset` pixels.
///
/// Use [`set_vertical_scroll_region`](Self::set_vertical_scroll_region) to setup the scroll region, before
/// using this method.
fn set_vertical_scroll_offset<DI>(
di: &mut DI,
offset: u16,
) -> impl core::future::Future<Output = Result<(), DI::Error>>
where
DI: Interface,
{
async move {
let vscad = dcs::SetScrollStart::new(offset);
di.write_command(vscad).await
}
}
}
/// Error returned by [`Model::init`].
///
/// This error type is used internally by implementations of the [`Model`]
/// trait.
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ModelInitError<DiError> {
/// Error caused by the display interface.
Interface(DiError),
/// Invalid configuration error.
///
/// This error is returned when the configuration passed to the builder is
/// invalid. For example, when the combination of bit depth and interface
/// kind isn't supported by the selected model.
InvalidConfiguration(ConfigurationError),
}
impl<DiError> From<DiError> for ModelInitError<DiError> {
fn from(value: DiError) -> Self {
Self::Interface(value)
}
}
#[cfg(test)]
mod tests {
use embedded_graphics::pixelcolor::Rgb565;
use crate::{
Builder,
_mock::{MockDelay, MockDisplayInterface},
dcs::SetAddressMode,
interface::InterfaceKind,
ConfigurationError, InitError,
};
use super::*;
struct OnlyOneKindModel(InterfaceKind);
impl Model for OnlyOneKindModel {
type ColorFormat = Rgb565;
const FRAMEBUFFER_SIZE: (u16, u16) = (16, 16);
async fn init<DELAY, DI>(
&mut self,
_di: &mut DI,
_delay: &mut DELAY,
_options: &ModelOptions,
) -> Result<SetAddressMode, ModelInitError<DI::Error>>
where
DELAY: DelayNs,
DI: Interface,
{
if DI::KIND != self.0 {
return Err(ModelInitError::InvalidConfiguration(
ConfigurationError::UnsupportedInterface,
));
}
Ok(SetAddressMode::default())
}
}
#[test]
fn test_assert_interface_kind_serial() {
tokio_test::block_on(async {
Builder::new(
OnlyOneKindModel(InterfaceKind::Serial4Line),
MockDisplayInterface,
)
.init(&mut MockDelay)
.await
.unwrap();
});
}
#[test]
fn test_assert_interface_kind_parallel() {
tokio_test::block_on(async {
assert!(matches!(
Builder::new(
OnlyOneKindModel(InterfaceKind::Parallel8Bit),
MockDisplayInterface,
)
.init(&mut MockDelay)
.await,
Err(InitError::InvalidConfiguration(
ConfigurationError::UnsupportedInterface
))
));
});
}
}