You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
4.0 KiB
153 lines
4.0 KiB
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, "exclusive": True}
|
|
|
|
|
|
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)]
|
|
|