diff --git a/python_payload/apps/nick.py b/python_payload/apps/nick.py index fcfbcba50988259a6d0e14a181f1859d88a1f77c..e4b2df6e2ca5177607bf865116b822aee6c6622d 100644 --- a/python_payload/apps/nick.py +++ b/python_payload/apps/nick.py @@ -2,20 +2,29 @@ from st4m.application import Application from st4m.property import PUSH_RED, GO_GREEN, BLACK import leds +import json + class NickApp(Application): def __init__(self, name): super().__init__(name) - self._nick = self._read_nick_from_file() self._scale = 1 self._dir = 1 self._led = 0.0 + self._filename = "nick.json" + self._defaults = { + "name": "flow3r", + "size": 75, + "font": 5, + } + self._data = {} + self._load() def draw(self, ctx): ctx.text_align = ctx.CENTER ctx.text_baseline = ctx.MIDDLE - ctx.font_size = 75 - ctx.font = ctx.get_font_name(5) + ctx.font_size = self._data["size"] + ctx.font = ctx.get_font_name(self._data["font"]) # TODO (q3k) bug: we have to do this, otherwise we have horrible blinking ctx.rgb(1, 1, 1) @@ -27,7 +36,7 @@ class NickApp(Application): ctx.move_to(0, 0) ctx.save() ctx.scale(self._scale, 1) - ctx.text(self._nick) + ctx.text(self._data["name"]) ctx.restore() leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 0.2) @@ -38,6 +47,9 @@ class NickApp(Application): def on_enter(self): super().on_enter() + def on_exit(self): + self._save_to_json(self._data) + def think(self, ins: InputState, delta_ms: int) -> None: super().think(ins, delta_ms) @@ -50,8 +62,27 @@ class NickApp(Application): if self._led >= 40: self._led = 0 - def _read_nick_from_file(self): - return "iggy" + def _load(self): + self._data = self._defaults.copy() + self._data.update(self._load_from_json()) + + def _load_from_json(self): + jsondata = "" + data = {} + try: + with open(self._filename) as f: + jsondata = f.read() + data = json.loads(jsondata) + except OSError: + pass + return data + + def _save_to_json(self, data): + jsondata = json.dumps(data) + + with open(self._filename, "w") as f: + f.write(jsondata) + f.close() app = NickApp("nick") diff --git a/python_payload/st4m/application.py b/python_payload/st4m/application.py index a8559472ec876b9b13a6e238ab4c5291ef55a4f4..5cb0255db7967a7a2d50f083eb83c4e14b57a759 100644 --- a/python_payload/st4m/application.py +++ b/python_payload/st4m/application.py @@ -8,9 +8,13 @@ class Application(ViewWithInputState): self._view_manager = None super().__init__() + def on_exit(self): + pass + def think(self, ins: InputState, delta_ms: int) -> None: super().think(ins, delta_ms) if self.input.left_shoulder.middle.pressed: if self._view_manager is not None: + self.on_exit() self._view_manager.pop(ViewTransitionSwipeRight())