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
extern crate serial_unix;
extern crate serial;
extern crate rgb;

use std::io::Write;

const BLACK: rgb::RGB8 = rgb::RGB8 { r: 0, g: 0, b: 0 };

pub struct Mote {
    port: serial_unix::TTYPort,
}

impl Mote {
    pub fn new(path: &str) -> Mote {
        let mut mote =
            Mote { port: serial_unix::TTYPort::open(std::path::Path::new(path)).unwrap() };
        mote.init();
        mote.clear();
        mote
    }

    pub fn init(&mut self) {
        self.configure_channel(1, 16, false);
        self.configure_channel(2, 16, false);
        self.configure_channel(3, 16, false);
        self.configure_channel(4, 16, false);
    }

    pub fn clear(&mut self) {
        self.write(&[BLACK; 16 * 4])
    }

    fn configure_channel(&mut self, channel: u8, num_pixels: u8, gamma_correction: bool) {
        // 'mote'
        self.port.write(&[0x6D, 0x6F, 0x74, 0x65]).unwrap();
        // 'c'
        self.port.write(&[0x63]).unwrap();
        self.port.write(&[channel]).unwrap();
        self.port.write(&[num_pixels]).unwrap();
        self.port.write(&[if gamma_correction { 1 } else { 0 }]).unwrap();
    }

    pub fn write(&mut self, pixels: &[rgb::RGB8]) {
        // 'mote'
        self.port.write(&[0x6D, 0x6F, 0x74, 0x65]).unwrap();
        // 'o'
        self.port.write(&[0x6F]).unwrap();
        for pixel in pixels {
            self.port.write(&[pixel.b, pixel.g, pixel.r]).unwrap();
        }
    }
}