Files
second-screen/main.py
T
2026-10-22 13:24:51 +02:00

69 lines
2.4 KiB
Python

"""Container entry point: show the view named by VIEW on the second Turing screen."""
import logging
import os
import signal
import sys
import threading
import registry
import runner
from timeutil import local_now
DEFAULT_DEVICE = "/dev/serial/by-id/usb-Turing_UsbMonitor_USB35INCHIPSV2-if00"
# force=True: the screen library configures logging at import, which would swallow ours.
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s", force=True)
log = logging.getLogger("main")
def _open_lcd(device: str, orientation_name: str, brightness: int):
from library.lcd.lcd_comm import Orientation # the screen library lives outside the project
from library.lcd.lcd_comm_rev_a import LcdCommRevA
lcd = LcdCommRevA(com_port=device) if device != "AUTO" else LcdCommRevA()
lcd.InitializeComm()
lcd.SetOrientation(Orientation[orientation_name])
lcd.SetBrightness(brightness)
return lcd
def main(stop: threading.Event | None = None) -> None:
stop = stop or threading.Event()
view_name = os.environ.get("VIEW", "")
device = os.environ.get("SCREEN_DEVICE", DEFAULT_DEVICE)
orientation = os.environ.get("SCREEN_ORIENTATION", "LANDSCAPE")
brightness = int(os.environ.get("SCREEN_BRIGHTNESS", "50"))
sys.path.insert(0, os.environ.get("LCD_LIB_PATH", "/opt/lcd"))
def running() -> bool:
return not stop.is_set()
log.info("Opening display on %s (%s), view %r", device, orientation, view_name)
lcd = runner.open_screen(lambda: _open_lcd(device, orientation, brightness), running=running, sleep=stop.wait)
if lcd is None:
return
try:
try:
view = registry.make_view(view_name)
except registry.UnknownView as exc:
log.error("%s", exc)
runner.idle_with_message(
lcd, [f"UNKNOWN VIEW: {view_name or '(VIEW not set)'}", "VALID: " + ", ".join(exc.valid)],
running=running, sleep=stop.wait)
return
view.start()
try:
runner.run(view, lcd, now_fn=local_now, running=running, sleep=stop.wait)
finally:
view.stop()
finally:
lcd.closeSerial()
log.info("Stopped")
if __name__ == "__main__":
event = threading.Event()
signal.signal(signal.SIGTERM, lambda *_: event.set())
signal.signal(signal.SIGINT, lambda *_: event.set())
main(event)