Files
2026-11-15 17:44:52 -05:00

64 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Load the logo atlas once, then blit logos to an rpi-rgb-led-matrix panel.
Speed notes (rpi-rgb-led-matrix):
- Decode everything at startup (atlas.bin), never PNGs inside the render loop.
- Keep logos as RGB-mode PIL images and only redraw when the aircraft changes.
The Python binding's SetImage(img, x, y) defaults to unsafe=True, a fast raw-buffer
path that requires RGB mode (which the atlas loader guarantees). Pass unsafe=False
only if you feed it other image modes.
- Draw into an offscreen canvas and SwapOnVSync() so you never show a half-drawn frame.
- Don't pre-apply gamma: the library's luminance correction (on by default) handles it.
"""
import re
import struct
from pathlib import Path
from PIL import Image
from rgbmatrix import RGBMatrix, RGBMatrixOptions # type: ignore
def load_atlas(path="out/atlas.bin"):
raw = Path(path).read_bytes()
assert raw[:4] == b"LG16"
size, count = struct.unpack_from("<BH", raw, 4)
off, logos = 7, {}
step = 4 + size * size * 3
for _ in range(count):
code = raw[off:off + 4].rstrip(b"\0").decode()
logos[code] = Image.frombytes("RGB", (size, size), raw[off + 4:off + step])
off += step
return logos
LOGOS = load_atlas()
UFO = LOGOS["UFO"] # unidentified: no callsign, or a prefix we have no logo for
GA = LOGOS["GA"] # general aviation: US tail numbers
# US N-number: N + 1-5 digits + up to 2 letters (N123AB, N7, N99999). Doesn't match airline callsigns.
N_NUMBER = re.compile(r"^N\d{1,5}[A-Z]{0,2}$")
def logo_for(callsign) -> Image.Image:
cs = (callsign or "").strip().upper()
if not cs:
return UFO
if N_NUMBER.match(cs):
return GA
# Airline callsign = 3-letter ICAO prefix + flight number (DAL1234)
if len(cs) > 3 and cs[:3].isalpha() and cs[3].isdigit():
return LOGOS.get(cs[:3], UFO)
return UFO
if __name__ == "__main__":
o = RGBMatrixOptions()
o.rows, o.cols = 32, 64 # adjust to your panel
matrix = RGBMatrix(options=o)
canvas = matrix.CreateFrameCanvas()
canvas.Clear()
canvas.SetImage(logo_for("DAL1234"), 0, 8)
canvas = matrix.SwapOnVSync(canvas)
input("enter to quit")