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
use crate::c;
use crate::{LedCanvas, LedColor, LedMatrixOptions, LedRuntimeOptions};
#[cfg(feature = "embeddedgraphics")]
use embedded_graphics::{drawable::Pixel, geometry::Size, pixelcolor::PixelColor, DrawTarget};
pub struct LedMatrix {
handle: *mut c::LedMatrix,
_options: LedMatrixOptions,
}
impl LedMatrix {
pub fn new(
options: Option<LedMatrixOptions>,
rt_options: Option<LedRuntimeOptions>,
) -> Result<LedMatrix, &'static str> {
let mut options = options.unwrap_or_default();
let mut rt_options = rt_options.unwrap_or_default();
let handle = unsafe {
c::led_matrix_create_from_options_and_rt_options(
&mut options as *mut LedMatrixOptions,
&mut rt_options as *mut LedRuntimeOptions,
)
};
if handle.is_null() {
Err("Couldn't create LedMatrix")
} else {
Ok(LedMatrix {
handle,
_options: options,
})
}
}
pub fn canvas(&self) -> LedCanvas {
let handle = unsafe { c::led_matrix_get_canvas(self.handle) };
LedCanvas { handle }
}
pub fn offscreen_canvas(&self) -> LedCanvas {
let handle = unsafe { c::led_matrix_create_offscreen_canvas(self.handle) };
LedCanvas { handle }
}
pub fn swap(&self, canvas: LedCanvas) -> LedCanvas {
let handle = unsafe { c::led_matrix_swap_on_vsync(self.handle, canvas.handle) };
LedCanvas { handle }
}
}
impl Drop for LedMatrix {
fn drop(&mut self) {
unsafe {
c::led_matrix_delete(self.handle);
}
}
}
#[cfg(feature = "embeddedgraphics")]
impl<C> DrawTarget<C> for LedCanvas
where
C: Into<LedColor> + PixelColor,
{
type Error = core::convert::Infallible;
fn draw_pixel(&mut self, item: Pixel<C>) -> Result<(), Self::Error> {
let Pixel(point, color) = item;
self.set(point.x, point.y, &color.into());
Ok(())
}
fn size(&self) -> Size {
let size = self.canvas_size();
Size::new(size.0 as u32, size.1 as u32)
}
fn clear(&mut self, color: C) -> Result<(), Self::Error> {
self.fill(&color.into());
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn led_matrix() -> LedMatrix {
let mut options = LedMatrixOptions::new();
let mut rt_options = LedRuntimeOptions::new();
options.set_hardware_mapping("adafruit-hat-pwm");
options.set_chain_length(2);
options.set_hardware_pulsing(false);
options.set_refresh_rate(true);
options.set_brightness(10).unwrap();
rt_options.set_gpio_slowdown(2);
LedMatrix::new(Some(options), Some(rt_options)).unwrap()
}
#[test]
fn matrix_create() {
let _matrix = led_matrix();
}
}