"""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)