From ea4a76739fc7a7dc72021ab9c58cf12861c21819 Mon Sep 17 00:00:00 2001 From: Lennard Kittner Date: Fri, 16 May 2025 13:20:09 +0200 Subject: [PATCH] Add passive_refresh_state --- src/bin/hyper_headset_cli.rs | 7 ++----- src/devices/mod.rs | 19 ++++++++++++++++++- src/main.rs | 21 ++++++++++++++------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/bin/hyper_headset_cli.rs b/src/bin/hyper_headset_cli.rs index 9418505..b6be5ae 100644 --- a/src/bin/hyper_headset_cli.rs +++ b/src/bin/hyper_headset_cli.rs @@ -110,14 +110,11 @@ fn main() { std::thread::sleep(Duration::from_secs_f64(0.5)); - if let Err(error) = device.refresh_state() { + if let Err(error) = device.active_refresh_state() { eprintln!("{error}"); std::process::exit(1); }; - println!( - "{}", - device.get_device_state() - ); + println!("{}", device.get_device_state()); } #[test] diff --git a/src/devices/mod.rs b/src/devices/mod.rs index a8445e5..a10c897 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -300,7 +300,8 @@ pub trait Device { self.get_event_from_device_response(&buf[0..res]) } - fn refresh_state(&mut self) -> Result<(), DeviceError> { + /// Refreshes the state by querying all available information + fn active_refresh_state(&mut self) -> Result<(), DeviceError> { let packets = vec![ self.get_wireless_connected_status_packet(), self.get_charging_packet(), @@ -333,4 +334,20 @@ pub trait Device { Err(DeviceError::NoResponse()) } } + + /// Refreshes the state by listening for events + /// Only the battery level is actively queried because it is not communicated by the device on its own + fn passive_refresh_state(&mut self) -> Result<(), DeviceError> { + if let Some(event) = self.wait_for_updates(Duration::from_secs(1)) { + self.get_device_state_mut().update_self_with_event(&event); + } + if let Some(batter_packet) = self.get_battery_packet() { + self.get_device_state().hid_device.write(&batter_packet)?; + if let Some(event) = self.wait_for_updates(Duration::from_secs(1)) { + self.get_device_state_mut().update_self_with_event(&event); + } + } + + Ok(()) + } } diff --git a/src/main.rs b/src/main.rs index 8e4e07d..1ed6908 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use std::time::Duration; use clap::{Arg, Command}; +use std::time::Duration; mod status_tray; use hyper_headset::devices::connect_compatible_device; @@ -14,11 +14,10 @@ fn main() { Arg::new("refresh_interval") .long("refresh_interval") .required(false) - .help( - "Set the refresh interval (in seconds)", - ) + .help("Set the refresh interval (in seconds)") .value_parser(clap::value_parser!(u64)), - ).get_matches(); + ) + .get_matches(); let refresh_interval = *matches.get_one::("refresh_interval").unwrap_or(&3); let refresh_interval = Duration::from_secs(refresh_interval); let tray_handler = TrayHandler::new(StatusTray::new()); @@ -32,9 +31,16 @@ fn main() { }; // Run loop + let mut run_counter = 0; loop { std::thread::sleep(refresh_interval); - match device.refresh_state() { + // with the default refresh_interval the state is only actively queried every 3min + // quiting the device to frequently can lead to instability + match if run_counter % 60 == 0 { + device.active_refresh_state() + } else { + device.passive_refresh_state() + } { Ok(()) => (), Err(error) => { eprintln!("{error}"); @@ -43,7 +49,8 @@ fn main() { break; // try to reconnect } }; - tray_handler.update(device.get_device_state()) + tray_handler.update(device.get_device_state()); + run_counter += 1; } } }