vibrating_alarm_m5stickc.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. tested on uiflow for stickc v1.8.1
  3. """
  4. # pylint: disable=import-error
  5. import json
  6. import m5ui
  7. import machine
  8. import micropython
  9. import utils
  10. from m5stack import axp, btnA, btnB, lcd
  11. FONT = lcd.FONT_DejaVu40
  12. DEFAULT_FONT_COLOR = lcd.WHITE
  13. ALARM_TIME_PATH = "alarm.json"
  14. SCREEN_WIDTH, SCREEN_HEIGHT = lcd.winsize()
  15. m5ui.setScreenColor(0x000000) # clear screen
  16. print("battery:", axp.getBatVoltage(), "V")
  17. # > [contradictory to] official micropython documentation,
  18. # > to set RTC, use particular tuple (year , month, day, week=0, hour, minute, second, timestamp=0)
  19. # https://community.m5stack.com/topic/3108/m5stack-core2-micropython-rtc-example
  20. rtc = machine.RTC()
  21. # https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
  22. clock_text_box = m5ui.M5TextBox(
  23. SCREEN_WIDTH - 1, 0, "HH:MM", FONT, DEFAULT_FONT_COLOR, rotate=90
  24. )
  25. clock_update_timer = machine.Timer(0)
  26. clock_update_timer.init(
  27. period=10000, # ms
  28. mode=machine.Timer.PERIODIC,
  29. callback=lambda t: clock_text_box.setText(
  30. "{:02d}:{:02d}".format(*rtc.datetime()[4:6])
  31. ),
  32. )
  33. if not utils.exists(ALARM_TIME_PATH):
  34. with open(ALARM_TIME_PATH, "w") as alarm_time_file:
  35. json.dump({"hour": 0, "minute": 0}, alarm_time_file)
  36. with open(ALARM_TIME_PATH, "r") as alarm_time_file:
  37. alarm_time = json.load(alarm_time_file)
  38. alarm_hour_text_box = m5ui.M5TextBox(
  39. SCREEN_WIDTH // 2,
  40. 0,
  41. "{:02d}".format(alarm_time["hour"]),
  42. FONT,
  43. DEFAULT_FONT_COLOR,
  44. rotate=90,
  45. )
  46. m5ui.M5TextBox(SCREEN_WIDTH // 2, 53, ":", FONT, DEFAULT_FONT_COLOR, rotate=90)
  47. alarm_minute_text_box = m5ui.M5TextBox(
  48. SCREEN_WIDTH // 2,
  49. 66,
  50. "{:02d}".format(alarm_time["minute"]),
  51. FONT,
  52. DEFAULT_FONT_COLOR,
  53. rotate=90,
  54. )
  55. menu_position = 0 # pylint: disable=invalid-name
  56. def update_menu(event_arg: None):
  57. # pylint: disable=unused-argument; callback
  58. alarm_hour_text_box.setColor(
  59. lcd.GREEN
  60. if menu_position == 1
  61. else (lcd.RED if menu_position == 2 else DEFAULT_FONT_COLOR)
  62. )
  63. alarm_minute_text_box.setColor(
  64. lcd.GREEN
  65. if menu_position == 3
  66. else (lcd.RED if menu_position == 4 else DEFAULT_FONT_COLOR)
  67. )
  68. def button_a_pressed():
  69. global menu_position # pylint: disable=global-statement,invalid-name
  70. menu_position = (menu_position + 1) % 5
  71. # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule
  72. micropython.schedule(update_menu, None)
  73. def update_alarm_time(event_arg: None):
  74. # pylint: disable=unused-argument; callback
  75. alarm_hour_text_box.setText("{:02d}".format(alarm_time["hour"]))
  76. alarm_minute_text_box.setText("{:02d}".format(alarm_time["minute"]))
  77. # TODO persist alarm time
  78. def button_b_pressed():
  79. global menu_position # pylint: disable=global-statement,invalid-name
  80. if menu_position == 1:
  81. alarm_time["hour"] += 1
  82. elif menu_position == 2:
  83. alarm_time["hour"] -= 1
  84. elif menu_position == 3:
  85. alarm_time["minute"] += 1
  86. elif menu_position == 4:
  87. alarm_time["minute"] -= 1
  88. alarm_time["hour"] %= 24
  89. alarm_time["minute"] %= 60
  90. micropython.schedule(update_alarm_time, None)
  91. btnA.wasPressed(button_a_pressed)
  92. btnB.wasPressed(button_b_pressed)
  93. # TODO https://docs.micropython.org/en/latest/library/machine.WDT.html#machine-wdt
  94. # TODO https://docs.micropython.org/en/latest/esp8266/tutorial/powerctrl.html#deep-sleep-mode