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:
lennard
2023-06-14 12:31:00 +02:00
parent d3245faddd
commit 5121e164df
4 changed files with 14 additions and 22 deletions

View File

@@ -19,15 +19,6 @@ jobs:
- name: test - name: test
run: cargo test --bin cli_app 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: build-linux:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@@ -20,7 +20,7 @@ fn main() {
#[test] #[test]
fn test_basic_device_access() { fn test_basic_device_access() {
let mut device = match Device::new() { let _ = match Device::new() {
Ok(device) => device, Ok(device) => device,
Err(_) => return Err(_) => return
}; };

View File

@@ -39,7 +39,7 @@ impl DeviceEvent {
return Err(DeviceError::NoResponse()); return Err(DeviceError::NoResponse());
} }
if len != 8 { if len != 8 {
return Err(DeviceError::UnknownResponse(buf.clone())); return Err(DeviceError::UnknownResponse(buf.clone(), len));
} }
match buf { match buf {
buf if buf.starts_with(&NOW_CHARGING) => Ok(Self::NowCharging), 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(&STOPPED_MUTED) => Ok(Self::StoppedMuted),
buf if buf.starts_with(&NOW_MIC_CONNECTED) => Ok(Self::NowMicConnected), buf if buf.starts_with(&NOW_MIC_CONNECTED) => Ok(Self::NowMicConnected),
buf if buf.starts_with(&NOW_MIC_DISCONNECTED) => Ok(Self::NowMicDisconnected), 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(), HeadSetOff(),
#[error("No response.")] #[error("No response.")]
NoResponse(), NoResponse(),
#[error("Unknown response: {0:?}")] #[error("Unknown response: {0:?} with length: {1}")]
UnknownResponse([u8; 8]), UnknownResponse([u8; 8], usize),
} }
pub struct Device { pub struct Device {
@@ -110,7 +110,6 @@ impl Device {
pub fn wait_for_updates(&mut self, duration: Duration) -> Result<DeviceEvent, DeviceError> { pub fn wait_for_updates(&mut self, duration: Duration) -> Result<DeviceEvent, DeviceError> {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
let res = self.hid_device.read_timeout(&mut buf[..], duration.as_millis() as i32)?; 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) { match DeviceEvent::get_event_from_buf(&buf, res) {
Ok(event) => { Ok(event) => {
@@ -126,7 +125,6 @@ impl Device {
self.hid_device.write(&BATTERY_PACKET)?; self.hid_device.write(&BATTERY_PACKET)?;
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];
let res = self.hid_device.read_timeout(&mut buf[..], 1000)?; let res = self.hid_device.read_timeout(&mut buf[..], 1000)?;
println!("{:?}", &buf[..res]);
match DeviceEvent::get_event_from_buf(&buf, res) { match DeviceEvent::get_event_from_buf(&buf, res) {
Ok(DeviceEvent::BatterLevel(level)) => { Ok(DeviceEvent::BatterLevel(level)) => {
self.update_self_with_event(&DeviceEvent::BatterLevel(level)); self.update_self_with_event(&DeviceEvent::BatterLevel(level));
@@ -140,4 +138,11 @@ impl Device {
} }
Err(DeviceError::NoResponse()) Err(DeviceError::NoResponse())
} }
pub fn clear_state(&mut self) {
self.charging = None;
self.battery_level = 0;
self.muted = None;
self.mic_connected = None;
}
} }

View File

@@ -29,17 +29,13 @@ fn handle_error(error: DeviceError, device: &mut Device, tray_handler: &mut Tray
} }
DeviceError::NoDeviceFound() => { DeviceError::NoDeviceFound() => {
eprintln!("{}", DeviceError::NoDeviceFound()); eprintln!("{}", DeviceError::NoDeviceFound());
device.charging = None; device.clear_state();
device.mic_connected = None;
device.muted = None;
tray_handler.update(device); tray_handler.update(device);
tray_handler.set_status( &DeviceError::NoDeviceFound().to_string()); tray_handler.set_status( &DeviceError::NoDeviceFound().to_string());
} }
DeviceError::HeadSetOff() => { DeviceError::HeadSetOff() => {
eprintln!("{}", DeviceError::HeadSetOff()); eprintln!("{}", DeviceError::HeadSetOff());
device.charging = None; device.clear_state();
device.mic_connected = None;
device.muted = None;
tray_handler.update(device); tray_handler.update(device);
tray_handler.set_status(&DeviceError::HeadSetOff().to_string()); tray_handler.set_status(&DeviceError::HeadSetOff().to_string());
} }