forked from korkje/mow
-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.rs
46 lines (32 loc) · 990 Bytes
/
status.rs
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
use anyhow::anyhow;
use hidapi::HidDevice;
use std::{thread, time::Duration};
pub fn get_buffer(device: &HidDevice) -> Result<[u8; 65], anyhow::Error> {
let mut to_send = [0u8; 65];
to_send[3] = 0x02;
to_send[4] = 0x02;
to_send[6] = 0x83;
device.send_feature_report(&to_send)?;
thread::sleep(Duration::from_millis(50));
let mut resp = [0u8; 65];
device.get_feature_report(&mut resp)?;
Ok(resp)
}
pub fn get(device: &HidDevice) -> Result<usize, anyhow::Error> {
let mut resp = get_buffer(device)?;
device.get_feature_report(&mut resp)?;
let status = [0xA1, 0xA4, 0xA2, 0xA0, 0xA3]
.iter()
.position(|&s| s == resp[1])
.ok_or_else(|| anyhow!("failed to get status"))?;
if resp[6] != 0x83 {
return Ok(2);
}
Ok(status)
}
pub fn check_sleep(device: &HidDevice) -> Result<(), anyhow::Error> {
if get(device)? == 1 {
return Err(anyhow!("device is sleeping"));
}
Ok(())
}