Made some device properties optional. A device property will be set once the appropriate event is observed.
This commit is contained in:
@@ -31,8 +31,9 @@ impl TrayHandler {
|
||||
#[derive(Debug)]
|
||||
pub struct BatteryTray {
|
||||
battery_level: u8,
|
||||
charging: bool,
|
||||
muted: bool,
|
||||
charging: Option<bool>,
|
||||
muted: Option<bool>,
|
||||
mic_connected: Option<bool>,
|
||||
status_message: Option<String>,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
},
|
||||
|
||||
@@ -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}");
|
||||
|
||||
32
src/lib.rs
32
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<bool>,
|
||||
pub muted: Option<bool>,
|
||||
pub mic_connected: Option<bool>,
|
||||
}
|
||||
|
||||
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<u8, DeviceError> {
|
||||
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()),
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user