Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion crates/secbird-tray/src/studio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ fn battery_turn(tick: u32) -> bool {
tick % BATTERY_EVERY == BATTERY_TICK
}

// Wireless devices get no regular resend (the traffic would keep them awake), only the rows
// that changed. A keyboard can drop back to its own lighting without saying so: a DeathStalker
// V2 Pro does after it was switched off and on, and showed its saved green while the tray kept
// a 15 % battery bar on it, until a lock started the stream over. So when a wireless device has
// just answered the battery read, its stream starts over there and then: the device is awake,
// and the radio is in use for that read anyway. Other scenes, and a device that doesn't answer,
// are left as they were. With replies, as at any start: sent without waiting for them, two of
// the six rows got lost on the way to that keyboard and stayed dark.

impl Default for Studio {
fn default() -> Self {
Studio {
Expand Down Expand Up @@ -217,13 +226,16 @@ impl Studio {
if failures > 0 && !started && !tick.is_multiple_of(backoff) {
continue;
}
let mut answered = false;
if battery_due {
let level = dev.battery().ok().flatten();
answered = level.is_some();
if let Some(r) = self.running.get_mut(&key) {
r.battery = level.or(r.battery);
}
}
let resend = !wireless && tick.is_multiple_of(RESEND_EVERY);
let refresh = wireless && started && answered && battery_turn(tick);
if started && !animated && !resend {
continue;
}
Expand All @@ -232,6 +244,7 @@ impl Studio {
let frame = render(&r.scene, &r.canvas, t, &input);
let result = match &r.last {
None => dev.start_stream(&r.canvas, &frame),
Some(_) if refresh => dev.start_stream(&r.canvas, &frame),
Some(_) if resend => dev.stream(&r.canvas, &frame, None),
Some(last) => dev.stream(&r.canvas, &frame, Some(last)),
};
Expand Down Expand Up @@ -281,7 +294,8 @@ impl Studio {
#[cfg(test)]
mod tests {
use super::*;
use secbird_core::sim::{Sim, WIRELESS_MOUSE_PID};
use secbird_core::commands as cmd;
use secbird_core::sim::{Sim, KEYBOARD_PID, WIRELESS_MOUSE_PID};
use secbird_core::studio::Layer;

/// A wireless device is skipped on every tick but each [`WIRELESS_EVERY`]th. The battery's
Expand All @@ -308,4 +322,100 @@ mod tests {
assert_eq!((studio.tick, studio.running[&key].battery), (tick, Some(level)));
}
}

/// A wireless keyboard that dropped back to its own lighting gets the frame again when its
/// scene shows the battery: its stream starts over at the next battery read it answers.
/// Before, it only got rows that changed, so an unchanged level never came back.
#[test]
fn a_wireless_keyboard_gets_its_whole_frame_again() {
let (sim, devices, mut studio) = wireless_keyboard_streaming(&["static:00FF00", "battery"]);
let no_profiles = std::path::Path::new("no-profiles.json");

// Ticks 1801 and 1802 send nothing; on 1803 the battery is read (still 50 %) and the
// stream starts over: six rows, then the effect.
studio.tick(&devices, None, no_profiles, false, false);
studio.tick(&devices, None, no_profiles, false, false);
assert!(sim.state().log.is_empty());
studio.tick(&devices, None, no_profiles, false, false);
{
let s = sim.state();
assert_eq!(s.effects[&0][0], cmd::EFFECT_CUSTOM_FRAME);
assert_eq!(s.log.iter().filter(|c| **c == cmd::MATRIX_FRAME).count(), 6);
assert_eq!(s.log.last(), Some(&cmd::MATRIX_EFFECT));
assert_eq!(s.key(5, 0), Some([0x80, 0xFF, 0]), "the bar's first key, yellow-green at 50 %");
}
// Once a minute only: 1806, the next tick the keyboard gets, sends nothing again.
sim.state().log.clear();
for _ in 0..3 {
studio.tick(&devices, None, no_profiles, false, false);
}
assert!(sim.state().log.is_empty());
}

/// A scene that doesn't show the battery gets no refresh: it would add a radio wake-up.
#[test]
fn a_still_wireless_scene_is_left_alone() {
let (sim, devices, mut studio) = wireless_keyboard_streaming(&["static:00FF00"]);
let no_profiles = std::path::Path::new("no-profiles.json");
for _ in 0..3 {
studio.tick(&devices, None, no_profiles, false, false);
}
let s = sim.state();
assert!(s.log.is_empty());
assert_eq!(s.effects[&0][0], cmd::EFFECT_STATIC);
}

/// A keyboard that doesn't answer the battery read (switched off, out of reach) is not
/// sent a frame and counts no failure: its stream stays as it is until it answers again.
#[test]
fn a_wireless_keyboard_that_does_not_answer_is_left_alone() {
let (sim, devices, mut studio) = wireless_keyboard_streaming(&["static:00FF00", "battery"]);
sim.state().battery = None;
let no_profiles = std::path::Path::new("no-profiles.json");
for _ in 0..3 {
studio.tick(&devices, None, no_profiles, false, false);
}
let s = sim.state();
assert!(!s.log.contains(&cmd::MATRIX_FRAME) && !s.log.contains(&cmd::MATRIX_EFFECT));
assert_eq!(s.effects[&0][0], cmd::EFFECT_STATIC);
let r = &studio.running[&devices[0].key()];
assert!(r.last.is_some() && r.failures == 0 && r.battery == Some(50));
}

/// The stream starts over with replies, as at any start: a keyboard that refuses a row stops
/// it there and counts as a failed frame. Sent without waiting for replies, rows got lost on
/// the way to a real keyboard and nothing noticed.
#[test]
fn a_refused_row_ends_the_refresh_as_a_failed_frame() {
let (sim, devices, mut studio) = wireless_keyboard_streaming(&["static:00FF00", "battery"]);
sim.state().matrix = None; // the battery still answers, frame rows are refused
let no_profiles = std::path::Path::new("no-profiles.json");
for _ in 0..3 {
studio.tick(&devices, None, no_profiles, false, false);
}
let s = sim.state();
assert_eq!(s.log.iter().filter(|c| **c == cmd::MATRIX_FRAME).count(), 1);
assert!(!s.log.contains(&cmd::MATRIX_EFFECT));
let r = &studio.running[&devices[0].key()];
assert!(r.last.is_none() && r.failures == 1);
}

/// The simulated keyboard as a wireless one at 50 %, streaming a scene of these layers
/// since a minute ago, then dropped back to its own lighting. The counter is at 1800.
fn wireless_keyboard_streaming(layers: &[&str]) -> (Sim, [Device; 1], Studio) {
let sim = Sim::keyboard();
sim.state().battery = Some(128);
let spec = Sim::spec_for(KEYBOARD_PID).map(|s| secbird_core::DeviceSpec { wireless: true, ..s });
let dev = sim.open(KEYBOARD_PID, spec).expect("the simulated keyboard");
let layers = layers.iter().map(|l| Layer::parse(&[l.to_string()]).expect("a layer")).collect();
let scene = Scene { layers };
let canvas = Canvas::for_device(&dev, &[]);
let frame = render(&scene, &canvas, 0.0, &Inputs { battery: Some(50), ..Inputs::default() });
dev.start_stream(&canvas, &frame).expect("the stream starts");
let mut studio = Studio { dirty: false, tick: BATTERY_EVERY, ..Studio::default() };
studio.running.insert(dev.key(), Running { profile: "Default".into(), scene, canvas, last: Some(frame), failures: 0, wireless: true, battery: Some(50) });
sim.state().effects.insert(0, vec![cmd::EFFECT_STATIC]);
sim.state().log.clear();
(sim, [dev], studio)
}
}
Loading