diff --git a/src/battery_tray.rs b/src/battery_tray.rs index a233a9a..b8e5ef5 100644 --- a/src/battery_tray.rs +++ b/src/battery_tray.rs @@ -31,8 +31,9 @@ impl TrayHandler { #[derive(Debug)] pub struct BatteryTray { battery_level: u8, - charging: bool, - muted: bool, + charging: Option, + muted: Option, + mic_connected: Option, status_message: Option, } @@ -40,8 +41,9 @@ impl BatteryTray { pub fn new() -> Self { BatteryTray { battery_level: 0, - charging: false, - muted: false, + charging: None, + muted: None, + mic_connected: None, status_message: Some("No device found".to_string()), } } @@ -50,6 +52,7 @@ impl BatteryTray { self.battery_level = device.battery_level; self.charging = device.charging; self.muted = device.muted; + self.mic_connected = device.mic_connected; } pub fn set_status(&mut self, message: &str) { @@ -81,13 +84,26 @@ impl Tray for BatteryTray { Some(m) => m.clone(), None => { let mut description = format!("Battery level: {}%", self.battery_level); - if self.charging { - description += "\nCharging"; - } else { - description += "\nNot charging"; + if let Some(charging) = self.charging { + if charging { + description += "\nCharging"; + } else { + description += "\nNot charging"; + } } - if self.muted { - description += "\nMuted"; + if let Some(muted) = self.muted { + if muted { + description += "\nMuted"; + } else { + description += "\nNot muted"; + } + } + if let Some(mic_connected) = self.mic_connected { + if mic_connected { + description += "\nMicrophone connected"; + } else { + description += "\nMicrophone not connected"; + } } description }, diff --git a/src/bin/cli_app.rs b/src/bin/cli_app.rs index 423d131..48cf640 100644 --- a/src/bin/cli_app.rs +++ b/src/bin/cli_app.rs @@ -8,7 +8,7 @@ fn main() { std::process::exit(1); } }; - let (battery_level, _) = match device.update_battery_level() { + let battery_level = match device.update_battery_level() { Ok(t) => t, Err(error) => { eprintln!("{error}"); diff --git a/src/lib.rs b/src/lib.rs index a8a8e45..fbf7837 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,8 @@ const NOW_CHARGING: [u8; 5] = [6, 255, 187, 3, 1]; const STOPPED_CHARGING: [u8; 5] = [6, 255, 187, 3, 0]; const NOW_MUTED: [u8; 5] = [6, 255, 187, 32, 1]; const STOPPED_MUTED: [u8; 5] = [6, 255, 187, 32, 0]; +const NOW_MIC_DISCONNECTED: [u8; 5] = [6, 255, 187, 8, 0]; +const NOW_MIC_CONNECTED: [u8; 5] = [6, 255, 187, 8, 1]; const BATTERY_PACKET: [u8; 20] = { let mut packet = [0; 20]; @@ -27,6 +29,8 @@ pub enum DeviceEvent { StoppedCharging, NowMuted, StoppedMuted, + NowMicDisconnected, + NowMicConnected, } impl DeviceEvent { @@ -43,6 +47,8 @@ impl DeviceEvent { buf if buf.starts_with(&CHARGING_PREAMBLE) => Ok(Self::BatterLevel(buf[BATTERY_LEVEL_INDEX])), buf if buf.starts_with(&NOW_MUTED) => Ok(Self::NowMuted), 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())), } } @@ -65,8 +71,9 @@ pub enum DeviceError { pub struct Device { hid_device: HidDevice, pub battery_level: u8, - pub charging: bool, - pub muted: bool, + pub charging: Option, + pub muted: Option, + pub mic_connected: Option, } impl Device { @@ -81,19 +88,22 @@ impl Device { }).ok_or(DeviceError::NoDeviceFound())??; Ok(Device { hid_device, - charging: false, + charging: None, battery_level: 0, - muted: false, + muted: None, + mic_connected: None, }) } fn update_self_with_event(&mut self, event: &DeviceEvent) { match event { DeviceEvent::BatterLevel(level) => self.battery_level = level.clone(), - DeviceEvent::NowCharging => self.charging = true, - DeviceEvent::StoppedCharging => self.charging = false, - DeviceEvent::NowMuted => self.muted = true, - DeviceEvent::StoppedMuted => self.muted = false, + DeviceEvent::NowCharging => self.charging = Some(true), + DeviceEvent::StoppedCharging => self.charging = Some(false), + DeviceEvent::NowMuted => self.muted = Some(true), + DeviceEvent::StoppedMuted => self.muted = Some(false), + DeviceEvent::NowMicDisconnected => self.mic_connected = Some(false), + DeviceEvent::NowMicConnected => self.mic_connected = Some(true), }; } @@ -111,8 +121,8 @@ impl Device { } } - pub fn update_battery_level(&mut self) -> Result<(u8, bool), DeviceError> { - for _ in 0..10 { + pub fn update_battery_level(&mut self) -> Result { + for _ in 0..10 { // loop if other events are currently happening. self.hid_device.write(&BATTERY_PACKET)?; let mut buf = [0u8; 8]; let res = self.hid_device.read_timeout(&mut buf[..], 1000)?; @@ -120,7 +130,7 @@ impl Device { match DeviceEvent::get_event_from_buf(&buf, res) { Ok(DeviceEvent::BatterLevel(level)) => { self.update_self_with_event(&DeviceEvent::BatterLevel(level)); - return Ok((self.battery_level, self.charging)); + return Ok(self.battery_level); } Ok(event) => self.update_self_with_event(&event), Err(DeviceError::NoResponse()) => return Err(DeviceError::HeadSetOff()), diff --git a/src/main.rs b/src/main.rs index 844b62c..d4005ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,10 +29,18 @@ 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; + 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; + tray_handler.update(device); tray_handler.set_status(&DeviceError::HeadSetOff().to_string()); } error => {