diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 48388f5..0e7a012 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -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: diff --git a/src/bin/cli_app.rs b/src/bin/cli_app.rs index b310019..c7f169c 100644 --- a/src/bin/cli_app.rs +++ b/src/bin/cli_app.rs @@ -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 }; diff --git a/src/lib.rs b/src/lib.rs index fbf7837..960c496 100644 --- a/src/lib.rs +++ b/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 { 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; + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index d4005ef..0c92be7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()); }