Quellcode durchsuchen

persist alarm time

Fabian Peter Hammerle vor 2 Jahren
Ursprung
Commit
85ae8d45d6
1 geänderte Dateien mit 113 neuen und 92 gelöschten Zeilen
  1. 113 92
      vibrating_alarm_m5stickc.py

+ 113 - 92
vibrating_alarm_m5stickc.py

@@ -11,102 +11,123 @@ import micropython
 import utils
 from m5stack import axp, btnA, btnB, lcd
 
-FONT = lcd.FONT_DejaVu40
-DEFAULT_FONT_COLOR = lcd.WHITE
-ALARM_TIME_PATH = "alarm.json"
-SCREEN_WIDTH, SCREEN_HEIGHT = lcd.winsize()
-
-m5ui.setScreenColor(0x000000)  # clear screen
-
-print("battery:", axp.getBatVoltage(), "V")
-
-# > [contradictory to] official micropython documentation,
-# > to set RTC, use particular tuple (year , month, day, week=0, hour, minute, second, timestamp=0)
-# https://community.m5stack.com/topic/3108/m5stack-core2-micropython-rtc-example
-rtc = machine.RTC()
-# https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
-clock_text_box = m5ui.M5TextBox(
-    SCREEN_WIDTH - 1, 0, "HH:MM", FONT, DEFAULT_FONT_COLOR, rotate=90
-)
-clock_update_timer = machine.Timer(0)
-clock_update_timer.init(
-    period=10000,  # ms
-    mode=machine.Timer.PERIODIC,
-    callback=lambda t: clock_text_box.setText(
-        "{:02d}:{:02d}".format(*rtc.datetime()[4:6])
-    ),
-)
-
-if not utils.exists(ALARM_TIME_PATH):
-    with open(ALARM_TIME_PATH, "w") as alarm_time_file:
-        json.dump({"hour": 0, "minute": 0}, alarm_time_file)
-with open(ALARM_TIME_PATH, "r") as alarm_time_file:
-    alarm_time = json.load(alarm_time_file)
-alarm_hour_text_box = m5ui.M5TextBox(
-    SCREEN_WIDTH // 2,
-    0,
-    "{:02d}".format(alarm_time["hour"]),
-    FONT,
-    DEFAULT_FONT_COLOR,
-    rotate=90,
-)
-m5ui.M5TextBox(SCREEN_WIDTH // 2, 53, ":", FONT, DEFAULT_FONT_COLOR, rotate=90)
-alarm_minute_text_box = m5ui.M5TextBox(
-    SCREEN_WIDTH // 2,
-    66,
-    "{:02d}".format(alarm_time["minute"]),
-    FONT,
-    DEFAULT_FONT_COLOR,
-    rotate=90,
-)
-menu_position = 0  # pylint: disable=invalid-name
-
-
-def update_menu(event_arg: None):
-    # pylint: disable=unused-argument; callback
-    alarm_hour_text_box.setColor(
-        lcd.GREEN
-        if menu_position == 1
-        else (lcd.RED if menu_position == 2 else DEFAULT_FONT_COLOR)
+_FONT = lcd.FONT_DejaVu40
+_DEFAULT_FONT_COLOR = lcd.WHITE
+_ALARM_TIME_PATH = "alarm.json"
+_SCREEN_WIDTH, _SCREEN_HEIGHT = lcd.winsize()
+
+
+class App:
+    # pylint: disable=too-few-public-methods
+    def __init__(self):
+        self._menu_position = 0
+        self._alarm_hour = None
+        self._alarm_minute = None
+        self._alarm_hour_text_box = None
+        self._alarm_minute_text_box = None
+
+    def _load_alarm_time(self):
+        with open(_ALARM_TIME_PATH, "r") as alarm_time_file:
+            alarm_time = json.load(alarm_time_file)
+        self._alarm_hour = alarm_time["hour"]
+        self._alarm_minute = alarm_time["minute"]
+
+    def _save_alarm_time(self):
+        with open(_ALARM_TIME_PATH, "w") as alarm_time_file:
+            json.dump(
+                {"hour": self._alarm_hour, "minute": self._alarm_minute},
+                alarm_time_file,
+            )
+
+    def _update_menu(self, event_arg: None):
+        # pylint: disable=unused-argument; callback
+        self._alarm_hour_text_box.setColor(
+            lcd.GREEN
+            if self._menu_position == 1
+            else (lcd.RED if self._menu_position == 2 else _DEFAULT_FONT_COLOR)
+        )
+        self._alarm_minute_text_box.setColor(
+            lcd.GREEN
+            if self._menu_position == 3
+            else (lcd.RED if self._menu_position == 4 else _DEFAULT_FONT_COLOR)
+        )
+
+    def _button_a_pressed(self):
+        self._menu_position = (self._menu_position + 1) % 5
+        # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule
+        micropython.schedule(self._update_menu, None)
+
+    def _update_alarm_time(self, event_arg: None):
+        # pylint: disable=unused-argument; callback
+        self._alarm_hour_text_box.setText("{:02d}".format(self._alarm_hour))
+        self._alarm_minute_text_box.setText("{:02d}".format(self._alarm_minute))
+        self._save_alarm_time()
+
+    def _button_b_pressed(self):
+        if self._menu_position == 1:
+            self._alarm_hour += 1
+        elif self._menu_position == 2:
+            self._alarm_hour -= 1
+        elif self._menu_position == 3:
+            self._alarm_minute += 1
+        elif self._menu_position == 4:
+            self._alarm_minute -= 1
+        self._alarm_hour %= 24
+        self._alarm_minute %= 60
+        micropython.schedule(self._update_alarm_time, None)
+
+    def run(self):
+        if not utils.exists(_ALARM_TIME_PATH):
+            self._alarm_hour = self._alarm_minute = 0
+            self._save_alarm_time()
+        else:
+            self._load_alarm_time()
+        self._alarm_hour_text_box = m5ui.M5TextBox(
+            _SCREEN_WIDTH // 2,
+            0,
+            "{:02d}".format(self._alarm_hour),
+            _FONT,
+            _DEFAULT_FONT_COLOR,
+            rotate=90,
+        )
+        m5ui.M5TextBox(
+            _SCREEN_WIDTH // 2, 53, ":", _FONT, _DEFAULT_FONT_COLOR, rotate=90
+        )
+        self._alarm_minute_text_box = m5ui.M5TextBox(
+            _SCREEN_WIDTH // 2,
+            66,
+            "{:02d}".format(self._alarm_minute),
+            _FONT,
+            _DEFAULT_FONT_COLOR,
+            rotate=90,
+        )
+        btnA.wasPressed(self._button_a_pressed)
+        btnB.wasPressed(self._button_b_pressed)
+
+
+def main():
+    m5ui.setScreenColor(0x000000)  # clear screen
+    print("battery:", axp.getBatVoltage(), "V")
+    # > [contradictory to] official micropython documentation,
+    # > to set RTC, use particular tuple (year , month, day, week=0, hour, minute, second, timestamp=0)
+    # https://community.m5stack.com/topic/3108/m5stack-core2-micropython-rtc-example
+    rtc = machine.RTC()
+    # https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
+    clock_text_box = m5ui.M5TextBox(
+        _SCREEN_WIDTH - 1, 0, "HH:MM", _FONT, _DEFAULT_FONT_COLOR, rotate=90
     )
-    alarm_minute_text_box.setColor(
-        lcd.GREEN
-        if menu_position == 3
-        else (lcd.RED if menu_position == 4 else DEFAULT_FONT_COLOR)
+    clock_update_timer = machine.Timer(0)
+    clock_update_timer.init(
+        period=10000,  # ms
+        mode=machine.Timer.PERIODIC,
+        callback=lambda t: clock_text_box.setText(
+            "{:02d}:{:02d}".format(*rtc.datetime()[4:6])
+        ),
     )
+    App().run()
 
 
-def button_a_pressed():
-    global menu_position  # pylint: disable=global-statement,invalid-name
-    menu_position = (menu_position + 1) % 5
-    # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule
-    micropython.schedule(update_menu, None)
-
-
-def update_alarm_time(event_arg: None):
-    # pylint: disable=unused-argument; callback
-    alarm_hour_text_box.setText("{:02d}".format(alarm_time["hour"]))
-    alarm_minute_text_box.setText("{:02d}".format(alarm_time["minute"]))
-    # TODO persist alarm time
-
-
-def button_b_pressed():
-    global menu_position  # pylint: disable=global-statement,invalid-name
-    if menu_position == 1:
-        alarm_time["hour"] += 1
-    elif menu_position == 2:
-        alarm_time["hour"] -= 1
-    elif menu_position == 3:
-        alarm_time["minute"] += 1
-    elif menu_position == 4:
-        alarm_time["minute"] -= 1
-    alarm_time["hour"] %= 24
-    alarm_time["minute"] %= 60
-    micropython.schedule(update_alarm_time, None)
-
-
-btnA.wasPressed(button_a_pressed)
-btnB.wasPressed(button_b_pressed)
+main()
 
 # TODO https://docs.micropython.org/en/latest/library/machine.WDT.html#machine-wdt
 # TODO https://docs.micropython.org/en/latest/esp8266/tutorial/powerctrl.html#deep-sleep-mode