Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/lcd/lcd_comm_rev_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,15 @@ def DisplayPILImage(
payload=bytearray(self._generate_full_image(image)),
readsize=1024)
self._send_command(Command.QUERY_STATUS, readsize=1024)
# The display expects the render count of partial updates to restart at 0 after each full frame
Count.Start = 0
else:
with self.update_queue_mutex:
img, pyd = self._generate_update_image(image, x, y, Count.Start, Command.UPDATE_BITMAP)
self._send_command(Command.SEND_PAYLOAD, payload=pyd)
self._send_command(Command.SEND_PAYLOAD, payload=img)
self._send_command(Command.QUERY_STATUS, readsize=1024)
Count.Start += 1
Count.Start += 1

def _generate_full_image(self, image: Image.Image) -> bytes:
if self.sub_revision == SubRevision.REV_8INCH:
Expand Down Expand Up @@ -460,8 +462,9 @@ def _generate_update_image(
payload.extend(Padding.NULL.value * 3)
payload.extend(count.to_bytes(4, 'big'))

if len(img_raw_data) > 250:
img_raw_data = bytearray(b'\x00').join(chunked(bytes(img_raw_data), 249))
# The display reads 249 data bytes per 250-byte packet, the last byte of each packet being a separator.
# The "ef69" terminator is part of this data stream, so it must be added before chunking.
img_raw_data += b'\xef\x69'
img_raw_data = bytearray(b'\x00').join(chunked(bytes(img_raw_data), 249))

return img_raw_data, payload
115 changes: 115 additions & 0 deletions tests/library/lcd/test_lcd_comm_rev_c_update_framing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import unittest

from PIL import Image

from library.lcd import lcd_comm_rev_c
from library.lcd.lcd_comm_rev_c import LcdCommRevC, Orientation, SubRevision, Command

from .serial_mock import new_testing_serial


class MockedLcdCommRevC(LcdCommRevC):
def openSerial(self):
self.lcd_serial = new_testing_serial()


def new_lcd(sub_revision: SubRevision, rom_version: int, width: int, height: int) -> MockedLcdCommRevC:
lcd = MockedLcdCommRevC(display_width=width, display_height=height)
lcd.sub_revision = sub_revision
lcd.rom_version = rom_version
lcd.SetOrientation(Orientation.LANDSCAPE) # no image rotation
lcd.lcd_serial.reset_mock()
return lcd


def device_parse(body: bytes, size: int) -> bytes:
"""Model of the display firmware: the body is sent in 250-byte packets zero-padded; only the first 249 bytes of
each packet are data, the 250th is a separator. `size` data bytes are read (the "ef69" terminator included)."""
if len(body) % 250:
body += bytes(250 - len(body) % 250)
stream = b"".join(body[i:i + 249] for i in range(0, len(body), 250))
return stream[:size]


def update_for(lcd, width: int, height: int):
"""Run the real _generate_update_image and return (body, header, raw_length)."""
image = Image.new("RGB", (width, height), (10, 20, 30))
body, header = lcd._generate_update_image(image, 0, 0, 0, Command.UPDATE_BITMAP)
size = int.from_bytes(header[4:7], "big")
return bytes(body), bytes(header), size - 2


class TestLcdCommRevCUpdateFraming(unittest.TestCase):
def test_terminator_survives_for_any_raw_length(self):
# With the 2.1" 3-byte pixel encoding, raw length = height * (3 + 2 + 3 * width)
lcd = new_lcd(SubRevision.REV_2INCH, 89, 480, 480)
covered = set()
sizes = [(w, h) for w in range(1, 300) for h in range(1, 9)]
sizes += [(480, h) for h in range(150, 161)] # ~227 KB, close to a quarter of the screen
for w, h in sizes:
body, header, n = update_for(lcd, w, h)
covered.add(n)
with self.subTest(width=w, height=h, raw_length=n):
self.assertEqual(n, h * (5 + 3 * w))
data = device_parse(body, n + 2)
self.assertEqual(data[-2:], b"\xef\x69")
self.assertEqual(len(data), n + 2)
# The data (without the separators) is the raw records followed by the terminator
self.assertEqual(data[:5], bytes(3) + w.to_bytes(2, "big"))
# Raw lengths that used to be framed wrongly (248, 249, 250, then 497, 498 and so on) must be covered
for n in (248, 249, 250, 251, 497, 498, 746, 1992):
self.assertTrue(n in covered, f"raw length {n} not covered")
self.assertTrue(any(226000 < n < 228000 for n in covered))

def test_header_layout_and_chunking(self):
lcd = new_lcd(SubRevision.REV_2INCH, 89, 480, 480)
img = Image.new("RGB", (10, 2), (1, 2, 3))
body, header = lcd._generate_update_image(img, 4, 6, 9, Command.UPDATE_BITMAP)
# 2 lines of (3 pos + 2 width + 30 colour) bytes + 2 terminator bytes
self.assertEqual(bytes(header), bytes.fromhex("cc ef 69 00") + (72).to_bytes(3, "big") + bytes(3)
+ (9).to_bytes(4, "big"))
self.assertEqual(bytes(body)[-2:], b"\xef\x69")
self.assertEqual(len(body), 72)

def test_all_models_use_same_framing(self):
for sub_revision, rom, width, height in (
(SubRevision.REV_2INCH, 89, 480, 480),
(SubRevision.REV_5INCH, 89, 480, 800),
(SubRevision.REV_5INCH, 87, 480, 800),
(SubRevision.REV_8INCH, 89, 480, 1920)):
lcd = new_lcd(sub_revision, rom, width, height)
for w, h in ((1, 1), (28, 3), (83, 1), (160, 2), (200, 100)):
with self.subTest(sub_revision=sub_revision, rom=rom, size=(w, h)):
body, header, n = update_for(lcd, w, h)
size = n + 2
data = device_parse(bytes(body), size)
self.assertEqual(len(data), size)
self.assertEqual(data[-2:], b"\xef\x69")
# The separator bytes must not be part of the data: de-chunked data is body without separators
self.assertEqual(len(body), size + (size - 1) // 249)

def test_count_restarts_after_full_frame_and_increments_per_update(self):
lcd = new_lcd(SubRevision.REV_2INCH, 89, 480, 480)
full = Image.new("RGB", (480, 480))
patch = Image.new("RGB", (20, 10))
lcd_comm_rev_c.Count.Start = 5
lcd.DisplayPILImage(full)
self.assertEqual(lcd_comm_rev_c.Count.Start, 0)

counts = []
for _ in range(3):
lcd.lcd_serial.reset_mock()
lcd.DisplayPILImage(patch, x=0, y=0, image_width=20, image_height=10)
header = lcd.lcd_serial.mock_calls[0].args[0]
counts.append(int.from_bytes(header[10:14], "big"))
self.assertEqual(counts, [0, 1, 2])
self.assertEqual(lcd_comm_rev_c.Count.Start, 3)

lcd.DisplayPILImage(full)
lcd.lcd_serial.reset_mock()
lcd.DisplayPILImage(patch, x=0, y=0, image_width=20, image_height=10)
self.assertEqual(int.from_bytes(lcd.lcd_serial.mock_calls[0].args[0][10:14], "big"), 0)


if __name__ == "__main__":
unittest.main()