Removed windows from workflows.
Removed debug printing. UnknownResponse now includes the length of the response. Added new method for clearing the device state.
This commit is contained in:
9
.github/workflows/rust.yml
vendored
9
.github/workflows/rust.yml
vendored
@@ -19,15 +19,6 @@ jobs:
|
||||
- name: test
|
||||
run: cargo test --bin cli_app
|
||||
|
||||
build-windows:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Build
|
||||
run: cargo build --verbose --bin cli_app
|
||||
- name: test
|
||||
run: cargo test --bin cli_app
|
||||
|
||||
build-linux:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
@@ -20,7 +20,7 @@ fn main() {
|
||||
|
||||
#[test]
|
||||
fn test_basic_device_access() {
|
||||
let mut device = match Device::new() {
|
||||
let _ = match Device::new() {
|
||||
Ok(device) => device,
|
||||
Err(_) => return
|
||||
};
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -39,7 +39,7 @@ impl DeviceEvent {
|
||||
return Err(DeviceError::NoResponse());
|
||||
}
|
||||
if len != 8 {
|
||||
return Err(DeviceError::UnknownResponse(buf.clone()));
|
||||
return Err(DeviceError::UnknownResponse(buf.clone(), len));
|
||||
}
|
||||
match buf {
|
||||
buf if buf.starts_with(&NOW_CHARGING) => Ok(Self::NowCharging),
|
||||
@@ -49,7 +49,7 @@ impl DeviceEvent {
|
||||
buf if buf.starts_with(&STOPPED_MUTED) => Ok(Self::StoppedMuted),
|
||||
buf if buf.starts_with(&NOW_MIC_CONNECTED) => Ok(Self::NowMicConnected),
|
||||
buf if buf.starts_with(&NOW_MIC_DISCONNECTED) => Ok(Self::NowMicDisconnected),
|
||||
_ => Err(DeviceError::UnknownResponse(buf.clone())),
|
||||
_ => Err(DeviceError::UnknownResponse(buf.clone(), len)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,8 +64,8 @@ pub enum DeviceError {
|
||||
HeadSetOff(),
|
||||
#[error("No response.")]
|
||||
NoResponse(),
|
||||
#[error("Unknown response: {0:?}")]
|
||||
UnknownResponse([u8; 8]),
|
||||
#[error("Unknown response: {0:?} with length: {1}")]
|
||||
UnknownResponse([u8; 8], usize),
|
||||
}
|
||||
|
||||
pub struct Device {
|
||||
@@ -110,7 +110,6 @@ impl Device {
|
||||
pub fn wait_for_updates(&mut self, duration: Duration) -> Result<DeviceEvent, DeviceError> {
|
||||
let mut buf = [0u8; 8];
|
||||
let res = self.hid_device.read_timeout(&mut buf[..], duration.as_millis() as i32)?;
|
||||
println!("{:?}", &buf[..res]);
|
||||
|
||||
match DeviceEvent::get_event_from_buf(&buf, res) {
|
||||
Ok(event) => {
|
||||
@@ -126,7 +125,6 @@ impl Device {
|
||||
self.hid_device.write(&BATTERY_PACKET)?;
|
||||
let mut buf = [0u8; 8];
|
||||
let res = self.hid_device.read_timeout(&mut buf[..], 1000)?;
|
||||
println!("{:?}", &buf[..res]);
|
||||
match DeviceEvent::get_event_from_buf(&buf, res) {
|
||||
Ok(DeviceEvent::BatterLevel(level)) => {
|
||||
self.update_self_with_event(&DeviceEvent::BatterLevel(level));
|
||||
@@ -140,4 +138,11 @@ impl Device {
|
||||
}
|
||||
Err(DeviceError::NoResponse())
|
||||
}
|
||||
|
||||
pub fn clear_state(&mut self) {
|
||||
self.charging = None;
|
||||
self.battery_level = 0;
|
||||
self.muted = None;
|
||||
self.mic_connected = None;
|
||||
}
|
||||
}
|
||||
@@ -29,17 +29,13 @@ fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &mut Tray
|
||||
}
|
||||
DeviceError::NoDeviceFound() => {
|
||||
eprintln!("{}", DeviceError::NoDeviceFound());
|
||||
device.charging = None;
|
||||
device.mic_connected = None;
|
||||
device.muted = None;
|
||||
device.clear_state();
|
||||
tray_handler.update(device);
|
||||
tray_handler.set_status( &DeviceError::NoDeviceFound().to_string());
|
||||
}
|
||||
DeviceError::HeadSetOff() => {
|
||||
eprintln!("{}", DeviceError::HeadSetOff());
|
||||
device.charging = None;
|
||||
device.mic_connected = None;
|
||||
device.muted = None;
|
||||
device.clear_state();
|
||||
tray_handler.update(device);
|
||||
tray_handler.set_status(&DeviceError::HeadSetOff().to_string());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user