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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
extern crate libc;
mod c;

use libc::{c_int, c_char};
use std::ptr::null;
use std::ffi::CString;
use std::path::Path;

pub use c::LedMatrixOptions;
pub use c::LedColor;


pub struct LedCanvas {
    handle: *mut c::LedCanvas
}

pub struct LedMatrix {
    handle: *mut c::LedMatrix,
    _options: LedMatrixOptions,
}

pub struct LedFont {
    handle: *mut c::LedFont
}


impl LedMatrix {
    pub fn new(options: Option<LedMatrixOptions>) -> Result<LedMatrix, &'static str> {
        let options = {
            if let Some(o) = options {
                o
            }
            else {
                LedMatrixOptions::new()
            }
        };

        let handle = unsafe {
            c::led_matrix_create_from_options(
                &options as *const LedMatrixOptions,
                null::<c_int>() as *mut c_int,
                null::<c_char>() as *mut*mut*mut c_char)
        };

        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);
        }
    }
}


impl LedFont {
    pub fn new(bdf_file: &Path) -> Result<LedFont, &'static str> {
        let string = match bdf_file.to_str() {
            Some(s) => s,
            None => return Err("Couldn't convert path to str")
        };
        let cstring = CString::new(string).unwrap();

        let handle = unsafe {
            c::load_font(cstring.as_ptr())
        };

        if handle.is_null() {
            Err("Couldn't load font")
        }
        else {
            Ok(LedFont {
                handle
            })
        }
    }
}

impl Drop for LedFont {
    fn drop(&mut self) {
        unsafe {
            c::delete_font(self.handle)
        }
    }
}


impl LedCanvas {
    pub fn size(&self) -> (i32, i32) {
        let (mut width, mut height): (c_int, c_int) = (0, 0);
        unsafe {
            c::led_canvas_get_size(self.handle,
                &mut width as *mut c_int, &mut height as *mut c_int);
        }
        (width as i32, height as i32)
    }

    pub fn set(&mut self, x: i32, y: i32, color: &LedColor) {
        unsafe {
            c::led_canvas_set_pixel(self.handle, x as c_int, y as c_int,
                color.red, color.green, color.blue)
        }
    }

    pub fn clear(&mut self) {
        unsafe {
            c::led_canvas_clear(self.handle);
        }
    }

    pub fn fill(&mut self, color: &LedColor) {
        unsafe {
            c::led_canvas_fill(self.handle,
                color.red, color.green, color.blue);
        }
    }

    pub fn draw_line(&mut self,
        x0: i32, y0: i32, x1: i32, y1: i32, color: &LedColor)
    {
        unsafe {
            c::draw_line(self.handle,
                x0, y0, x1, y1,
                color.red, color.green, color.blue);
        }
    }

    pub fn draw_circle(&mut self,
        x: i32, y: i32, radius: u32, color: &LedColor)
    {
        unsafe {
            c::draw_circle(self.handle,
                x as c_int, y as c_int,
                radius as c_int,
                color.red, color.green, color.blue);
        }
    }

