Bladeren bron

prepare persistence of alarm time

Fabian Peter Hammerle 2 jaren geleden
bovenliggende
commit
f923fda6a4
1 gewijzigde bestanden met toevoegingen van 33 en 15 verwijderingen
  1. 33 15
      vibrating_alarm_m5stickc.py

+ 33 - 15
vibrating_alarm_m5stickc.py

@@ -3,35 +3,31 @@ tested on uiflow for stickc v1.8.1
 """
 
 # pylint: disable=import-error
+import json
+
 import m5ui
 import machine
 import micropython
+import utils
 from m5stack import axp, btnA, lcd
 
 FONT = lcd.FONT_DejaVu40
 DEFAULT_FONT_COLOR = lcd.WHITE
-
-print("battery:", axp.getBatVoltage(), "V")
+ALARM_TIME_PATH = "alarm.json"
+SCREEN_WIDTH, SCREEN_HEIGHT = lcd.winsize()
 
 m5ui.setScreenColor(0x000000)  # clear screen
-screen_width, screen_height = lcd.winsize()
-# 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_hour_text_box = m5ui.M5TextBox(
-    screen_width // 2, 0, "00", 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, "00", FONT, DEFAULT_FONT_COLOR, rotate=90
-)
+
+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
@@ -41,6 +37,28 @@ clock_update_timer.init(
     ),
 )
 
+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