Skip to content
Snippets Groups Projects
Commit 148aa1db authored by iggy's avatar iggy Committed by q3k
Browse files

py: nick.py read and store nickname and config in a json file

parent 1d6ad2d9
Branches
Tags
No related merge requests found
...@@ -2,20 +2,29 @@ from st4m.application import Application ...@@ -2,20 +2,29 @@ from st4m.application import Application
from st4m.property import PUSH_RED, GO_GREEN, BLACK from st4m.property import PUSH_RED, GO_GREEN, BLACK
import leds import leds
import json
class NickApp(Application): class NickApp(Application):
def __init__(self, name): def __init__(self, name):
super().__init__(name) super().__init__(name)
self._nick = self._read_nick_from_file()
self._scale = 1 self._scale = 1
self._dir = 1 self._dir = 1
self._led = 0.0 self._led = 0.0
self._filename = "nick.json"
self._defaults = {
"name": "flow3r",
"size": 75,
"font": 5,
}
self._data = {}
self._load()
def draw(self, ctx): def draw(self, ctx):
ctx.text_align = ctx.CENTER ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE ctx.text_baseline = ctx.MIDDLE
ctx.font_size = 75 ctx.font_size = self._data["size"]
ctx.font = ctx.get_font_name(5) ctx.font = ctx.get_font_name(self._data["font"])
# TODO (q3k) bug: we have to do this, otherwise we have horrible blinking # TODO (q3k) bug: we have to do this, otherwise we have horrible blinking
ctx.rgb(1, 1, 1) ctx.rgb(1, 1, 1)
...@@ -27,7 +36,7 @@ class NickApp(Application): ...@@ -27,7 +36,7 @@ class NickApp(Application):
ctx.move_to(0, 0) ctx.move_to(0, 0)
ctx.save() ctx.save()
ctx.scale(self._scale, 1) ctx.scale(self._scale, 1)
ctx.text(self._nick) ctx.text(self._data["name"])
ctx.restore() ctx.restore()
leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 0.2) leds.set_hsv(int(self._led), abs(self._scale) * 360, 1, 0.2)
...@@ -38,6 +47,9 @@ class NickApp(Application): ...@@ -38,6 +47,9 @@ class NickApp(Application):
def on_enter(self): def on_enter(self):
super().on_enter() super().on_enter()
def on_exit(self):
self._save_to_json(self._data)
def think(self, ins: InputState, delta_ms: int) -> None: def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms) super().think(ins, delta_ms)
...@@ -50,8 +62,27 @@ class NickApp(Application): ...@@ -50,8 +62,27 @@ class NickApp(Application):
if self._led >= 40: if self._led >= 40:
self._led = 0 self._led = 0
def _read_nick_from_file(self): def _load(self):
return "iggy" 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") app = NickApp("nick")
...@@ -8,9 +8,13 @@ class Application(ViewWithInputState): ...@@ -8,9 +8,13 @@ class Application(ViewWithInputState):
self._view_manager = None self._view_manager = None
super().__init__() super().__init__()
def on_exit(self):
pass
def think(self, ins: InputState, delta_ms: int) -> None: def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms) super().think(ins, delta_ms)
if self.input.left_shoulder.middle.pressed: if self.input.left_shoulder.middle.pressed:
if self._view_manager is not None: if self._view_manager is not None:
self.on_exit()
self._view_manager.pop(ViewTransitionSwipeRight()) self._view_manager.pop(ViewTransitionSwipeRight())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment