diff --git a/python_payload/mypystubs/machine.pyi b/python_payload/mypystubs/machine.pyi index 5f1a7adbde03a02ec785825cb5f408c521cac864..1b180563b6df2992d4d704e1bfa495575372640f 100644 --- a/python_payload/mypystubs/machine.pyi +++ b/python_payload/mypystubs/machine.pyi @@ -1,5 +1,8 @@ from typing import Any +def reset() -> None: + pass + class Pin: """ See the official micropython docs for more details: diff --git a/python_payload/st3m/run.py b/python_payload/st3m/run.py index e1810daec288c91f833f203fab3a00c0b64662ed..df94f95d1b2cd1401cc1d539ac9793ee0f0619b6 100644 --- a/python_payload/st3m/run.py +++ b/python_payload/st3m/run.py @@ -1,6 +1,12 @@ from st3m.reactor import Reactor, Responder from st3m.goose import List -from st3m.ui.menu import MenuItem, MenuItemBack, MenuItemForeground, MenuItemNoop +from st3m.ui.menu import ( + MenuItem, + MenuItemBack, + MenuItemForeground, + MenuItemNoop, + MenuItemAction, +) from st3m.ui.elements import overlays from st3m.ui.view import View, ViewManager, ViewTransitionBlend from st3m.ui.elements.menus import SimpleMenu, SunMenu @@ -8,6 +14,10 @@ from st3m.application import discover_bundles, BundleMetadata from st3m import settings, logging, processors import captouch, audio, leds, gc +import os + +import machine + log = logging.Log(__name__, level=logging.INFO) @@ -91,6 +101,11 @@ def run_view(v: View) -> None: reactor.run() +def yeet_local_changes() -> None: + os.remove("/flash/sys/.sys-installed") + machine.reset() + + def run_main() -> None: log.info(f"starting main") log.info(f"free memory: {gc.mem_free()}") @@ -110,7 +125,8 @@ def run_main() -> None: MenuItemForeground("Settings", menu_settings), MenuItemNoop("Disk Mode"), MenuItemNoop("About"), - MenuItemNoop("Reboot"), + MenuItemAction("Yeet Local Changes", yeet_local_changes), + MenuItemAction("Reboot", lambda: machine.reset()), ], ) menu_main = SunMenu( diff --git a/python_payload/st3m/ui/menu.py b/python_payload/st3m/ui/menu.py index da90f6583321b1abd491ffe60010dcb9dbd40efc..70e68c142b331a09324facc76bd253f0f7a34917 100644 --- a/python_payload/st3m/ui/menu.py +++ b/python_payload/st3m/ui/menu.py @@ -2,7 +2,7 @@ import st3m from st3m import Responder from st3m import logging -from st3m.goose import ABCBase, abstractmethod, List, Optional +from st3m.goose import ABCBase, abstractmethod, List, Optional, Callable from st3m.input import InputState, InputController from st3m.ui.view import ( BaseView, @@ -14,7 +14,6 @@ from st3m.ui.view import ( from st3m.ui.interactions import ScrollController from ctx import Context - log = logging.Log(__name__) @@ -73,6 +72,22 @@ class MenuItemForeground(MenuItem): return self._label +class MenuItemAction(MenuItem): + """ + A MenuItem which runs the provided lambda action. + """ + + def __init__(self, label: str, action: Callable[[], None]) -> None: + self._label = label + self._action = action + + def press(self, vm: Optional[ViewManager]) -> None: + self._action() + + def label(self) -> str: + return self._label + + class MenuItemNoop(MenuItem): """ A MenuItem which does nothing.