# code.py - PM_K-1 A/B firmware loader (stable; rarely changes). # # The real application lives in app.py; app.bak holds the previous known-good build. The web editor # pushes a new app.py to a "trial" slot over USB-MIDI; this loader runs it, and if the new build # fails to boot it AUTOMATICALLY ROLLS BACK to app.bak. (The Pico is also unbrickable: BOOTSEL -> # drag a CircuitPython .uf2.) app.py clears the /trial marker once it has run healthily for ~5s. import supervisor, os supervisor.runtime.autoreload = False # updates reboot explicitly; never auto-restart on our own writes def _trial(): try: os.stat("/trial"); return True except OSError: return False try: import app # runs the application (app.py ends with App().run()) except Exception: if _trial(): # a freshly-pushed build crashed on startup -> roll back try: os.remove("/app.py"); os.rename("/app.bak", "/app.py"); os.remove("/trial") except Exception: pass supervisor.reload() # reboot into the restored known-good build else: raise # the active build failed unexpectedly (rare) -> on-screen traceback