1extern crate firmata;
2
3use firmata::*;
4use std::sync::{Arc, Mutex};
5use std::thread;
6
7fn init(board: Arc<Mutex<firmata::Board>>) {
8 {
9 let mut b = board.lock().unwrap();
10 b.i2c_config(0);
11 b.i2c_write(0x09, "o".as_bytes());
12 thread::sleep_ms(10);
13 }
14
15 let b = board.clone();
16 thread::spawn(move || {
17 loop {
18 b.lock().unwrap().decode();
19 b.lock().unwrap().query_firmware();
20 thread::sleep_ms(10);
21 }
22 });
23}
24
25fn set_rgb(board: Arc<Mutex<firmata::Board>>, rgb: [u8; 3]) {
26 let mut b = board.lock().unwrap();
27 b.i2c_write(0x09, "n".as_bytes());
28 b.i2c_write(0x09, &rgb);
29}
30
31fn read_rgb(board: Arc<Mutex<firmata::Board>>) -> Vec<u8> {
32 {
33 let mut b = board.lock().unwrap();
34 b.i2c_write(0x09, "g".as_bytes());
35 b.i2c_read(0x09, 3);
36 }
37 loop {
38 {
39 let mut b = board.lock().unwrap();
40 if b.i2c_data.iter().count() > 0 {
41 return b.i2c_data.pop().unwrap().data;
42 }
43 }
44 thread::sleep_ms(10);
45 }
46}
47
48fn main() {
49 let board = Arc::new(Mutex::new(firmata::Board::new("/dev/ttyACM0")));
50
51 init(board.clone());
52
53 set_rgb(board.clone(), [255, 0, 0]);
54 println!("rgb: {:?}", read_rgb(board.clone()));
55 thread::sleep_ms(1000);
56
57 set_rgb(board.clone(), [0, 255, 0]);
58 println!("rgb: {:?}", read_rgb(board.clone()));
59 thread::sleep_ms(1000);
60
61 set_rgb(board.clone(), [0, 0, 255]);
62 println!("rgb: {:?}", read_rgb(board.clone()));
63 thread::sleep_ms(1000);
64}