-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmodels.rs
75 lines (63 loc) · 1.92 KB
/
models.rs
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
use crate::{instruction::Instruction, Error};
use display_interface::{DataFormat, DisplayError, WriteOnlyDataCommand};
use embedded_graphics_core::prelude::RgbColor;
use embedded_hal::{blocking::delay::DelayUs, digital::v2::OutputPin};
// existing model implementations
mod ili9486;
mod st7789;
pub use ili9486::*;
pub use st7789::*;
pub trait Model {
type ColorFormat: RgbColor;
/// Common model constructor
fn new() -> Self;
/// Initializes the display for this model
fn init<RST, DELAY>(
&mut self,
di: &mut dyn WriteOnlyDataCommand,
rst: &mut Option<RST>,
delay: &mut DELAY,
) -> Result<(), Error<RST::Error>>
where
RST: OutputPin,
DELAY: DelayUs<u32>;
fn hard_reset<RST, DELAY>(
&mut self,
rst: &mut RST,
delay: &mut DELAY,
) -> Result<(), Error<RST::Error>>
where
RST: OutputPin,
DELAY: DelayUs<u32>,
{
rst.set_low().map_err(Error::Pin)?;
delay.delay_us(10);
rst.set_high().map_err(Error::Pin)?;
Ok(())
}
/// Writes pixels to the display IC via the given DisplayInterface
/// Any pixel color format conversion is done here
fn write_pixels<DI, I>(&mut self, di: &mut DI, colors: I) -> Result<(), DisplayError>
where
DI: WriteOnlyDataCommand,
I: IntoIterator<Item = Self::ColorFormat>;
/// Size of the visible display as `(width, height)`
fn display_size(&self) -> (u16, u16);
/// Size of the display framebuffer as `(width, height)`
fn framebuffer_size(&self) -> (u16, u16) {
self.display_size()
}
}
// helper for models
pub fn write_command(
di: &mut dyn WriteOnlyDataCommand,
command: Instruction,
params: &[u8],
) -> Result<(), DisplayError> {
di.send_commands(DataFormat::U8(&[command as u8]))?;
if !params.is_empty() {
di.send_data(DataFormat::U8(params))
} else {
Ok(())
}
}