parent
e09431c3b8
commit
bb3edb3b8b
@ -0,0 +1,96 @@ |
||||
"""Treiber für den Turing Smart Screen 3.5" (Revision A). |
||||
|
||||
Protokoll nach turing-smart-screen-python (library/lcd/lcd_comm_rev_a.py), siehe docs/recherche.md. |
||||
""" |
||||
|
||||
from collections.abc import Callable |
||||
from enum import IntEnum |
||||
|
||||
import numpy as np |
||||
import serial |
||||
from PIL import Image |
||||
|
||||
WIDTH, HEIGHT = 320, 480 |
||||
BAUDRATE = 115200 |
||||
CHUNK_SIZE = WIDTH * 8 |
||||
PORTRAIT = 0 |
||||
|
||||
|
||||
class Cmd(IntEnum): |
||||
SET_BRIGHTNESS = 110 |
||||
SET_ORIENTATION = 121 |
||||
DISPLAY_BITMAP = 197 |
||||
|
||||
|
||||
def encode_header(cmd: int, x: int, y: int, ex: int, ey: int) -> bytes: |
||||
return bytes([ |
||||
x >> 2, |
||||
((x & 3) << 6) + (y >> 4), |
||||
((y & 15) << 4) + (ex >> 6), |
||||
((ex & 63) << 2) + (ey >> 8), |
||||
ey & 255, |
||||
cmd, |
||||
]) |
||||
|
||||
|
||||
def encode_orientation_portrait() -> bytes: |
||||
packet = bytearray(16) |
||||
packet[0:6] = encode_header(Cmd.SET_ORIENTATION, 0, 0, 0, 0) |
||||
packet[6] = PORTRAIT + 100 |
||||
packet[7:9] = WIDTH.to_bytes(2, "big") |
||||
packet[9:11] = HEIGHT.to_bytes(2, "big") |
||||
return bytes(packet) |
||||
|
||||
|
||||
def encode_brightness(percent: int) -> bytes: |
||||
if not 0 <= percent <= 100: |
||||
raise ValueError(f"Helligkeit muss zwischen 0 und 100 liegen, ist {percent}") |
||||
# Das Display erwartet 0 = am hellsten … 255 = dunkel |
||||
return encode_header(Cmd.SET_BRIGHTNESS, int(255 - percent / 100 * 255), 0, 0, 0) |
||||
|
||||
|
||||
def to_rgb565le(image: Image.Image) -> bytes: |
||||
rgb = np.asarray(image.convert("RGB"), dtype=np.uint16) |
||||
r = (rgb[..., 0] >> 3) << 11 |
||||
g = (rgb[..., 1] >> 2) << 5 |
||||
b = rgb[..., 2] >> 3 |
||||
return (r | g | b).astype("<u2").tobytes() |
||||
|
||||
|
||||
class Lcd: |
||||
def __init__(self, port: str, serial_factory: Callable[..., serial.Serial] = serial.Serial): |
||||
self.port = port |
||||
self._serial_factory = serial_factory |
||||
self._serial = None |
||||
|
||||
def open(self) -> None: |
||||
self.close() |
||||
self._serial = self._serial_factory(self.port, BAUDRATE, timeout=1, rtscts=True, write_timeout=5) |
||||
|
||||
def close(self) -> None: |
||||
if self._serial is not None: |
||||
try: |
||||
self._serial.close() |
||||
except OSError: |
||||
pass |
||||
self._serial = None |
||||
|
||||
def set_orientation_portrait(self) -> None: |
||||
self._write(encode_orientation_portrait()) |
||||
|
||||
def set_brightness(self, percent: int) -> None: |
||||
self._write(encode_brightness(percent)) |
||||
|
||||
def show(self, image: Image.Image, x: int, y: int) -> None: |
||||
w, h = image.size |
||||
if x < 0 or y < 0 or w <= 0 or h <= 0 or x + w > WIDTH or y + h > HEIGHT: |
||||
raise ValueError(f"Bildbereich außerhalb des Displays: x={x} y={y} {w}x{h}") |
||||
self._write(encode_header(Cmd.DISPLAY_BITMAP, x, y, x + w - 1, y + h - 1)) |
||||
data = to_rgb565le(image) |
||||
for start in range(0, len(data), CHUNK_SIZE): |
||||
self._write(data[start:start + CHUNK_SIZE]) |
||||
|
||||
def _write(self, data: bytes) -> None: |
||||
if self._serial is None: |
||||
raise OSError("Display nicht verbunden") |
||||
self._serial.write(data) |
||||
@ -0,0 +1,153 @@ |
||||
import pytest |
||||
from PIL import Image |
||||
|
||||
from ramscreen.lcd import ( |
||||
CHUNK_SIZE, |
||||
HEIGHT, |
||||
WIDTH, |
||||
Lcd, |
||||
encode_brightness, |
||||
encode_header, |
||||
encode_orientation_portrait, |
||||
to_rgb565le, |
||||
) |
||||
|
||||
|
||||
class FakeSerial: |
||||
def __init__(self, port, baudrate, **kwargs): |
||||
self.port = port |
||||
self.baudrate = baudrate |
||||
self.kwargs = kwargs |
||||
self.writes: list[bytes] = [] |
||||
self.closed = False |
||||
|
||||
def write(self, data): |
||||
self.writes.append(bytes(data)) |
||||
return len(data) |
||||
|
||||
def close(self): |
||||
self.closed = True |
||||
|
||||
|
||||
def make_lcd(): |
||||
created = [] |
||||
|
||||
def factory(port, baudrate, **kwargs): |
||||
s = FakeSerial(port, baudrate, **kwargs) |
||||
created.append(s) |
||||
return s |
||||
|
||||
return Lcd("/dev/test", serial_factory=factory), created |
||||
|
||||
|
||||
def test_header_vollbild(): |
||||
assert encode_header(197, 0, 0, 319, 479) == bytes([0, 0, 4, 253, 223, 197]) |
||||
|
||||
|
||||
def test_header_beliebiges_rechteck(): |
||||
assert encode_header(197, 10, 20, 100, 200) == bytes([2, 129, 65, 144, 200, 197]) |
||||
|
||||
|
||||
@pytest.mark.parametrize( |
||||
("percent", "expected"), |
||||
[ |
||||
(20, [51, 0, 0, 0, 0, 110]), |
||||
(100, [0, 0, 0, 0, 0, 110]), |
||||
(0, [63, 192, 0, 0, 0, 110]), |
||||
], |
||||
) |
||||
def test_helligkeit_ist_invertiert(percent, expected): |
||||
assert encode_brightness(percent) == bytes(expected) |
||||
|
||||
|
||||
@pytest.mark.parametrize("percent", [-1, 101]) |
||||
def test_helligkeit_ausserhalb_bereich(percent): |
||||
with pytest.raises(ValueError): |
||||
encode_brightness(percent) |
||||
|
||||
|
||||
def test_ausrichtung_portrait(): |
||||
assert encode_orientation_portrait() == bytes([0, 0, 0, 0, 0, 121, 100, 1, 64, 1, 224, 0, 0, 0, 0, 0]) |
||||
|
||||
|
||||
@pytest.mark.parametrize( |
||||
("rgb", "expected"), |
||||
[ |
||||
((255, 255, 255), b"\xff\xff"), |
||||
((255, 0, 0), b"\x00\xf8"), |
||||
((0, 255, 0), b"\xe0\x07"), |
||||
((0, 0, 255), b"\x1f\x00"), |
||||
((0, 0, 0), b"\x00\x00"), |
||||
], |
||||
) |
||||
def test_rgb565_little_endian(rgb, expected): |
||||
assert to_rgb565le(Image.new("RGB", (1, 1), rgb)) == expected |
||||
|
||||
|
||||
def test_rgb565_zeilenweise_reihenfolge(): |
||||
img = Image.new("RGB", (2, 1)) |
||||
img.putpixel((0, 0), (255, 0, 0)) |
||||
img.putpixel((1, 0), (0, 0, 255)) |
||||
assert to_rgb565le(img) == b"\x00\xf8\x1f\x00" |
||||
|
||||
|
||||
def test_open_nutzt_serielle_parameter(): |
||||
lcd, created = make_lcd() |
||||
lcd.open() |
||||
s = created[0] |
||||
assert s.port == "/dev/test" |
||||
assert s.baudrate == 115200 |
||||
assert s.kwargs == {"timeout": 1, "rtscts": True, "write_timeout": 5} |
||||
|
||||
|
||||
def test_show_vollbild_header_und_datenmenge(): |
||||
lcd, created = make_lcd() |
||||
lcd.open() |
||||
lcd.show(Image.new("RGB", (WIDTH, HEIGHT)), 0, 0) |
||||
writes = created[0].writes |
||||
assert writes[0] == bytes([0, 0, 4, 253, 223, 197]) |
||||
data = b"".join(writes[1:]) |
||||
assert len(data) == WIDTH * HEIGHT * 2 |
||||
assert all(len(chunk) <= CHUNK_SIZE for chunk in writes[1:]) |
||||
assert len(writes) == 1 + (WIDTH * HEIGHT * 2) // CHUNK_SIZE |
||||
|
||||
|
||||
def test_show_ausschnitt(): |
||||
lcd, created = make_lcd() |
||||
lcd.open() |
||||
lcd.show(Image.new("RGB", (10, 5), (255, 255, 255)), 10, 20) |
||||
writes = created[0].writes |
||||
assert writes[0] == encode_header(197, 10, 20, 19, 24) |
||||
assert b"".join(writes[1:]) == b"\xff\xff" * 50 |
||||
|
||||
|
||||
@pytest.mark.parametrize(("x", "y", "size"), [(311, 0, (10, 10)), (0, 471, (10, 10)), (-1, 0, (5, 5))]) |
||||
def test_show_ausserhalb_display(x, y, size): |
||||
lcd, _ = make_lcd() |
||||
lcd.open() |
||||
with pytest.raises(ValueError): |
||||
lcd.show(Image.new("RGB", size), x, y) |
||||
|
||||
|
||||
def test_schreiben_ohne_verbindung_ist_oserror(): |
||||
lcd, _ = make_lcd() |
||||
with pytest.raises(OSError): |
||||
lcd.set_brightness(20) |
||||
|
||||
|
||||
def test_close_schliesst_und_ist_wiederholbar(): |
||||
lcd, created = make_lcd() |
||||
lcd.open() |
||||
lcd.close() |
||||
lcd.close() |
||||
assert created[0].closed |
||||
with pytest.raises(OSError): |
||||
lcd.set_orientation_portrait() |
||||
|
||||
|
||||
def test_befehle_landen_auf_der_schnittstelle(): |
||||
lcd, created = make_lcd() |
||||
lcd.open() |
||||
lcd.set_orientation_portrait() |
||||
lcd.set_brightness(20) |
||||
assert created[0].writes == [encode_orientation_portrait(), encode_brightness(20)] |
||||
Loading…
Reference in new issue