Tutorial: 30-Board Async Simulator & pytest Loopback Harness

Developing embedded MicroPython apps traditionally requires constant flash/upload cycles and multiple physical boards on your desk. pylotos-sim solves both interactive visual design and fast CI regression testing on any laptop or GitHub Actions runner.


1. Interactive Dual-Pane Browser Simulator (pylotos-sim)

pylotos-sim runs a real Python asyncio loop executing your @app.every(...) timers, state bindings, themes, notifications, and wireless loopback mocks concurrently with an HTTP preview server:

# Launch with any of the 30 PylotOS board presets:
pylotos-sim examples/smart_thermostat.py --board m5stack_dial --port 8080

Key Simulator Features


2. Writing Deterministic pytest Suites with install_sim()

Call install_sim(board_id) in any pytest test to inject in-memory mocks for hal, framebuf, sys_web, sys_ota, pylotos.events, and pylotos.services:

import importlib
import sys
from pylotos_sim import BOARD_PRESETS, install_sim


def _reload_ui():
    for name in [m for m in list(sys.modules) if m.startswith("pylotos.ui")]:
        sys.modules.pop(name, None)
    return importlib.import_module("pylotos.ui")


def test_app_renders_across_all_30_boards():
    """Verify zero layout crashes across 72x40 OLED, 240x240 Round, 320x240 TFT & Headless."""
    for board_id in BOARD_PRESETS:
        env = install_sim(board_id)
        ui = _reload_ui()

        st = ui.State(val=42)
        app = ui.App(
            "MatrixTest",
            state=st,
            screens=[ui.Screen("Main", children=[ui.Metric("Load", st.bind("val"), unit="%")])],
        )
        cv = ui.Canvas()
        if env.preset["width"] > 0 and env.preset["height"] > 0:
            cv.render(app.draw)
            assert len(env.blits) > 0
        assert app.to_web_dict()["title"] == "MatrixTest"

3. Testing Wireless, NFC, Audio & CAN Without Hardware

install_sim() includes full Phase 4 loopback queues (env.wireless) so you can test packet framing, EU868 band guards, NDEF decoding, and CAN/ULP logic in milliseconds:

import asyncio
import pytest
from pylotos_sim import install_sim


def test_lora_loopback_and_regulatory_guard():
    env = install_sim("heltec_wifi_lora_32_v3")
    import hal
    from pylotos import wireless

    hal.rf.open()
    hal.rf.set_region("EU868")

    # Out-of-band frequency (915 MHz in EU868) raises OSError immediately, matching C HAL!
    with pytest.raises(OSError):
        hal.rf.set_freq(915_000_000)

    hal.rf.set_freq(868_100_000)
    key = b"unit-test-secret"
    hal.rf.send(wireless.pack_frame(key, 1, b"sensor:24.1"))

    rx_buf = bytearray(64)
    meta = {}
    n = asyncio.run(wireless.recv_rf_into(rx_buf, meta=meta, poll_ms=1, timeout_ms=50))
    seq, payload = wireless.unpack_frame(key, memoryview(rx_buf)[:n], min_seq=1)

    assert seq == 1
    assert payload == b"sensor:24.1"
    assert meta["rssi"] == -42
    assert hal.wireless_reset_app_handles() == 1