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)]
|
#[derive(Debug)]
|
||||||
pub struct BatteryTray {
|
pub struct BatteryTray {
|
||||||
battery_level: u8,
|
battery_level: u8,
|
||||||
charging: bool,
|
charging: Option<bool>,
|
||||||
muted: bool,
|
muted: Option<bool>,
|
||||||
|
mic_connected: Option<bool>,
|
||||||
status_message: Option<String>,
|
status_message: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,8 +41,9 @@ impl BatteryTray {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
BatteryTray {
|
BatteryTray {
|
||||||
battery_level: 0,
|
battery_level: 0,
|
||||||
charging: false,
|
charging: None,
|
||||||
muted: false,
|
muted: None,
|
||||||
|
mic_connected: None,
|
||||||
status_message: Some("No device found".to_string()),
|
status_message: Some("No device found".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,6 +52,7 @@ impl BatteryTray {
|
|||||||
self.battery_level = device.battery_level;
|
self.battery_level = device.battery_level;
|
||||||
self.charging = device.charging;
|
self.charging = device.charging;
|
||||||
self.muted = device.muted;
|
self.muted = device.muted;
|
||||||
|
self.mic_connected = device.mic_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_status(&mut self, message: &str) {
|
pub fn set_status(&mut self, message: &str) {
|
||||||
@@ -81,13 +84,26 @@ impl Tray for BatteryTray {
|
|||||||
Some(m) => m.clone(),
|
Some(m) => m.clone(),
|
||||||
None => {
|
None => {
|
||||||
let mut description = format!("Battery level: {}%", self.battery_level);
|
let mut description = format!("Battery level: {}%", self.battery_level);
|
||||||
if self.charging {
|
if let Some(charging) = self.charging {
|
||||||
description += "\nCharging";
|
if charging {
|
||||||
} else {
|
description += "\nCharging";
|
||||||
description += "\nNot charging";
|
} else {
|
||||||
|
description += "\nNot charging";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if self.muted {
|
if let Some(muted) = self.muted {
|
||||||
description += "\nMuted";
|
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
|
description
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ fn main() {
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let (battery_level, _) = match device.update_battery_level() {
|
let battery_level = match device.update_battery_level() {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
eprintln!("{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 STOPPED_CHARGING: [u8; 5] = [6, 255, 187, 3, 0];
|
||||||
const NOW_MUTED: [u8; 5] = [6, 255, 187, 32, 1];
|
const NOW_MUTED: [u8; 5] = [6, 255, 187, 32, 1];
|
||||||
const STOPPED_MUTED: [u8; 5] = [6, 255, 187, 32, 0];
|
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] = {
|
const BATTERY_PACKET: [u8; 20] = {
|
||||||
let mut packet = [0; 20];
|
let mut packet = [0; 20];
|
||||||
@@ -27,6 +29,8 @@ pub enum DeviceEvent {
|
|||||||
StoppedCharging,
|
StoppedCharging,
|
||||||
NowMuted,
|
NowMuted,
|
||||||
StoppedMuted,
|
StoppedMuted,
|
||||||
|
NowMicDisconnected,
|
||||||
|
NowMicConnected,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DeviceEvent {
|
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(&CHARGING_PREAMBLE) => Ok(Self::BatterLevel(buf[BATTERY_LEVEL_INDEX])),
|
||||||
buf if buf.starts_with(&NOW_MUTED) => Ok(Self::NowMuted),
|
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(&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())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,8 +71,9 @@ pub enum DeviceError {
|
|||||||
pub struct Device {
|
pub struct Device {
|
||||||
hid_device: HidDevice,
|
hid_device: HidDevice,
|
||||||
pub battery_level: u8,
|
pub battery_level: u8,
|
||||||
pub charging: bool,
|
pub charging: Option<bool>,
|
||||||
pub muted: bool,
|
pub muted: Option<bool>,
|
||||||
|
pub mic_connected: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
@@ -81,19 +88,22 @@ impl Device {
|
|||||||
}).ok_or(DeviceError::NoDeviceFound())??;
|
}).ok_or(DeviceError::NoDeviceFound())??;
|
||||||
Ok(Device {
|
Ok(Device {
|
||||||
hid_device,
|
hid_device,
|
||||||
charging: false,
|
charging: None,
|
||||||
battery_level: 0,
|
battery_level: 0,
|
||||||
muted: false,
|
muted: None,
|
||||||
|
mic_connected: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_self_with_event(&mut self, event: &DeviceEvent) {
|
fn update_self_with_event(&mut self, event: &DeviceEvent) {
|
||||||
match event {
|
match event {
|
||||||
DeviceEvent::BatterLevel(level) => self.battery_level = level.clone(),
|
DeviceEvent::BatterLevel(level) => self.battery_level = level.clone(),
|
||||||
DeviceEvent::NowCharging => self.charging = true,
|
DeviceEvent::NowCharging => self.charging = Some(true),
|
||||||
DeviceEvent::StoppedCharging => self.charging = false,
|
DeviceEvent::StoppedCharging => self.charging = Some(false),
|
||||||
DeviceEvent::NowMuted => self.muted = true,
|
DeviceEvent::NowMuted => self.muted = Some(true),
|
||||||
DeviceEvent::StoppedMuted => self.muted = false,
|
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> {
|
pub fn update_battery_level(&mut self) -> Result<u8, DeviceError> {
|
||||||
for _ in 0..10 {
|
for _ in 0..10 { // loop if other events are currently happening.
|
||||||
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)?;
|
||||||
@@ -120,7 +130,7 @@ impl Device {
|
|||||||
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));
|
||||||
return Ok((self.battery_level, self.charging));
|
return Ok(self.battery_level);
|
||||||
}
|
}
|
||||||
Ok(event) => self.update_self_with_event(&event),
|
Ok(event) => self.update_self_with_event(&event),
|
||||||
Err(DeviceError::NoResponse()) => return Err(DeviceError::HeadSetOff()),
|
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() => {
|
DeviceError::NoDeviceFound() => {
|
||||||
eprintln!("{}", 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());
|
tray_handler.set_status( &DeviceError::NoDeviceFound().to_string());
|
||||||
}
|
}
|
||||||
DeviceError::HeadSetOff() => {
|
DeviceError::HeadSetOff() => {
|
||||||
eprintln!("{}", 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());
|
tray_handler.set_status(&DeviceError::HeadSetOff().to_string());
|
||||||
}
|
}
|
||||||
error => {
|
error => {
|
||||||
|
|||||||
Reference in New Issue
Block a user