From 77f795903e7c86b978e107a841202abda16d3c4e Mon Sep 17 00:00:00 2001 From: cdnutter Date: Thu, 13 Aug 2026 18:43:48 -0700 Subject: [PATCH] Add temporary diagnostics for LED flicker investigation Logs (WARNING level) when: multiple heartbeats arrive in one serial read batch, a heartbeat gap exceeds 0.5s, or reapply_app_volumes takes >0.15s. Suspected cause: synchronous playerctl/pulsectl calls in the reapply path occasionally stalling the main loop long enough for backed-up heartbeats to flush in a burst, each triggering an LED resend. To be removed once confirmed/fixed. --- src/turnup/turnupd.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/turnup/turnupd.py b/src/turnup/turnupd.py index 5aabb4f..9c2c33f 100755 --- a/src/turnup/turnupd.py +++ b/src/turnup/turnupd.py @@ -421,11 +421,20 @@ def main() -> None: send_leds(ser, initial_colors) last_led_colors[:] = initial_colors + heartbeat_count = 0 + last_heartbeat_ts = 0.0 + while True: data = ser.read(64) if data: buf.extend(data) messages, buf = parse_messages(buf) + hb_in_batch = sum(1 for m in messages if m["type"] == "heartbeat") + if hb_in_batch > 1: + log.warning( + "DIAG: %d heartbeats in one read batch (backlog flush)", + hb_in_batch, + ) for msg in messages: if msg["type"] == "knob": handle_knob( @@ -439,6 +448,14 @@ def main() -> None: msg["id"], msg["action"], config, pulse, active_window ) elif msg["type"] == "heartbeat": + now_hb = time.monotonic() + heartbeat_count += 1 + if last_heartbeat_ts and (now_hb - last_heartbeat_ts) > 0.5: + log.warning( + "DIAG: heartbeat gap %.2fs before heartbeat #%d", + now_hb - last_heartbeat_ts, heartbeat_count, + ) + last_heartbeat_ts = now_hb new_colors = all_led_colors(config, knob_norms) send_leds(ser, new_colors) last_led_colors[:] = new_colors @@ -467,7 +484,11 @@ def main() -> None: knob_quiet = now - last_knob_event[0] >= 0.2 if (pulse.drain_events() or now - last_reapply >= 1.0) and knob_quiet: last_reapply = now + _t0 = time.monotonic() reapply_app_volumes(config, pulse, knob_norms) + _elapsed = time.monotonic() - _t0 + if _elapsed > 0.15: + log.warning("DIAG: reapply_app_volumes took %.3fs", _elapsed) except serial.SerialException as exc: log.warning("Serial error: %s — retrying in 3 s", exc)