From e4d09a1a39102cc1046a0e780a16cd878ad3dd66 Mon Sep 17 00:00:00 2001 From: a3k7dotmd <92009667+a3k7dotmd@users.noreply.github.com> Date: Fri, 20 Nov 2026 17:50:54 -0800 Subject: [PATCH] Studio: read the battery of a wireless device again while its stream runs A battery layer on a wireless device kept showing the level it had when its stream started: on a DeathStalker V2 Pro (Wireless) the bar stood below 40 % for hours while the keyboard reported 100 %. The level is read once every 1800 ticks, and it was read on tick 1 of them. A wireless device whose stream has started is only served on every third tick, and that check comes first. 1800 is a multiple of 3, so tick 1 is never one of those and the read did not run again. - The battery's turn is tick 3 of the 1800 (BATTERY_TICK), a tick wireless devices get. Wired devices are read as often as before, two ticks later. - A const assertion keeps the turn on such a tick if the numbers change. - A test runs Studio::tick on the simulated wireless mouse: a started stream with 75 % noted, the battery full since. The level is still 75 % after ticks 1801 and 1802 and 100 % after 1803. With the old schedule it stays 75 %. --- crates/secbird-tray/src/studio.rs | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/secbird-tray/src/studio.rs b/crates/secbird-tray/src/studio.rs index 2092a10..afa76b3 100644 --- a/crates/secbird-tray/src/studio.rs +++ b/crates/secbird-tray/src/studio.rs @@ -51,8 +51,17 @@ const READ_EVERY: u32 = FPS; /// Static scenes and wired keyboards: send the whole frame again every few seconds, as /// keyboards drop per-key colours in quick adjustment mode without saying so (#45). const RESEND_EVERY: u32 = FPS * 3; -/// Battery read once a minute. +/// Battery read once a minute, on tick [`BATTERY_TICK`] of that minute. Wireless devices only +/// get every [`WIRELESS_EVERY`]th tick, so the read has to fall on one of those: on any other +/// it never happened, and a battery layer kept the level it had when the stream started. const BATTERY_EVERY: u32 = FPS * 60; +const BATTERY_TICK: u32 = WIRELESS_EVERY; +const _: () = assert!(BATTERY_TICK < BATTERY_EVERY && BATTERY_EVERY.is_multiple_of(WIRELESS_EVERY) && BATTERY_TICK.is_multiple_of(WIRELESS_EVERY)); + +/// Whether this tick is the battery's turn. +fn battery_turn(tick: u32) -> bool { + tick % BATTERY_EVERY == BATTERY_TICK +} impl Default for Studio { fn default() -> Self { @@ -196,7 +205,7 @@ impl Studio { let Some(dev) = devices.iter().find(|d| d.key() == key) else { continue }; let (wireless, animated, started, battery_due) = { let r = &self.running[&key]; - (r.wireless, r.scene.animated(), r.last.is_some(), r.scene.uses(Kind::Battery) && (tick % BATTERY_EVERY == 1 || r.battery.is_none())) + (r.wireless, r.scene.animated(), r.last.is_some(), r.scene.uses(Kind::Battery) && (battery_turn(tick) || r.battery.is_none())) }; if wireless && started && !tick.is_multiple_of(WIRELESS_EVERY) { continue; @@ -268,3 +277,35 @@ impl Studio { keys } } + +#[cfg(test)] +mod tests { + use super::*; + use secbird_core::sim::{Sim, WIRELESS_MOUSE_PID}; + use secbird_core::studio::Layer; + + /// A wireless device is skipped on every tick but each [`WIRELESS_EVERY`]th. The battery's + /// turn used to be tick 1 of each minute, which is never one of those, so a battery layer + /// kept showing the level from when its stream started while the device charged. + #[test] + fn a_wireless_device_gets_its_battery_read_again() { + let sim = Sim::wireless_mouse(); + let dev = sim.open(WIRELESS_MOUSE_PID, Sim::spec_for(WIRELESS_MOUSE_PID)).expect("the simulated wireless mouse"); + let key = dev.key(); + let scene = Scene { layers: vec![Layer::parse(&["battery".to_string()]).expect("a battery layer")] }; + let canvas = Canvas::for_device(&dev, &[]); + // A stream that started a minute ago, when the battery was at 75 %. + let last = Some(vec![Rgb(0, 0, 0); canvas.pixels.len()]); + let mut studio = Studio { dirty: false, tick: BATTERY_EVERY, ..Studio::default() }; + studio.running.insert(key.clone(), Running { profile: "Default".into(), scene, canvas, last, failures: 0, wireless: true, battery: Some(75) }); + sim.state().battery = Some(255); // charged since + let devices = [dev]; + let no_profiles = std::path::Path::new("no-profiles.json"); + + // Ticks 1801 and 1802 of this minute are not the device's; 1803 is, and the battery's. + for (tick, level) in [(BATTERY_EVERY + 1, 75), (BATTERY_EVERY + 2, 75), (BATTERY_EVERY + 3, 100)] { + studio.tick(&devices, None, no_profiles, false, false); + assert_eq!((studio.tick, studio.running[&key].battery), (tick, Some(level))); + } + } +}