    pub fn draw_text(&mut self, font: &LedFont, text: &str,
        x: i32, y: i32, color: &LedColor,
        kerning_offset: i32, vertical: bool) -> i32
    {
        let ctext = CString::new(text).unwrap();
        unsafe {
            if vertical {
                c::vertical_draw_text(self.handle, font.handle, x as c_int, y as c_int,
                    color.red, color.green, color.blue,
                    ctext.as_ptr(), kerning_offset as c_int) as i32
            }
            else {
                c::draw_text(self.handle, font.handle, x as c_int, y as c_int,
                    color.red, color.green, color.blue,
                    ctext.as_ptr(), kerning_offset as c_int) as i32
            }
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use std::{thread, time};
    use std::f64::consts::PI;

    fn led_matrix() -> LedMatrix {
        let mut options = LedMatrixOptions::new();
        options.set_hardware_mapping("adafruit-hat-pwm");
        options.set_chain_length(2);
        options.set_hardware_pulsing(false);
        //options.set_inverse_colors(true);
        //options.set_refresh_rate(true);
        LedMatrix::new(Some(options)).unwrap()
    }

    #[test]
    fn matrix_create() {
        let _matrix = led_matrix();
    }

    #[test]
    fn canvas_size() {
        let matrix = led_matrix();
        let canvas = matrix.canvas();
        assert_eq!(canvas.size(), (64, 32));
    }

    #[test]
    fn draw_line() {
        let matrix = led_matrix();
        let mut canvas = matrix.canvas();
        let (width, height) = canvas.size();
        let mut color = LedColor {
            red: 127,
            green: 0,
            blue: 0
        };

        canvas.clear();
        for x in 0..width {
            color.blue = 255 - 3 * x as u8;
            canvas.draw_line(x, 0, width - 1 - x, height - 1, &color);
            thread::sleep(time::Duration::new(0, 10000000));
        }
    }

    #[test]
    fn draw_circle() {
        let matrix = led_matrix();
        let mut canvas = matrix.canvas();
        let (width, height) = canvas.size();
        let mut color = LedColor {
            red: 127,
            green: 0,
            blue: 0
        };
        let (x, y) = (width / 2, height / 2);

        canvas.clear();
        for r in 0..(width / 2) {
            color.green = color.red;
            color.red = color.blue;
            color.blue = (r * r) as u8;
            canvas.draw_circle(x, y, r as u32, &color);
            thread::sleep(time::Duration::new(0, 100000000));
        }
    }

    #[test]
    fn draw_text() {
        let matrix = led_matrix();
        let mut canvas = matrix.canvas();
        let font = LedFont::new(
            Path::new("/usr/share/fonts/misc/10x20.bdf")).unwrap();
        let color = LedColor {
            red: 0,
            green: 127,
            blue: 0
        };
        let (width, height) = canvas.size();
        let text_width = 10 * 9;
        let baseline = height / 2;

        canvas = matrix.offscreen_canvas();
        for x in 0..(2 * width) {
            let x = x % (10 * 9);
            canvas.clear();
            canvas.draw_text(&font, "Mah boy! ",
                x, baseline, &color, 0, false);
            canvas.draw_text(&font, "Mah boy! ",
                x - text_width, baseline, &color, 0, false);
            canvas.draw_text(&font, "Mah boy! ",
                x + text_width, baseline, &color, 0, false);
            canvas = matrix.swap(canvas);
            thread::sleep(time::Duration::new(0, 50000000));
        }
    }

    #[test]
    fn gradient() {
        let matrix = led_matrix();
        let mut canvas = matrix.canvas();
        let mut color = LedColor {
            red: 0,
            green: 0,
            blue: 0
        };
        let period = 400;
        let duration = time::Duration::new(3, 0);
        let sleep_duration = duration / period;

        for t in 0..period {
            let t = t as f64;
            color.red = ((PI * t / period as f64).sin() * 255.) as u8;
            color.green = ((2. * PI * t / period as f64).cos() * 255.) as u8;
            color.blue = ((3. * PI * t / period as f64 + 0.3).cos() * 255.) as u8;
            canvas.fill(&color);
            thread::sleep(sleep_duration);
        }
    }

    #[test]
    fn canvas_swap() {
        let matrix = led_matrix();
        let mut canvas = matrix.canvas();
        let mut color = LedColor {
            red: 127,
            green: 127,
            blue: 0
        };
        
        canvas.fill(&color);
        canvas = matrix.offscreen_canvas();
        color.blue = 127;
        canvas.fill(&color);
        thread::sleep(time::Duration::new(0, 500000000));
        canvas = matrix.swap(canvas);
        color.red = 0;
        canvas.fill(&color);
        thread::sleep(time::Duration::new(0, 500000000));
        matrix.swap(canvas);
        thread::sleep(time::Duration::new(0, 500000000));
    }
}