Newer
Older
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
// Uses too much RAM to be useful ):
// 320 * 240 * 2 * 2 bytes/pixel = 307KB of RAM
use core::convert::Infallible;
use embedded_graphics::{
draw_target::DrawTarget,
geometry::{Dimensions, Point},
pixelcolor::PixelColor,
Pixel,
};
pub struct BufferedDisplay<const L: usize, C: 'static, E, DT: DrawTarget<Color = C, Error = E>> {
target: DT,
width: usize,
height: usize,
last: &'static mut [C; L],
cur: &'static mut [C; L],
}
impl<const L: usize, C: PixelColor, E, DT: DrawTarget<Color = C, Error = E>>
BufferedDisplay<L, C, E, DT>
{
pub fn new(
mut target: DT,
color: C,
last: &'static mut [C; L],
cur: &'static mut [C; L],
) -> Result<Self, E> {
let size = target.bounding_box().size;
let width = size.width.try_into().unwrap();
let height = size.height.try_into().unwrap();
assert_eq!(L, width * height);
target.fill_solid(&target.bounding_box(), color)?;
Ok(Self {
target,
width,
height,
last,
cur,
})
}
pub fn flush(&mut self) -> Result<(), E> {
let iter = BufferedDisplayIter::new(self.width, self.height, self.last, self.cur);
self.target.draw_iter(iter)?;
Ok(())
}
#[inline(always)]
fn index<X: TryInto<usize>, Y: TryInto<usize>>(&self, x: X, y: Y) -> Option<usize> {
let x: usize = x.try_into().ok()?;
let y: usize = y.try_into().ok()?;
if y >= self.height || x >= self.width {
return None;
}
Some(x + y * self.width)
}
}
impl<const L: usize, C: PixelColor, E, DT: DrawTarget<Color = C, Error = E>> Dimensions
for BufferedDisplay<L, C, E, DT>
{
fn bounding_box(&self) -> embedded_graphics::primitives::Rectangle {
self.target.bounding_box()
}
}
impl<const L: usize, C: PixelColor, E, DT: DrawTarget<Color = C, Error = E>> DrawTarget
for BufferedDisplay<L, C, E, DT>
{
type Color = C;
type Error = Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = embedded_graphics::prelude::Pixel<Self::Color>>,
{
for Pixel(p, c) in pixels {
if let Some(idx) = self.index(p.x, p.y) {
self.cur[idx] = c;
}
}
Ok(())
}
}
struct BufferedDisplayIter<'a, C: PixelColor> {
width: usize,
height: usize,
last: &'a mut [C],
cur: &'a mut [C],
i: usize,
}
impl<'a, C: PixelColor> BufferedDisplayIter<'a, C> {
fn new(width: usize, height: usize, last: &'a mut [C], cur: &'a mut [C]) -> Self {
Self {
width,
height,
last,
cur,
i: 0,
}
}
#[inline(always)]
fn get_xy(&self, idx: usize) -> (i32, i32) {
let y = (idx / self.width).try_into().unwrap();
let x = (idx % self.width).try_into().unwrap();
(x, y)
}
}
impl<'a, C: PixelColor> Iterator for BufferedDisplayIter<'a, C> {
type Item = Pixel<C>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.i >= self.cur.len() {
return None;
}
let c = self.cur[self.i];
if self.last[self.i] != c {
self.last[self.i] = c;
let (x, y) = self.get_xy(self.i);
return Some(Pixel(Point::new(x, y), c));
}
self.i += 1;
}
}
}