Send only the changed regions of each frame

The link moves ~150 KB/s, so a full frame takes 2 s. After the first frame the runner
diffs against what the screen shows and sends tight boxes; full frame on big changes,
after a failed push, and every 15 minutes.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Sonnet 5.5 committed 2026-10-22 14:25:09 +02:00
1 parent c3a42cf35b
commit edba341f30
5 files changed
+205 -11

No files matched your search

+82 -5
View File
@@ -25,9 +25,10 @@ class FakeLcd:
class StubView:
name = "stub"
def __init__(self, keys, with_patch=True):
def __init__(self, keys, with_patch=True, render_fn=None):
self._keys = keys # callable(now) -> key
self._with_patch = with_patch
self._render_fn = render_fn # callable(now) -> Image, default: a blank frame
self.renders = 0
def start(self): ...
@@ -38,12 +39,19 @@ class StubView:
def render(self, now):
self.renders += 1
return Image.new("RGB", (480, 320), BG)
return self._render_fn(now) if self._render_fn else Image.new("RGB", (480, 320), BG)
def patch(self, now):
return Patch(Image.new("RGB", (10, 5), BG), 100, 50) if self._with_patch else None
def marker(x, y, size=10):
"""A blank frame with one dark box; moving it between frames is a small change."""
img = Image.new("RGB", (480, 320), BG)
img.paste((0, 0, 0), (x, y, x + size, y + size))
return img
def ticks(n):
"""running() that is true for the first n calls."""
state = {"left": n}
@@ -83,10 +91,12 @@ def test_run_pushes_patch_each_new_second_only():
assert lcd.calls == [((480, 320), 0, 0), ((10, 5), 100, 50), ((10, 5), 100, 50)]
def test_run_pushes_full_frame_when_key_changes():
lcd, view = FakeLcd(), StubView(lambda now: now.minute)
def test_run_pushes_only_the_changed_region_when_the_key_changes():
view = StubView(lambda now: now.minute, render_fn=lambda now: marker(now.minute * 10, 40))
lcd = FakeLcd()
runner.run(view, lcd, now_fn=clock(T0, 60), running=ticks(3), sleep=lambda s: None)
assert [c[0] for c in lcd.calls] == [(480, 320)] * 3
assert lcd.calls[0] == ((480, 320), 0, 0) # first frame: everything
assert lcd.calls[1:] == [((20, 10), 320, 40), ((20, 10), 330, 40)] # then just the box that moved
def test_run_retries_failed_draw_and_continues():
@@ -163,3 +173,70 @@ def test_run_counts_library_sys_exit_as_a_failed_draw():
with pytest.raises(SystemExit): # after max_failures; Docker then restarts and open_screen retries
runner.run(view, ExitingLcd(), now_fn=clock(T0, 0), running=ticks(50), sleep=lambda s: None, max_failures=3)
assert view.renders == 3 # it tried three times first
def test_run_sends_everything_when_most_of_the_screen_changed():
frames = iter([Image.new("RGB", (480, 320), BG), Image.new("RGB", (480, 320), (0, 0, 0))])
view = StubView(lambda now: now.minute, render_fn=lambda now: next(frames))
lcd = FakeLcd()
runner.run(view, lcd, now_fn=clock(T0, 60), running=ticks(2), sleep=lambda s: None)
assert lcd.calls == [((480, 320), 0, 0), ((480, 320), 0, 0)]
def test_run_sends_a_full_frame_again_after_the_refresh_interval():
view = StubView(lambda now: now.minute, render_fn=lambda now: marker(now.minute * 10, 40))
lcd = FakeLcd()
elapsed = {32: 0.0, 33: 10.0, 34: 1000.0} # seconds since start, by the minute being shown
t = {"now": 0.0}
def now_fn():
now = T0 + timedelta(minutes=t["tick"])
t["tick"] += 1
t["now"] = elapsed[now.minute]
return now
t["tick"] = 0
runner.run(view, lcd, now_fn=now_fn, running=ticks(3), sleep=lambda s: None,
full_refresh_s=900.0, monotonic=lambda: t["now"])
sizes = [c[0] for c in lcd.calls]
assert sizes == [(480, 320), (20, 10), (480, 320)] # the third frame is 1000 s after the last full one
def test_run_sends_a_full_frame_after_a_failed_push():
view = StubView(lambda now: now.minute, render_fn=lambda now: marker(now.minute * 10, 40))
class FailsOnSecondCall(FakeLcd):
def DisplayPILImage(self, image, x=0, y=0, image_width=None, image_height=None):
if len(self.calls) == 1 and not getattr(self, "failed", False):
self.failed = True
raise OSError("serial hiccup")
super().DisplayPILImage(image, x, y)
lcd = FailsOnSecondCall()
runner.run(view, lcd, now_fn=clock(T0, 60), running=ticks(3), sleep=lambda s: None)
assert [c[0] for c in lcd.calls] == [(480, 320), (480, 320)] # the screen state was unknown, so everything again
def test_run_does_not_resend_what_a_patch_already_drew():
state = {"n": 0}
def render(now):
state["n"] += 1
img = marker(300, 200) # something that never changes
img.paste((50, 50, 50), (100, 50, 110, 55)) # where the view's patch draws its seconds
if state["n"] == 2:
img.paste((0, 0, 0), (10, 10, 20, 20)) # the one real change
return img
class PatchView(StubView):
def patch(self, now):
return Patch(Image.new("RGB", (10, 5), (50, 50, 50)), 100, 50)
view = PatchView(lambda now: now.minute, render_fn=render)
lcd = FakeLcd()
seconds = iter([0, 1, 0]) # tick1 full; tick2 same key new second -> patch; tick3 new key
minutes = iter([0, 0, 1])
now_fn = lambda: T0.replace(minute=next(minutes), second=next(seconds))
runner.run(view, lcd, now_fn=now_fn, running=ticks(3), sleep=lambda s: None)
# full frame, the patch, then only the changed box: the patch region was already up to date
assert lcd.calls == [((480, 320), 0, 0), ((10, 5), 100, 50), ((10, 10), 10, 10)]