98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
||
"""Holt einen Screenshot vom T-Display über die serielle Schnittstelle.
|
||
|
||
Aufruf: screenshot.py <port> <ausgabe.png> [seitenwechsel] [optionen]
|
||
|
||
Schickt vorher so oft 'p' wie angegeben (nächste Seite), dann so oft 'o' (Optionsknopf) und dann 's'.
|
||
Das Board antwortet mit "SCREENSHOT <w> <h>" und dem Bildpuffer (RGB565, Big-Endian).
|
||
Braucht nur die Standardbibliothek.
|
||
"""
|
||
import fcntl
|
||
import os
|
||
import select
|
||
import struct
|
||
import sys
|
||
import termios
|
||
import time
|
||
import zlib
|
||
|
||
|
||
def open_port(path):
|
||
fd = os.open(path, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
|
||
# DTR und RTS gemeinsam zurücknehmen – einzeln würde die Auto-Reset-Schaltung das Board neu starten
|
||
fcntl.ioctl(fd, termios.TIOCMBIC, struct.pack("I", termios.TIOCM_DTR | termios.TIOCM_RTS))
|
||
attrs = termios.tcgetattr(fd)
|
||
attrs[0] = attrs[1] = attrs[3] = 0
|
||
attrs[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
|
||
attrs[4] = attrs[5] = termios.B115200
|
||
termios.tcsetattr(fd, termios.TCSANOW, attrs)
|
||
termios.tcflush(fd, termios.TCIOFLUSH)
|
||
return fd
|
||
|
||
|
||
def read_until(fd, done, timeout):
|
||
buf = bytearray()
|
||
deadline = time.time() + timeout
|
||
while not done(buf):
|
||
left = deadline - time.time()
|
||
if left <= 0 or not select.select([fd], [], [], left)[0]:
|
||
sys.exit("Zeitüberschreitung – antwortet das Board?")
|
||
buf += os.read(fd, 65536)
|
||
return buf
|
||
|
||
|
||
def write_png(path, w, h, rgb):
|
||
def chunk(tag, data):
|
||
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data))
|
||
|
||
rows = b"".join(b"\x00" + rgb[y * w * 3:(y + 1) * w * 3] for y in range(h))
|
||
with open(path, "wb") as f:
|
||
f.write(b"\x89PNG\r\n\x1a\n")
|
||
f.write(chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0)))
|
||
f.write(chunk(b"IDAT", zlib.compress(rows, 9)))
|
||
f.write(chunk(b"IEND", b""))
|
||
|
||
|
||
def main():
|
||
if len(sys.argv) < 3:
|
||
sys.exit(__doc__)
|
||
port, out = sys.argv[1], sys.argv[2]
|
||
pages = int(sys.argv[3]) if len(sys.argv) > 3 else 0
|
||
options = int(sys.argv[4]) if len(sys.argv) > 4 else 0
|
||
|
||
fd = open_port(port)
|
||
os.write(fd, b"p" * pages + b"o" * options)
|
||
|
||
# Loggt das Board mitten in die Bilddaten, stimmt die Prüfsumme nicht: dann noch einmal anfordern
|
||
for attempt in range(4):
|
||
os.write(fd, b"s")
|
||
marker = b"SCREENSHOT "
|
||
buf = read_until(fd, lambda b: marker in b and b"\n" in b[b.index(marker):], 10)
|
||
start = buf.index(marker)
|
||
header_end = buf.index(b"\n", start)
|
||
w, h = map(int, buf[start + len(marker):header_end].split())
|
||
size = w * h * 2
|
||
data = buf[header_end + 1:]
|
||
crc_line = lambda b: b"\nCRC " in b[size:] and b"\n" in b[b.index(b"\nCRC ", size) + 1:]
|
||
data += read_until(fd, lambda b: crc_line(data + b), 30)
|
||
pos = data.index(b"\nCRC ", size) + 5
|
||
if int(data[pos:data.index(b"\n", pos)], 16) == zlib.crc32(data[:size]):
|
||
break
|
||
print("Bilddaten gestört, neuer Versuch", file=sys.stderr)
|
||
time.sleep(0.5)
|
||
termios.tcflush(fd, termios.TCIFLUSH)
|
||
else:
|
||
sys.exit("Bilddaten mehrfach gestört")
|
||
os.close(fd)
|
||
|
||
rgb = bytearray()
|
||
for i in range(0, size, 2):
|
||
v = (data[i] << 8) | data[i + 1]
|
||
rgb += bytes(((v >> 11) * 255 // 31, ((v >> 5) & 63) * 255 // 63, (v & 31) * 255 // 31))
|
||
write_png(out, w, h, bytes(rgb))
|
||
print(f"{out}: {w}x{h}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|