vibrating_alarm_m5stickc.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. class App:
  16. # pylint: disable=too-few-public-methods,too-many-instance-attributes
  17. def __init__(self) -> None:
  18. self._rtc = None
  19. self._clock_text_box = None
  20. self._menu_position = 0
  21. self._alarm_hour = None
  22. self._alarm_minute = None
  23. self._alarm_hour_text_box = None
  24. self._alarm_minute_text_box = None
  25. self._alarm_timer = None
  26. def _load_alarm_time(self) -> None:
  27. with open(_ALARM_TIME_PATH, "r") as alarm_time_file:
  28. alarm_time = json.load(alarm_time_file)
  29. self._alarm_hour = alarm_time["hour"]
  30. self._alarm_minute = alarm_time["minute"]
  31. def _save_alarm_time(self) -> None:
  32. with open(_ALARM_TIME_PATH, "w") as alarm_time_file:
  33. json.dump(
  34. {"hour": self._alarm_hour, "minute": self._alarm_minute},
  35. alarm_time_file,
  36. )
  37. def _update_menu(self, event_arg: None) -> None:
  38. # pylint: disable=unused-argument; callback
  39. self._alarm_hour_text_box.setColor( # type: ignore
  40. lcd.GREEN
  41. if self._menu_position == 1
  42. else (lcd.RED if self._menu_position == 2 else _DEFAULT_FONT_COLOR)
  43. )
  44. self._alarm_minute_text_box.setColor( # type: ignore
  45. lcd.GREEN
  46. if self._menu_position == 3
  47. else (lcd.RED if self._menu_position == 4 else _DEFAULT_FONT_COLOR)
  48. )
  49. def _button_a_pressed(self) -> None:
  50. self._menu_position = (self._menu_position + 1) % 5
  51. # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule
  52. micropython.schedule(self._update_menu, None)
  53. @staticmethod
  54. def _alert() -> None:
  55. print("ALARM")
  56. def _alarm(self, timer: machine.Timer) -> None:
  57. # pylint: disable=unused-argument; callback
  58. micropython.schedule(lambda n: self._alert(), None)
  59. micropython.schedule(lambda n: self._configure_alarm_timer(), None)
  60. def _configure_alarm_timer(self) -> None:
  61. now_hour, now_minute = self._rtc.datetime()[4:6] # type: ignore
  62. minutes_until_alarm = (
  63. (self._alarm_hour - now_hour) * 60 + self._alarm_minute - now_minute - 1
  64. ) % (24 * 60) + 1
  65. print("alarm in ", minutes_until_alarm, " min")
  66. self._alarm_timer.init( # type: ignore
  67. period=minutes_until_alarm * 60 * 1000, # ms
  68. mode=machine.Timer.ONE_SHOT,
  69. callback=self._alarm,
  70. )
  71. def _update_alarm_time(self, event_arg: None) -> None:
  72. # pylint: disable=unused-argument; callback
  73. self._alarm_hour_text_box.setText( # type: ignore
  74. "{:02d}".format(self._alarm_hour) # type: ignore
  75. )
  76. self._alarm_minute_text_box.setText( # type: ignore
  77. "{:02d}".format(self._alarm_minute) # type: ignore
  78. )
  79. self._save_alarm_time()
  80. self._configure_alarm_timer()
  81. def _button_b_pressed(self) -> None:
  82. if self._menu_position == 1:
  83. self._alarm_hour += 1 # type: ignore
  84. elif self._menu_position == 2:
  85. self._alarm_hour -= 1 # type: ignore
  86. elif self._menu_position == 3:
  87. self._alarm_minute += 1 # type: ignore
  88. elif self._menu_position == 4:
  89. self._alarm_minute -= 1 # type: ignore
  90. self._alarm_hour %= 24 # type: ignore
  91. self._alarm_minute %= 60 # type: ignore
  92. micropython.schedule(self._update_alarm_time, None)
  93. def _format_time(self) -> str:
  94. return "{:02d}:{:02d}".format(*self._rtc.datetime()[4:6]) # type: ignore
  95. def _setup_clock(self) -> None:
  96. # > [contradictory to] official micropython documentation, to set RTC,
  97. # > use particular tuple (year , month, day, week=0, hour, minute, second, timestamp=0)
  98. # https://community.m5stack.com/topic/3108/m5stack-core2-micropython-rtc-example
  99. self._rtc = machine.RTC()
  100. # https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
  101. self._clock_text_box = m5ui.M5TextBox(
  102. _SCREEN_WIDTH - 1,
  103. 0,
  104. self._format_time(),
  105. _FONT,
  106. _DEFAULT_FONT_COLOR,
  107. rotate=90,
  108. )
  109. machine.Timer(0).init(
  110. period=10000, # ms
  111. mode=machine.Timer.PERIODIC,
  112. callback=lambda t: self._clock_text_box.setText(self._format_time()), # type: ignore
  113. )
  114. def _setup_alarm(self) -> None:
  115. if not utils.exists(_ALARM_TIME_PATH):
  116. self._alarm_hour = self._alarm_minute = 0 # type: ignore
  117. self._save_alarm_time()
  118. else:
  119. self._load_alarm_time()
  120. self._alarm_hour_text_box = m5ui.M5TextBox(
  121. _SCREEN_WIDTH // 2,
  122. 0,
  123. "{:02d}".format(self._alarm_hour), # type: ignore
  124. _FONT,
  125. _DEFAULT_FONT_COLOR,
  126. rotate=90,
  127. )
  128. m5ui.M5TextBox(
  129. _SCREEN_WIDTH // 2, 53, ":", _FONT, _DEFAULT_FONT_COLOR, rotate=90
  130. )
  131. self._alarm_minute_text_box = m5ui.M5TextBox(
  132. _SCREEN_WIDTH // 2,
  133. 66,
  134. "{:02d}".format(self._alarm_minute), # type: ignore
  135. _FONT,
  136. _DEFAULT_FONT_COLOR,
  137. rotate=90,
  138. )
  139. # machine.Timer(1).init(...) breaks button handling
  140. self._alarm_timer = machine.Timer(2)
  141. self._configure_alarm_timer()
  142. def run(self) -> None:
  143. m5ui.setScreenColor(0x000000) # clear screen
  144. print("battery:", axp.getBatVoltage(), "V")
  145. self._setup_clock()
  146. self._setup_alarm()
  147. btnA.wasPressed(self._button_a_pressed)
  148. btnB.wasPressed(self._button_b_pressed)
  149. App().run()
  150. # TODO https://docs.micropython.org/en/latest/library/machine.WDT.html#machine-wdt
  151. # TODO https://docs.micropython.org/en/latest/esp8266/tutorial/powerctrl.html#deep-sleep-mode