firmament: generalize the slot loop to any frame period
Assisted-by: Claude Opus 5.5 via Claude Code
change zwtstypxwxrvpuqnormlvyrwxyrxpplx
commit 8f609aa1948338022095150e3f4d33403ee304c8
author Alpha Chen <alpha@kejadlen.dev>
date
parent zrxonsov
diff --git a/firmament/src/firmament/cli.py b/firmament/src/firmament/cli.py
index 8a48347..057bd86 100644
--- a/firmament/src/firmament/cli.py
+++ b/firmament/src/firmament/cli.py
@@ -24,29 +24,30 @@ from firmament.pattern import (
 LIGHT_RESEND_SECONDS = 1.0
 
 
-def play(
+def run(
     send: Callable[[bytes], None],
-    frame_for_slot: Callable[[int], bytes],
+    frame_for_tick: Callable[[int], bytes],
+    period: float,
     *,
-    on_slot: Callable[[int], None] = lambda _: None,
+    on_tick: Callable[[int], None] = lambda _: None,
     on_rebase: Callable[[float], None] = lambda _: None,
     clock: Callable[[], float] = time.monotonic,
     sleep: Callable[[float], None] = time.sleep,
     limit: int | None = None,
 ) -> None:
-    """Loop the pattern, one slot per SLOT_SECONDS, until interrupted or `limit` slots."""
+    """Send one frame per `period` seconds, until interrupted or `limit` ticks."""
     next_at = clock()
-    slots = count() if limit is None else range(limit)
-    for k in slots:
-        slot = k % SLOT_COUNT
-        send(frame_for_slot(slot))
-        on_slot(slot)
-        next_at += SLOT_SECONDS
+    ticks = count() if limit is None else range(limit)
+    for k in ticks:
+        send(frame_for_tick(k))
+        on_tick(k)
+        next_at += period
         lag = clock() - next_at
         if lag <= 0:
             sleep(-lag)
-        elif lag > SLOT_SECONDS / 2:
-            # Catching up would leave the next slot too short to decode, so start a fresh schedule.
+        elif lag > period / 2:
+            # Catching up would leave the next frame too short (for the pattern, too short to
+            # decode), so start a fresh schedule.
             on_rebase(lag)
             next_at = clock()
 
@@ -110,9 +111,9 @@ def _frames(args: argparse.Namespace) -> list[bytes]:
 
 
 def _play(frames: list[bytes], sender: DdpSender, log: TextIO | None) -> None:
-    def on_slot(slot: int) -> None:
+    def on_tick(k: int) -> None:
         if log:
-            log.write(json.dumps({"t": time.time(), "slot": slot}) + "\n")
+            log.write(json.dumps({"t": time.time(), "slot": k % SLOT_COUNT}) + "\n")
 
     def on_rebase(lag: float) -> None:
         print(
@@ -120,7 +121,13 @@ def _play(frames: list[bytes], sender: DdpSender, log: TextIO | None) -> None:
         )
 
     try:
-        play(sender.send, frames.__getitem__, on_slot=on_slot, on_rebase=on_rebase)
+        run(
+            sender.send,
+            lambda k: frames[k % SLOT_COUNT],
+            SLOT_SECONDS,
+            on_tick=on_tick,
+            on_rebase=on_rebase,
+        )
     finally:
         if log:
             log.close()
diff --git a/firmament/tests/test_cli.py b/firmament/tests/test_cli.py
index 41c294b..aac3703 100644
--- a/firmament/tests/test_cli.py
+++ b/firmament/tests/test_cli.py
@@ -3,9 +3,8 @@ from collections.abc import Iterator
 
 import pytest
 
-from firmament.cli import main, play
+from firmament.cli import main, run
 from firmament.ddp import Header
-from firmament.pattern import SLOT_COUNT, SLOT_SECONDS
 
 
 @pytest.fixture
@@ -49,37 +48,43 @@ class FakeClock:
         self.now += seconds
 
 
-def test_play_cycles_through_slots_in_order():
+PERIOD = 0.150
+
+
+def test_run_sends_each_ticks_frame_in_order():
     clock = FakeClock()
-    slots: list[int] = []
-    play(
-        lambda _: None,
-        lambda s: bytes([s]),
-        on_slot=slots.append,
+    sent: list[bytes] = []
+    ticks: list[int] = []
+    run(
+        sent.append,
+        lambda k: bytes([k]),
+        PERIOD,
+        on_tick=ticks.append,
         clock=clock,
         sleep=clock.sleep,
         limit=30,
     )
-    assert slots == [k % SLOT_COUNT for k in range(30)]
+    assert sent == [bytes([k]) for k in range(30)]
+    assert ticks == list(range(30))
 
 
-def test_play_holds_each_slot_for_slot_seconds():
+def test_run_holds_each_frame_for_one_period():
     clock = FakeClock()
-    play(lambda _: None, lambda _: bytes(3), clock=clock, sleep=clock.sleep, limit=5)
-    assert clock.sleeps == pytest.approx([SLOT_SECONDS] * 5)
+    run(lambda _: None, lambda _: bytes(3), PERIOD, clock=clock, sleep=clock.sleep, limit=5)
+    assert clock.sleeps == pytest.approx([PERIOD] * 5)
 
 
-def test_play_absorbs_small_lag_without_drift():
+def test_run_absorbs_small_lag_without_drift():
     clock = FakeClock()
 
     def slow_send(_: bytes) -> None:
         clock.now += 0.010
 
-    play(slow_send, lambda _: bytes(3), clock=clock, sleep=clock.sleep, limit=10)
-    assert clock.now == pytest.approx(10 * SLOT_SECONDS)
+    run(slow_send, lambda _: bytes(3), PERIOD, clock=clock, sleep=clock.sleep, limit=10)
+    assert clock.now == pytest.approx(10 * PERIOD)
 
 
-def test_play_restarts_schedule_after_a_long_stall():
+def test_run_restarts_schedule_after_a_long_stall():
     clock = FakeClock()
     rebases: list[float] = []
     sends = 0
@@ -90,16 +95,17 @@ def test_play_restarts_schedule_after_a_long_stall():
         if sends == 2:
             clock.now += 1.0
 
-    play(
+    run(
         stalling_send,
         lambda _: bytes(3),
+        PERIOD,
         on_rebase=rebases.append,
         clock=clock,
         sleep=clock.sleep,
         limit=4,
     )
-    assert rebases == [pytest.approx(1.0 - SLOT_SECONDS)]
-    assert clock.sleeps[-2:] == pytest.approx([SLOT_SECONDS] * 2)
+    assert rebases == [pytest.approx(1.0 - PERIOD)]
+    assert clock.sleeps[-2:] == pytest.approx([PERIOD] * 2)
 
 
 def test_light_rejects_index_beyond_led_count(capsys):