#!/usr/bin/env python3 """Run one simulated led-driver (one strip) in its own process. Started by ``bridge_sim.py``; not normally run by hand. Runs the real ``led-driver/src/main.py`` as ``__main__``, with: - ``stubs/`` first on ``sys.path`` (machine, neopixel, network, espnow, ...) - the driver's own folder ``workspace/drivers/NN`` as its flash root (cwd): ``settings.json``, ``presets.json`` and ``patterns/`` live there - ESP-NOW and pixel output carried to the bridge simulator by ``_simlink`` Environment (set by the bridge): ``LED_SIM_INDEX``, ``LED_SIM_MAC``, ``LED_SIM_DIR``, ``LED_SIM_ADDR`` (host:port), ``LED_SIM_AUTH`` (hex), ``LED_SIM_FPS``, ``LED_SIM_LOOP_SLEEP_MS``. """ import os import runpy import sys from pathlib import Path SIM_ROOT = Path(__file__).resolve().parent STUBS_ROOT = SIM_ROOT / "stubs" DRIVER_ROOT = SIM_ROOT.parent / "led-driver" DRIVER_SRC = DRIVER_ROOT / "src" DRIVER_LIB = DRIVER_ROOT / "lib" DRIVER_MAIN = DRIVER_SRC / "main.py" def main(): index = int(os.environ["LED_SIM_INDEX"]) drv_dir = Path(os.environ["LED_SIM_DIR"]).resolve() host, port = os.environ["LED_SIM_ADDR"].rsplit(":", 1) authkey = bytes.fromhex(os.environ["LED_SIM_AUTH"]) fps = float(os.environ.get("LED_SIM_FPS", "30")) os.chdir(drv_dir) log = open(drv_dir / "driver.log", "w", buffering=1, encoding="utf-8") sys.stdout = log sys.stderr = log import faulthandler import signal faulthandler.register(signal.SIGUSR1, file=log, all_threads=True) # kill -USR1 : stacks to driver.log # MicroPython search order is cwd (flash root) then /lib. Stubs go first so # they replace the hardware modules; the driver folder comes before src so # its patterns/ package (OTA target) wins over led-driver/src/patterns. sys.path[:] = [ str(STUBS_ROOT), str(drv_dir), str(DRIVER_LIB), str(DRIVER_SRC), ] + [p for p in sys.path[1:] if p] import gc if not hasattr(gc, "mem_free"): gc.mem_free = lambda: 256 * 1024 gc.mem_alloc = lambda: 64 * 1024 import _simlink _simlink.connect((host, int(port)), authkey, index, fps=fps) import settings as led_settings led_settings.Settings.SETTINGS_FILE = str(drv_dir / "settings.json") runpy.run_path(str(DRIVER_MAIN), run_name="__main__") if __name__ == "__main__": try: main() except SystemExit: raise except BaseException: import traceback traceback.print_exc() sys.stdout.flush() raise SystemExit(1)