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 as hidapi_rust;
pub use hidapi_rust::HidApi;
use hidapi_rust::{HidDevice, c_ushort};
const VENDOR_ID: c_ushort = 6000;
const PRODUCT_ID: c_ushort = 65280;
pub enum Area {
Left,
Middle,
Right,
}
pub enum Mode {
Reset = 7,
Off = 0,
Default = 1,
}
impl Area {
pub fn to_number(self) -> u8 {
match self {
Area::Left => 1,
Area::Middle => 2,
Area::Right => 3,
}
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Color {
pub fn new(r: u8, g: u8, b: u8) -> Color {
Color { r: r, g: g, b: b }
}
}
pub struct KeyboardLights<'a> {
device: HidDevice<'a>,
}
impl<'a> KeyboardLights<'a> {
pub fn from_hid_api(api: &'a HidApi) -> Result<KeyboardLights<'a>, &'static str> {
let device = api.open(VENDOR_ID, PRODUCT_ID)?;
Ok(KeyboardLights { device: device })
}
pub fn set_mode(&self, mode: Mode) {
self.device.send_feature_report(&[1, 2, 65, mode as u8, 0, 0, 0, 0]);
}
pub fn set_area(&self, area: Area, color: Color) {
self.device
.send_feature_report(&[1, 2, 64, area.to_number(), color.r, color.g, color.b, 0]);
}
pub fn set_all(&self, color: Color) {
for i in 1..4 {
self.device.send_feature_report(&[1, 2, 64, i, color.r, color.g, color.b, 0]);
}
}
}