From 3063400eb6c3daffd5b5ebae789a6d2bc41312ff Mon Sep 17 00:00:00 2001 From: Daniel Hoffend <dh@dotlan.net> Date: Thu, 22 Aug 2019 18:48:08 +0200 Subject: [PATCH] feat(main.py) support color themes via json config --- preload/main.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/preload/main.py b/preload/main.py index 7f51be72..6ca514d6 100644 --- a/preload/main.py +++ b/preload/main.py @@ -5,6 +5,10 @@ import utime import math import leds import buttons +import ujson +import os + +CONFIG_NAME = "clock.json" class Time: def __init__(self, start = 0): @@ -65,14 +69,34 @@ class Clock: }, ] self.themes = self.default_themes - # TODO load themes from clock.json - # TODO load current theme from clock.json + + # check for config file + if CONFIG_NAME in os.listdir("."): + self.readConfig() + else: + self.writeConfig() # load colors - print("theme: ", self.theme) - print("themes: ", self.themes) self.setTheme(self.theme) + def readConfig(self): + with open(CONFIG_NAME, 'r') as f: + try: + c = ujson.loads(f.read()) + if "themes" in c and len(c["themes"]) > 0 and isinstance(c["themes"], list): + self.themes = c["themes"] + if "theme" and isinstance(c["theme"], int): + self.theme = c["theme"] + except ValueError: + print("parsing %s failed" % CONFIG_NAME) + + def writeConfig(self): + with open(CONFIG_NAME, 'w') as f: + f.write(ujson.dumps({ + "theme": self.theme, + "themes": self.themes, + })) + def setTheme(self, theme): self.theme = theme % len(self.themes) self.background_col = ( @@ -129,9 +153,11 @@ class Clock: if not button_pressed and v & buttons.BOTTOM_LEFT != 0: button_pressed = True self.setTheme(self.theme - 1) + self.writeConfig() elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0: button_pressed = True self.setTheme(self.theme + 1) + self.writeConfig() except KeyboardInterrupt: for i in range(11): -- GitLab