Adds the driver half of the v2 controller link as extra 0x4C message types: DATA with fragment reassembly, de-duplication and acks, a state-version digest with jittered resync requests, a bench echo, and a BEAT frame handled by BeatSync (accepts one of the two copies, free-runs on the last period when a beat is missed). Presets.beat() steps the selected pattern without a message. Optional settings: espnow_loss, bench, beat_pin, usb_log. The working tree also carries the files already running on the bench device, which were ahead of origin/p2p: board.py (per-chip LED pin and CPU speed), the reversed device_config flag in controller_messages.py, and the apply_cpu_freq call in main.py. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
92 lines
2.3 KiB
Python
92 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""BeatSync accepts exactly one of the two copies of each beat - runs on MicroPython."""
|
|
import struct
|
|
from beat_sync import BeatSync
|
|
|
|
|
|
def frame(idx, period=500, fire_in=30, flags=0):
|
|
return struct.pack("<BBBHHB", 0x4C, 0x25, flags, idx, period, fire_in)
|
|
|
|
|
|
def run(events, tick=1):
|
|
"""events: (time_ms, frame_bytes). Returns the number of fires."""
|
|
b = BeatSync()
|
|
fires = 0
|
|
t = 0
|
|
end = events[-1][0] + 200
|
|
ev = list(events)
|
|
while t < end:
|
|
while ev and ev[0][0] <= t:
|
|
b.on_frame(ev.pop(0)[1], t)
|
|
if b.poll(t):
|
|
fires += 1
|
|
t += tick
|
|
return fires
|
|
|
|
|
|
def test_both_copies():
|
|
assert run([(0, frame(1)), (2, frame(1, fire_in=28))]) == 1
|
|
|
|
|
|
def test_only_first():
|
|
assert run([(0, frame(1))]) == 1
|
|
|
|
|
|
def test_only_second():
|
|
assert run([(2, frame(1, fire_in=28))]) == 1
|
|
|
|
|
|
def test_wraparound():
|
|
b = BeatSync()
|
|
assert b.on_frame(frame(0xFFFF), 0)
|
|
assert not b.on_frame(frame(0xFFFF), 2)
|
|
assert b.on_frame(frame(0), 500)
|
|
|
|
|
|
def test_late_old_copy_ignored():
|
|
b = BeatSync()
|
|
assert b.on_frame(frame(5), 0)
|
|
assert b.on_frame(frame(6), 500)
|
|
assert not b.on_frame(frame(5), 502)
|
|
|
|
|
|
def test_two_beats_two_fires():
|
|
ev = [(0, frame(1)), (2, frame(1)), (500, frame(2)), (502, frame(2))]
|
|
assert run(ev) == 2
|
|
|
|
|
|
def test_free_run_covers_missed_beat():
|
|
# beat 3 lost entirely: the driver still fires about once per period
|
|
ev = [(0, frame(1)), (2, frame(1)), (500, frame(2)), (502, frame(2)), (1500, frame(4)), (1502, frame(4))]
|
|
assert run(ev) == 4
|
|
|
|
|
|
def test_bad_frames():
|
|
b = BeatSync()
|
|
assert not b.on_frame(b"\x00" * 8, 0)
|
|
assert not b.on_frame(b"\xb1\x00", 0)
|
|
|
|
|
|
def test_freerun_flag_marks_invented_beats():
|
|
b = BeatSync()
|
|
b.on_frame(frame(1), 0)
|
|
assert b.poll(40) is True and b.freerun_fire is False
|
|
assert b.poll(700) is True and b.freerun_fire is True
|
|
|
|
|
|
def test_stats_count_dups_and_freerun():
|
|
b = BeatSync()
|
|
b.on_frame(frame(1), 0)
|
|
b.on_frame(frame(1), 2)
|
|
b.poll(40)
|
|
assert b.stats["fires"] == 1 and b.stats["dups"] == 1
|
|
b.poll(700)
|
|
assert b.stats["freerun"] == 1 and b.stats["fires"] == 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for name, fn in list(globals().items()):
|
|
if name.startswith("test_"):
|
|
fn()
|
|
print("ok", name)
|