A run of three consecutive indices that all look old means the controller rebooted and restarted its counter, so resync to it; one or two stray old frames are still dropped as late copies. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
113 lines
3.1 KiB
Python
113 lines
3.1 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
|
|
|
|
|
|
def test_controller_restart_is_followed():
|
|
b = BeatSync()
|
|
assert b.on_frame(frame(29000), 0)
|
|
assert not b.on_frame(frame(1), 500) # looks old
|
|
assert not b.on_frame(frame(1), 502) # second copy of the same beat
|
|
assert not b.on_frame(frame(2), 1000)
|
|
assert b.on_frame(frame(3), 1500) # third consecutive: the counter really restarted
|
|
assert not b.on_frame(frame(3), 1502)
|
|
assert b.on_frame(frame(4), 2000)
|
|
|
|
|
|
def test_stray_old_frames_do_not_reset():
|
|
b = BeatSync()
|
|
assert b.on_frame(frame(100), 0)
|
|
assert not b.on_frame(frame(90), 100)
|
|
assert b.on_frame(frame(101), 500)
|
|
assert not b.on_frame(frame(99), 600)
|
|
assert not b.on_frame(frame(98), 700) # not consecutive upward
|
|
assert b.on_frame(frame(102), 1000)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for name, fn in list(globals().items()):
|
|
if name.startswith("test_"):
|
|
fn()
|
|
print("ok", name)
|