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
use crate::driver::color::{
LedPixelColor, LedPixelColorGrb24, LedPixelColorImpl, LedPixelColorRgbw32,
};
use crate::driver::{Ws2812Esp32RmtDriver, Ws2812Esp32RmtDriverError};
use embedded_graphics_core::draw_target::DrawTarget;
use embedded_graphics_core::geometry::{OriginDimensions, Point, Size};
use embedded_graphics_core::pixelcolor::{Rgb888, RgbColor};
use embedded_graphics_core::Pixel;
use std::marker::PhantomData;
pub trait LedPixelShape {
fn pixel_len() -> usize {
let size = Self::size();
(size.width * size.height) as usize
}
fn size() -> Size;
fn pixel_index(point: Point) -> Option<usize>;
}
pub struct LedPixelMatrix<const W: usize, const H: usize> {}
impl<const W: usize, const H: usize> LedPixelShape for LedPixelMatrix<W, H> {
fn size() -> Size {
Size::new(W as u32, H as u32)
}
fn pixel_index(point: Point) -> Option<usize> {
if (0..W as i32).contains(&point.x) && (0..H as i32).contains(&point.y) {
Some((point.x + point.y * W as i32) as usize)
} else {
None
}
}
}
pub struct LedPixelDrawTarget<CDraw, CDev, S>
where
CDraw: RgbColor,
CDev: LedPixelColor + From<CDraw>,
S: LedPixelShape,
{
driver: Ws2812Esp32RmtDriver,
data: Vec<u8>,
brightness: u8,
changed: bool,
_phantom: PhantomData<(CDraw, CDev, S)>,
}
impl<CDraw, CDev, S> LedPixelDrawTarget<CDraw, CDev, S>
where
CDraw: RgbColor,
CDev: LedPixelColor + From<CDraw>,
S: LedPixelShape,
{
pub fn new(channel_num: u8, gpio_num: u32) -> Result<Self, Ws2812Esp32RmtDriverError> {
let driver = Ws2812Esp32RmtDriver::new(channel_num, gpio_num)?;
let data = std::iter::repeat(0)
.take(S::pixel_len() * CDev::BPP)
.collect::<Vec<_>>();
Ok(Self {
driver,
data,
brightness: u8::MAX,
changed: true,
_phantom: Default::default(),
})
}
#[inline]
pub fn set_brightness(&mut self, brightness: u8) {
self.brightness = brightness;
self.changed = true;
}
#[inline]
pub fn brightness(&self) -> u8 {
self.brightness
}
pub fn clear_with_black(&mut self) -> Result<(), Ws2812Esp32RmtDriverError> {
self.data.fill(0);
self.changed = true;
Ok(())
}
pub fn flush(&mut self) -> Result<(), Ws2812Esp32RmtDriverError> {
if self.changed {
self.driver.write(&self.data)?;
self.changed = false;
}
Ok(())
}
}
impl<CDraw, CDev, S> OriginDimensions for LedPixelDrawTarget<CDraw, CDev, S>
where
CDraw: RgbColor,
CDev: LedPixelColor + From<CDraw>,
S: LedPixelShape,
{
#[inline]
fn size(&self) -> Size {
S::size()
}
}
impl<CDraw, CDev, S> DrawTarget for LedPixelDrawTarget<CDraw, CDev, S>
where
CDraw: RgbColor,
CDev: LedPixelColor + From<CDraw>,
S: LedPixelShape,
{
type Color = CDraw;
type Error = Ws2812Esp32RmtDriverError;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for Pixel(point, color) in pixels {
if let Some(pixel_index) = S::pixel_index(point) {
let index = pixel_index * CDev::BPP;
let color_device = CDev::from(color).brightness(self.brightness);
for (offset, v) in color_device.as_ref().iter().enumerate() {
self.data[index + offset] = *v;
}
self.changed = true;
}
}
Ok(())
}
fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
let c = CDev::from(color).brightness(self.brightness);
for (index, v) in self.data.iter_mut().enumerate() {
*v = c.as_ref()[index % CDev::BPP];
}
self.changed = true;
Ok(())
}
}
impl<
const N: usize,
const R_ORDER: usize,
const G_ORDER: usize,
const B_ORDER: usize,
const W_ORDER: usize,
> From<Rgb888> for LedPixelColorImpl<N, R_ORDER, G_ORDER, B_ORDER, W_ORDER>
{
fn from(x: Rgb888) -> Self {
Self::new_with_rgb(x.r(), x.g(), x.b())
}
}
pub type LedPixelStrip<const L: usize> = LedPixelMatrix<L, 1>;
pub type Ws2812DrawTarget<S> = LedPixelDrawTarget<Rgb888, LedPixelColorGrb24, S>;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_led_pixel_matrix() {
assert_eq!(LedPixelMatrix::<10, 5>::pixel_len(), 50);
assert_eq!(LedPixelMatrix::<10, 5>::size(), Size::new(10, 5));
assert_eq!(
LedPixelMatrix::<10, 5>::pixel_index(Point::new(0, 0)),
Some(0)
);
assert_eq!(
LedPixelMatrix::<10, 5>::pixel_index(Point::new(9, 4)),
Some(49)
);
assert_eq!(
LedPixelMatrix::<10, 5>::pixel_index(Point::new(-1, 0)),
None
);
assert_eq!(
LedPixelMatrix::<10, 5>::pixel_index(Point::new(0, -1)),
None
);
assert_eq!(
LedPixelMatrix::<10, 5>::pixel_index(Point::new(10, 4)),
None
);
assert_eq!(LedPixelMatrix::<10, 5>::pixel_index(Point::new(9, 5)), None);
}
#[test]
fn test_led_pixel_strip() {
assert_eq!(LedPixelStrip::<10>::pixel_len(), 10);
assert_eq!(LedPixelStrip::<10>::size(), Size::new(10, 1));
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(0, 0)), Some(0));
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(9, 0)), Some(9));
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(-1, 0)), None);
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(0, -1)), None);
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(10, 0)), None);
assert_eq!(LedPixelStrip::<10>::pixel_index(Point::new(9, 1)), None);
}
#[test]
fn test_ws2812draw_target_new() {
let draw = Ws2812DrawTarget::<LedPixelMatrix<10, 5>>::new(0, 27).unwrap();
assert_eq!(draw.changed, true);
assert_eq!(
draw.data,
std::iter::repeat(0).take(150).collect::<Vec<_>>()
);
}
#[test]
fn test_ws2812draw_target_draw() {
let mut draw = Ws2812DrawTarget::<LedPixelMatrix<10, 5>>::new(0, 27).unwrap();
draw.draw_iter(
[
Pixel(Point::new(0, 0), Rgb888::new(0x01, 0x02, 0x03)),
Pixel(Point::new(9, 4), Rgb888::new(0x04, 0x05, 0x06)),
Pixel(Point::new(10, 5), Rgb888::new(0xFF, 0xFF, 0xFF)), ]
.iter()
.cloned(),
)
.unwrap();
assert_eq!(draw.changed, true);
assert_eq!(draw.data[0..3], [0x02, 0x01, 0x03]);
assert_eq!(draw.data[3..147], [0x00; 144]);
assert_eq!(draw.data[147..150], [0x05, 0x04, 0x06]);
draw.changed = false;
draw.clear(Rgb888::new(0x07, 0x08, 0x0A)).unwrap();
assert_eq!(draw.changed, true);
assert_eq!(
draw.data,
std::iter::repeat([0x08, 0x07, 0x0A])
.take(50)
.flatten()
.collect::<Vec<_>>()
);
draw.changed = false;
draw.clear_with_black().unwrap();
assert_eq!(draw.changed, true);
assert_eq!(draw.data, [0x00; 150]);
draw.changed = false;
}
#[test]
fn test_ws2812draw_target_flush() {
let mut draw = Ws2812DrawTarget::<LedPixelMatrix<10, 5>>::new(0, 27).unwrap();
draw.changed = true;
draw.data.fill(0x01);
draw.driver.pixel_data = None;
draw.flush().unwrap();
assert_eq!(draw.driver.pixel_data.unwrap(), draw.data);
assert_eq!(draw.changed, false);
draw.driver.pixel_data = None;
draw.flush().unwrap();
assert_eq!(draw.driver.pixel_data, None);
assert_eq!(draw.changed, false);
}
}