Files
led-controller/stubs/machine.py
T
jimmyandClaude Haiku 5.5 4d8aeb5df8 feat: simulator bridge, rig seed and web UI (in progress)
Work in progress from the same tree as the controller's sequences-v2 changes.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 14:10:55 +13:00

90 lines
1.7 KiB
Python

"""MicroPython ``machine`` stub for led-simulator (Pyodide)."""
import os
import time
try:
import _simlink
except ImportError: # outside the simulator
_simlink = None
PWRON_RESET = 1
HARD_RESET = 2
WDT_RESET = 3
DEEPSLEEP_RESET = 4
SOFT_RESET = 5
# SystemExit code that tells the page to boot this driver again (machine.reset()).
RESET_EXIT_CODE = 75
# The driver main loop calls wdt.feed() once per pass with no sleep. A short
# sleep here keeps 16 simulated drivers from pinning every CPU core, and the
# feed is where the simulator flushes pixels and flash writes.
_LOOP_SLEEP_S = max(0.0, float(os.environ.get("LED_SIM_LOOP_SLEEP_MS", "2")) / 1000.0)
_freq = 160000000
class Pin:
OUT = 1
IN = 0
PULL_UP = 1
PULL_DOWN = 2
def __init__(self, pin_id, mode=None, *, pull=None, value=None):
self.id = pin_id
self.mode = mode
self._value = value or 0
def value(self, v=None):
if v is None:
return self._value
self._value = 1 if v else 0
return None
def on(self):
self._value = 1
def off(self):
self._value = 0
class WDT:
def __init__(self, id=0, timeout=10000):
self._timeout = timeout
def feed(self):
if _LOOP_SLEEP_S:
time.sleep(_LOOP_SLEEP_S)
if _simlink is not None:
_simlink.on_feed()
def freq(hz=None):
global _freq
if hz is None:
return _freq
_freq = int(hz)
return None
def reset():
raise SystemExit(RESET_EXIT_CODE)
def soft_reset():
raise SystemExit(RESET_EXIT_CODE)
def reset_cause():
return PWRON_RESET
def unique_id():
mac = os.environ.get("LED_SIM_MAC", "aabbccddeeff")
return bytes.fromhex(mac)
def idle():
time.sleep(0.001)