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
extern crate hidapi;
use hidapi::HidApi;
use hidapi::HidError;
use hidapi::HidResult;
const BLINK1_VENDOR_ID: u16 = 0x27b8;
const BLINK1_PRODUCT_ID: u16 = 0x01ed;
const BLINK1_REPORT_ID: u8 = 1;
const BLINK1_REPORT_SIZE: usize = 8;
const BLINK1_REPORT2_ID: u8 = 2;
const BLINK1_REPORT2_SIZE: usize = 60;
const BLINK1_BUF_SIZE:usize = BLINK1_REPORT_SIZE + 1;
const BLINK1_BUF2_SIZE: usize = BLINK1_REPORT2_SIZE + 1;
pub struct Blink1Device {
device: hidapi::HidDevice,
}
impl Blink1Device {
pub fn open_first() -> Result<Blink1Device, HidError> {
match HidApi::new() {
Ok(api) => {
match api.open(BLINK1_VENDOR_ID, BLINK1_PRODUCT_ID) {
Ok(device) => Ok(Blink1Device { device: device }),
Err(e) => return Err(e),
}
}
Err(e) => return Err(e),
}
}
pub fn open(serial: &str) -> Result<Blink1Device, HidError> {
match HidApi::new() {
Ok(api) => {
match api.open_serial(BLINK1_VENDOR_ID, BLINK1_PRODUCT_ID, serial) {
Ok(device) => Ok(Blink1Device { device }),
Err(e) => return Err(e),
}
},
Err(e) => return Err(e),
}
}
pub fn get_serials() -> Result<Vec<String>, HidError> {
match HidApi::new() {
Ok(api) => {
let mut vec: Vec<String> = Vec::new();
for device in api.devices() {
if device.product_id == BLINK1_PRODUCT_ID && device.vendor_id == BLINK1_VENDOR_ID {
if let Some(serial) = &device.serial_number {
vec.push(serial.to_owned());
}
}
}
return Ok(vec);
},
Err(e) => return Err(e),
}
}
pub fn fade_off(&self, index: u8) -> HidResult<()> {
return self.fade_to_rgb(index, 0, 0, 0);
}
pub fn fade_to_rgb(&self, index: u8, r: u8, g: u8, b: u8) -> HidResult<()> {
let dms = 100; let mut buf: [u8; BLINK1_BUF_SIZE] = [0; BLINK1_BUF_SIZE];
buf[0] = BLINK1_REPORT_ID;
buf[1] = 'c' as u8; buf[2] = r;
buf[3] = g;
buf[4] = b;
buf[5] = 0;
buf[6] = dms % 0xff;
buf[7] = index;
return self.device.send_feature_report(&buf);
}
}