""" 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, 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 DEFAULT_FONT_COLOR ) alarm_minute_text_box.setColor( lcd.GREEN if menu_position == 2 else DEFAULT_FONT_COLOR ) def button_a_pressed(): global menu_position # pylint: disable=global-statement,invalid-name menu_position = (menu_position + 1) % 3 # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule micropython.schedule(update_menu, menu_position) def button_b_pressed(): global menu_position # pylint: disable=global-statement,invalid-name if menu_position == 1: alarm_time["hour"] += 1 alarm_hour_text_box.setText("{:02d}".format(alarm_time["hour"])) elif menu_position == 2: alarm_time["minute"] += 1 alarm_minute_text_box.setText("{:02d}".format(alarm_time["minute"])) # TODO persist alarm time # micropython.schedule(update_menu, menu_position) btnA.wasPressed(button_a_pressed) btnB.wasPressed(button_b_pressed) # 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