Added some validation checks, and the app now detects the charging state.

This commit is contained in:
lennard
2023-06-09 21:36:34 +02:00
parent ace84a318d
commit 06d75c110c
2 changed files with 22 additions and 3 deletions

View File

@@ -6,6 +6,12 @@ const VENDOR_IDS: [u16; 2] = [0x0951, 0x03F0];
// Possible Cloud II Wireless product IDs
const PRODUCT_IDS: [u16; 2] = [0x1718, 0x018B];
const BATTERY_LEVEL_INDEX: usize = 7;
const CHARGING_INDEX: usize = 5;
const CHARGING_STATE: u8 = 0x10;
const NOT_CHARGING_STATE: u8 = 0xF;
const PREAMBLE: [u8; 5] = [6, 255, 187, 2, 0];
const BATTERY_PACKET: [u8; 20] = {
let mut packet = [0; 20];
(packet[0], packet[1], packet[2], packet[3]) = (0x06, 0xff, 0xbb, 0x02);
@@ -20,6 +26,8 @@ pub enum DeviceError {
NoDeviceFound(),
#[error("Error: No response. Is the headset turned on?")]
HeadSetOff(),
#[error("Error: Unknown response.")]
UnknownResponse(),
}
pub struct Device {
@@ -39,7 +47,6 @@ impl Device {
Ok(Device { hid_device })
}
//TODO: implement charging
pub fn get_battery_level(&self) -> Result<(u8, bool), DeviceError> {
self.hid_device.write(&BATTERY_PACKET)?;
let mut buf = [0u8; 8];
@@ -47,7 +54,14 @@ impl Device {
if res == 0 {
return Err(DeviceError::HeadSetOff());
}
println!("Read: {:?}", &buf[..res]);
Ok((buf[7], false))
if !buf.starts_with(&PREAMBLE) {
return Err(DeviceError::UnknownResponse());
}
let charging = match buf[CHARGING_INDEX] {
CHARGING_STATE => true,
NOT_CHARGING_STATE => false,
_ => return Err(DeviceError::UnknownResponse()),
};
Ok((buf[BATTERY_LEVEL_INDEX], charging))
}
}

View File

@@ -16,4 +16,9 @@ fn main() {
}
};
println!("Battery level: {}%", battery_level);
if charging {
println!("Charging");
} else {
println!("Not charging");
}
}