66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
"""Liest die Serial-Ausgabe der esticky-Firmware im Vorschau-Modus (-DESTICKY_PREVIEW)
|
|
und speichert jeden PREVIEW-Block als PNG (Graustufen, 3-fach vergrößert).
|
|
|
|
Aufruf: uv run --with pyserial --with pillow python tools/capture_preview.py SEKUNDEN AUSGABEORDNER [PORT]
|
|
|
|
Das Öffnen des Ports startet den XIAO neu. Gelesen wird, bis "Vorschau-Modus" erscheint
|
|
oder die Zeit abgelaufen ist; alle anderen Zeilen werden ausgegeben.
|
|
"""
|
|
import re
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import serial
|
|
from PIL import Image
|
|
|
|
PALETTE = {"0": 255, "1": 192, "2": 128, "3": 0}
|
|
BOOT_NOISE = re.compile(r"^(ESP-ROM|Build:|Saved|SPIWP|mode:|load:|entry )")
|
|
|
|
|
|
def save_png(rows, path, scale=3, pad=4):
|
|
width, height = len(rows[0]), len(rows)
|
|
image = Image.new("L", (width, height))
|
|
image.putdata([PALETTE.get(c, 64) for row in rows for c in row])
|
|
image = image.resize((width * scale, height * scale), Image.NEAREST)
|
|
framed = Image.new("L", (image.width + 2 * pad, image.height + 2 * pad), 90)
|
|
framed.paste(image, (pad, pad))
|
|
framed.save(path)
|
|
|
|
|
|
def main():
|
|
timeout, outdir = float(sys.argv[1]), Path(sys.argv[2])
|
|
port = sys.argv[3] if len(sys.argv) > 3 else "/dev/cu.usbmodem21201"
|
|
outdir.mkdir(parents=True, exist_ok=True)
|
|
|
|
connection = serial.Serial()
|
|
connection.port, connection.baudrate, connection.timeout = port, 115200, 0.5
|
|
connection.dtr = connection.rts = False
|
|
connection.open()
|
|
|
|
data, deadline = b"", time.time() + timeout
|
|
while time.time() < deadline and b"Vorschau-Modus" not in data:
|
|
try:
|
|
data += connection.read(65536)
|
|
except serial.SerialException as error:
|
|
print("Serial beendet:", error)
|
|
break
|
|
|
|
rows, label = None, None
|
|
for line in data.decode("utf-8", "replace").splitlines():
|
|
line = line.rstrip("\r")
|
|
if line.startswith("PREVIEW-BEGIN"):
|
|
rows, label = [], line.split(" ", 1)[1]
|
|
elif line == "PREVIEW-END" and rows:
|
|
save_png(rows, outdir / f"{label}.png")
|
|
print(f"gespeichert: {outdir / label}.png")
|
|
rows = None
|
|
elif rows is not None:
|
|
rows.append(line)
|
|
elif line and not BOOT_NOISE.match(line):
|
|
print(line)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|