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
use libc::{c_char, c_int};
use std::ffi::CString;
type LedMatrixOptionsResult = Result<(), &'static str>;
#[derive(Debug)]
#[repr(C)]
pub struct LedMatrixOptions {
pub(crate) hardware_mapping: *mut c_char,
pub(crate) rows: c_int,
pub(crate) cols: c_int,
pub(crate) chain_length: c_int,
pub(crate) parallel: c_int,
pub(crate) pwm_bits: c_int,
pub(crate) pwm_lsb_nanoseconds: c_int,
pub(crate) pwm_dither_bits: c_int,
pub(crate) brightness: c_int,
pub(crate) scan_mode: c_int,
pub(crate) row_address_type: c_int,
pub(crate) multiplexing: c_int,
pub(crate) led_rgb_sequence: *mut c_char,
pub(crate) pixel_mapper_config: *mut c_char,
pub(crate) panel_type: *mut c_char,
pub(crate) disable_hardware_pulsing: c_char,
pub(crate) show_refresh_rate: c_char,
pub(crate) inverse_colors: c_char,
pub(crate) limit_refresh_rate_hz: c_int,
}
#[derive(Debug)]
#[repr(C)]
pub struct LedRuntimeOptions {
pub(crate) gpio_slowdown: c_int,
pub(crate) daemon: c_int,
pub(crate) drop_privileges: c_int,
pub(crate) do_gpio_init: bool,
}
impl LedMatrixOptions {
pub fn new() -> LedMatrixOptions {
LedMatrixOptions {
hardware_mapping: CString::new("regular").unwrap().into_raw(),
rows: 32,
cols: 32,
chain_length: 1,
parallel: 1,
pwm_bits: 11,
pwm_lsb_nanoseconds: 1000,
pwm_dither_bits: 1,
brightness: 100,
scan_mode: 0,
row_address_type: 0,
multiplexing: 0,
led_rgb_sequence: CString::new("RGB").unwrap().into_raw(),
pixel_mapper_config: CString::new("").unwrap().into_raw(),
panel_type: CString::new("").unwrap().into_raw(),
disable_hardware_pulsing: 1,
show_refresh_rate: 1,
inverse_colors: 0,
limit_refresh_rate_hz: 0,
}
}
pub fn set_hardware_mapping(&mut self, mapping: &str) {
unsafe {
let _ = CString::from_raw(self.hardware_mapping);
self.hardware_mapping = CString::new(mapping).unwrap().into_raw();
}
}
pub fn set_rows(&mut self, rows: u32) {
self.rows = rows as c_int;
}
pub fn set_cols(&mut self, cols: u32) {
self.cols = cols as c_int;
}
pub fn set_chain_length(&mut self, chain_length: u32) {
self.chain_length = chain_length as c_int;
}
pub fn set_parallel(&mut self, parallel: u32) {
self.parallel = parallel as c_int;
}
pub fn set_pwm_bits(&mut self, pwm_bits: u8) -> LedMatrixOptionsResult {
if pwm_bits > 11 {
Err("Pwm bits can only have value between 0 and 11 inclusive")
} else {
self.pwm_bits = pwm_bits as c_int;
Ok(())
}
}
pub fn set_pwm_lsb_nanoseconds(&mut self, pwm_lsb_nanoseconds: u32) {
self.pwm_lsb_nanoseconds = pwm_lsb_nanoseconds as c_int;
}
pub fn set_brightness(&mut self, brightness: u8) -> LedMatrixOptionsResult {
if brightness > 100 || brightness < 1 {
Err("Brigthness can only have value between 1 and 100 inclusive")
} else {
self.brightness = brightness as c_int;
Ok(())
}
}
pub fn set_scan_mode(&mut self, scan_mode: u32) {
self.scan_mode = scan_mode as c_int;
}
pub fn set_led_rgb_sequence(&mut self, sequence: &str) {
unsafe {
let _ = CString::from_raw(self.led_rgb_sequence);
self.led_rgb_sequence = CString::new(sequence).unwrap().into_raw();
}
}
pub fn set_pixel_mapper_config(&mut self, mapper: &str) {
unsafe {
let _ = CString::from_raw(self.pixel_mapper_config);
self.pixel_mapper_config = CString::new(mapper).unwrap().into_raw();
}
}
pub fn set_hardware_pulsing(&mut self, enable: bool) {
if enable {
self.disable_hardware_pulsing = 0;
} else {
self.disable_hardware_pulsing = 1;
}
}
pub fn set_refresh_rate(&mut self, enable: bool) {
if enable {
self.show_refresh_rate = 1;
} else {
self.show_refresh_rate = 0;
}
}
pub fn set_inverse_colors(&mut self, enable: bool) {
if enable {
self.inverse_colors = 1;
} else {
self.inverse_colors = 0;
}
}
pub fn set_multiplexing(&mut self, multiplexing: u32) {
self.multiplexing = multiplexing as c_int;
}
pub fn set_row_addr_type(&mut self, row_addr_type: u32) {
self.row_address_type = row_addr_type as c_int;
}
pub fn set_limit_refresh(&mut self, limit_refresh: u32) {
self.limit_refresh_rate_hz = limit_refresh as c_int;
}
pub fn set_pwm_dither_bits(&mut self, pwm_dither_bits: u32) {
self.pwm_dither_bits = pwm_dither_bits as c_int;
}
pub fn set_panel_type(&mut self, panel_type: &str) {
unsafe {
let _ = CString::from_raw(self.panel_type);
self.panel_type = CString::new(panel_type).unwrap().into_raw();
}
}
}
impl Default for LedMatrixOptions {
fn default() -> Self {
Self::new()
}
}
impl Drop for LedMatrixOptions {
fn drop(&mut self) {
unsafe {
let _ = CString::from_raw(self.hardware_mapping);
let _ = CString::from_raw(self.led_rgb_sequence);
let _ = CString::from_raw(self.panel_type);
}
}
}
impl LedRuntimeOptions {
pub fn new() -> Self {
Self {
gpio_slowdown: 1,
daemon: 0,
drop_privileges: 1,
do_gpio_init: true,
}
}
pub fn set_gpio_slowdown(&mut self, gpio_slowdown: u32) {
self.gpio_slowdown = gpio_slowdown as i32;
}
pub fn set_daemon(&mut self, daemon: bool) {
self.daemon = if daemon { 1 } else { 0 };
}
pub fn set_drop_privileges(&mut self, drop_privileges: bool) {
self.drop_privileges = if drop_privileges { 1 } else { 0 };
}
pub fn set_do_gpio_init(&mut self, do_gpio_init: bool) {
self.do_gpio_init = do_gpio_init;
}
}
impl Default for LedRuntimeOptions {
fn default() -> Self {
Self::new()
}
}