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:
1 parent
c3a42cf35b
commit
edba341f30
5 files changed
+205
-11
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from lcdkit import BG
|
||||
from regions import changed_regions
|
||||
|
||||
|
||||
def blank(w=480, h=320):
|
||||
return Image.new("RGB", (w, h), BG)
|
||||
|
||||
|
||||
def with_pixels(points, colour=(0, 0, 0)):
|
||||
img = blank()
|
||||
for p in points:
|
||||
img.putpixel(p, colour)
|
||||
return img
|
||||
|
||||
|
||||
def test_identical_images_have_no_changed_regions():
|
||||
assert changed_regions(blank(), blank()) == []
|
||||
|
||||
|
||||
def test_single_pixel_gives_a_tight_box():
|
||||
assert changed_regions(blank(), with_pixels([(100, 50)])) == [(100, 50, 101, 51)]
|
||||
|
||||
|
||||
def test_far_apart_changes_are_separate_boxes():
|
||||
boxes = changed_regions(blank(), with_pixels([(10, 10), (400, 250)]))
|
||||
assert boxes == [(10, 10, 11, 11), (400, 250, 401, 251)]
|
||||
|
||||
|
||||
def test_neighbouring_tiles_in_a_row_merge_into_one_box():
|
||||
# x=10 is in tile 0 and x=40 in tile 1 (32 px tiles): one run, tight bounding box
|
||||
assert changed_regions(blank(), with_pixels([(10, 10), (40, 12)])) == [(10, 10, 41, 13)]
|
||||
|
||||
|
||||
def test_boxes_always_cover_every_changed_pixel():
|
||||
rng = np.random.default_rng(7)
|
||||
for _ in range(20):
|
||||
points = [(int(rng.integers(0, 480)), int(rng.integers(0, 320))) for _ in range(int(rng.integers(1, 12)))]
|
||||
new = with_pixels(points)
|
||||
boxes = changed_regions(blank(), new)
|
||||
for x, y in points:
|
||||
assert any(x0 <= x < x1 and y0 <= y < y1 for x0, y0, x1, y1 in boxes), (x, y, boxes)
|
||||
for x0, y0, x1, y1 in boxes: # and stay inside the image
|
||||
assert 0 <= x0 < x1 <= 480 and 0 <= y0 < y1 <= 320
|
||||
|
||||
|
||||
def test_size_mismatch_is_an_error():
|
||||
with pytest.raises(ValueError):
|
||||
changed_regions(blank(480, 320), blank(320, 480))
|
||||
+82
-5
@@ -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)]
|
||||
Reference in new issue
Block a user