Browse Source

initialize clock immediately

Fabian Peter Hammerle 2 năm trước cách đây
mục cha
commit
27218df98c
1 tập tin đã thay đổi với 29 bổ sung23 xóa
  1. 29 23
      vibrating_alarm_m5stickc.py

+ 29 - 23
vibrating_alarm_m5stickc.py

@@ -20,6 +20,8 @@ _SCREEN_WIDTH, _SCREEN_HEIGHT = lcd.winsize()
 class App:
     # pylint: disable=too-few-public-methods
     def __init__(self):
+        self._rtc = None
+        self._clock_text_box = None
         self._menu_position = 0
         self._alarm_hour = None
         self._alarm_minute = None
@@ -76,7 +78,33 @@ class App:
         self._alarm_minute %= 60
         micropython.schedule(self._update_alarm_time, None)
 
+    def _format_time(self) -> str:
+        return "{:02d}:{:02d}".format(*self._rtc.datetime()[4:6])
+
+    def _setup_clock(self):
+        # > [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
+        self._rtc = machine.RTC()
+        # https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
+        self._clock_text_box = m5ui.M5TextBox(
+            _SCREEN_WIDTH - 1,
+            0,
+            self._format_time(),
+            _FONT,
+            _DEFAULT_FONT_COLOR,
+            rotate=90,
+        )
+        machine.Timer(0).init(
+            period=10000,  # ms
+            mode=machine.Timer.PERIODIC,
+            callback=lambda t: self._clock_text_box.setText(self._format_time()),
+        )
+
     def run(self):
+        m5ui.setScreenColor(0x000000)  # clear screen
+        print("battery:", axp.getBatVoltage(), "V")
+        self._setup_clock()
         if not utils.exists(_ALARM_TIME_PATH):
             self._alarm_hour = self._alarm_minute = 0
             self._save_alarm_time()
@@ -105,29 +133,7 @@ class App:
         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
-    )
-    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()
-
-
-main()
+App().run()
 
 # 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