diff --git a/python_payload/st3m/run.py b/python_payload/st3m/run.py index 6ba18204a85b73a65785bd87dd441b7e033460df..c4b89ac88fb50788abd8ee9c4a1f00c55ef6036b 100644 --- a/python_payload/st3m/run.py +++ b/python_payload/st3m/run.py @@ -18,7 +18,7 @@ from st3m.application import ( ApplicationContext, ) from st3m.about import About -from st3m import settings, logging, processors, wifi +from st3m import settings_menu as settings, logging, processors, wifi import captouch, audio, leds, gc, sys_buttons import os diff --git a/python_payload/st3m/settings.py b/python_payload/st3m/settings.py index 51a23f33da57c71b85544f00ee8d0a87896e68f6..f0c93bb0b98ebfc4e796a8c1c4c2da52bb906395 100644 --- a/python_payload/st3m/settings.py +++ b/python_payload/st3m/settings.py @@ -17,15 +17,9 @@ from st3m.goose import ( Any, Dict, List, - Tuple, Optional, Callable, - Union, - TYPE_CHECKING, ) -from st3m.ui.menu import MenuController, MenuItem, MenuItemBack, MenuItemForeground -from st3m.application import BundleMetadata, MenuItemAppLaunch -from st3m.ui.elements.menus import SimpleMenu from st3m.ui.view import ViewManager from st3m.utils import lerp, ease_out_cubic, reduce from ctx import Context @@ -326,54 +320,6 @@ class ObfuscatedValueWidget(TunableWidget): pass -class SettingsMenuItem(MenuItem): - """ - A MenuItem which draws its label offset to the left, and a Tunable's widget - to the right. - """ - - def __init__(self, tunable: UnaryTunable): - self.tunable = tunable - self.widget = tunable.get_widget() - - def press(self, vm: Optional[ViewManager]) -> None: - self.widget.press(vm) - - def label(self) -> str: - return self.tunable._name - - def draw(self, ctx: Context) -> None: - ctx.move_to(40, 0) - ctx.text_align = ctx.RIGHT - super().draw(ctx) - ctx.stroke() - - ctx.save() - ctx.translate(50, 0) - self.widget.draw(ctx) - ctx.restore() - - def think(self, ins: InputState, delta_ms: int) -> None: - self.widget.think(ins, delta_ms) - - -class SettingsMenu(SimpleMenu): - """ - SimpleMenu but smol. - """ - - def on_enter(self, vm: Optional[ViewManager]) -> None: - super().on_enter(vm) - load_all() - - def on_exit(self) -> None: - save_all() - super().on_exit() - - SIZE_LARGE = 20 - SIZE_SMALL = 15 - - # Actual tunables / settings. onoff_button_swap = OnOffTunable("Swap Buttons", "system.swap_buttons", False) onoff_show_fps = OnOffTunable("Show FPS", "system.show_fps", False) @@ -402,22 +348,6 @@ load_save_settings: List[UnaryTunable] = [ str_hostname, ] -if TYPE_CHECKING: - MenuStructureEntry = Union[UnaryTunable, Tuple[str, List["MenuStructureEntry"]]] - MenuStructure = List[MenuStructureEntry] - -# Main settings menu -settings_menu_structure: "MenuStructure" = [ - onoff_show_tray, - onoff_button_swap, - onoff_show_fps, - # onoff_debug, - # onoff_debug_touch, - onoff_wifi, - onoff_wifi_preference, - MenuItemAppLaunch(BundleMetadata("/flash/sys/apps/w1f1")), -] - def load_all() -> None: """ @@ -463,22 +393,3 @@ def save_all() -> None: return log.info("Saved settings to flash") - - -def build_menu_recursive(items: "MenuStructure") -> SimpleMenu: - """ - Recursively build a menu for the given setting structure. - """ - mib: MenuItem = MenuItemBack() - positions: List[MenuItem] = [ - mib, - ] + [SettingsMenuItem(t) if isinstance(t, UnaryTunable) else t for t in items] - - return SettingsMenu(positions) - - -def build_menu() -> SimpleMenu: - """ - Return a SettingsMenu for all settings. - """ - return build_menu_recursive(settings_menu_structure) diff --git a/python_payload/st3m/settings_menu.py b/python_payload/st3m/settings_menu.py new file mode 100644 index 0000000000000000000000000000000000000000..39172874064c717e7944bd5684f6aadf28167609 --- /dev/null +++ b/python_payload/st3m/settings_menu.py @@ -0,0 +1,132 @@ +""" +Settings menu of flow3r badge. +""" + +from st3m import InputState, logging +from st3m.goose import ( + List, + Tuple, + Optional, + Union, + TYPE_CHECKING, +) +from st3m.ui.menu import MenuItem, MenuItemBack +from st3m.application import BundleMetadata, MenuItemAppLaunch +from st3m.ui.elements.menus import SimpleMenu +from st3m.ui.view import ViewManager +from st3m.settings import ( + UnaryTunable, + load_all, + save_all, + onoff_show_tray, + onoff_button_swap, + onoff_debug, + onoff_debug_touch, + onoff_wifi, + onoff_wifi_preference, + onoff_show_fps, + str_wifi_ssid, + str_wifi_psk, + str_hostname, +) +from ctx import Context + +log = logging.Log(__name__, level=logging.INFO) + + +class SettingsMenuItem(MenuItem): + """ + A MenuItem which draws its label offset to the left, and a Tunable's widget + to the right. + """ + + def __init__(self, tunable: UnaryTunable): + self.tunable = tunable + self.widget = tunable.get_widget() + + def press(self, vm: Optional[ViewManager]) -> None: + self.widget.press(vm) + + def label(self) -> str: + return self.tunable._name + + def draw(self, ctx: Context) -> None: + ctx.move_to(40, 0) + ctx.text_align = ctx.RIGHT + super().draw(ctx) + ctx.stroke() + + ctx.save() + ctx.translate(50, 0) + self.widget.draw(ctx) + ctx.restore() + + def think(self, ins: InputState, delta_ms: int) -> None: + self.widget.think(ins, delta_ms) + + +class SettingsMenu(SimpleMenu): + """ + SimpleMenu but smol. + """ + + def on_enter(self, vm: Optional[ViewManager]) -> None: + super().on_enter(vm) + load_all() + + def on_exit(self) -> None: + save_all() + super().on_exit() + + SIZE_LARGE = 20 + SIZE_SMALL = 15 + + +# List of all settings to be loaded/saved +load_save_settings: List[UnaryTunable] = [ + onoff_show_tray, + onoff_button_swap, + onoff_debug, + onoff_debug_touch, + onoff_wifi, + onoff_wifi_preference, + onoff_show_fps, + str_wifi_ssid, + str_wifi_psk, + str_hostname, +] + +if TYPE_CHECKING: + MenuStructureEntry = Union[UnaryTunable, Tuple[str, List["MenuStructureEntry"]]] + MenuStructure = List[MenuStructureEntry] + +# Main settings menu +settings_menu_structure: "MenuStructure" = [ + onoff_show_tray, + onoff_button_swap, + onoff_show_fps, + # onoff_debug, + # onoff_debug_touch, + onoff_wifi, + onoff_wifi_preference, + MenuItemAppLaunch(BundleMetadata("/flash/sys/apps/w1f1")), +] + + +def build_menu_recursive(items: "MenuStructure") -> SimpleMenu: + """ + Recursively build a menu for the given setting structure. + """ + mib: MenuItem = MenuItemBack() + positions: List[MenuItem] = [ + mib, + ] + [SettingsMenuItem(t) if isinstance(t, UnaryTunable) else t for t in items] + + return SettingsMenu(positions) + + +def build_menu() -> SimpleMenu: + """ + Return a SettingsMenu for all settings. + """ + return build_menu_recursive(settings_menu_structure)