Browse Source

alert at beginning of minute

Fabian Peter Hammerle 2 years ago
parent
commit
1fd7713585
1 changed files with 19 additions and 12 deletions
  1. 19 12
      vibrating_alarm_m5stickc.py

+ 19 - 12
vibrating_alarm_m5stickc.py

@@ -20,7 +20,7 @@ _SCREEN_WIDTH, _SCREEN_HEIGHT = lcd.winsize()
 class App:
     # pylint: disable=too-few-public-methods,too-many-instance-attributes
     def __init__(self) -> None:
-        self._rtc = None
+        self._rtc = machine.RTC()
         self._clock_text_box = None
         self._menu_position = 0
         self._alarm_hour = None
@@ -29,6 +29,18 @@ class App:
         self._alarm_minute_text_box = None
         self._alarm_timer = None
 
+    @property
+    def _now_time(self) -> int:
+        # > [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
+        hour, minute, seconds = self._rtc.datetime()[4:7]
+        return (hour * 60 + minute) * 60 + seconds
+
+    @property
+    def _alarm_time(self) -> int:
+        return (self._alarm_hour * 60 + self._alarm_minute) * 60  # type: ignore
+
     def _load_alarm_time(self) -> None:
         with open(_ALARM_TIME_PATH, "r") as alarm_time_file:
             alarm_time = json.load(alarm_time_file)
@@ -70,13 +82,12 @@ class App:
         micropython.schedule(lambda n: self._configure_alarm_timer(), None)
 
     def _configure_alarm_timer(self) -> None:
-        now_hour, now_minute = self._rtc.datetime()[4:6]  # type: ignore
-        minutes_until_alarm = (
-            (self._alarm_hour - now_hour) * 60 + self._alarm_minute - now_minute - 1
-        ) % (24 * 60) + 1
-        print("alarm in ", minutes_until_alarm, " min")
+        seconds_until_alarm = (self._alarm_time - self._now_time - 1) % (
+            24 * 60 * 60
+        ) + 1
+        print("alarm in ", seconds_until_alarm / 60, " min")
         self._alarm_timer.init(  # type: ignore
-            period=minutes_until_alarm * 60 * 1000,  # ms
+            period=seconds_until_alarm * 1000,  # ms
             mode=machine.Timer.ONE_SHOT,
             callback=self._alarm,
         )
@@ -106,13 +117,9 @@ class App:
         micropython.schedule(self._update_alarm_time, None)
 
     def _format_time(self) -> str:
-        return "{:02d}:{:02d}".format(*self._rtc.datetime()[4:6])  # type: ignore
+        return "{:02d}:{:02d}".format(*self._rtc.datetime()[4:6])
 
     def _setup_clock(self) -> None:
-        # > [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,