vibrating_alarm_m5stickc.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. """
  2. tested on uiflow for stickc v1.8.1
  3. """
  4. import json
  5. # pylint: disable=import-error
  6. import esp32
  7. import m5ui
  8. import machine
  9. import micropython
  10. import utils
  11. import utime
  12. from m5stack import axp, btnA, btnB, lcd, rtc
  13. _LARGE_FONT = lcd.FONT_DejaVu40
  14. _SMALL_FONT = lcd.FONT_DejaVu18
  15. _DEFAULT_FONT_COLOR = lcd.WHITE
  16. _LEFT_PADDING = 8
  17. _ALARM_TIME_PATH = "alarm.json"
  18. _SCREEN_WIDTH, _SCREEN_HEIGHT = lcd.winsize()
  19. _AWAKE_SECONDS = 8
  20. def _handle_pending_events():
  21. # > [...] a millisecond sleep larger than 10ms will check for pending (soft)
  22. # > interrupts during the sleep. [...] The reason for the 10ms value is
  23. # > because the FreeRTOS tick is 10ms, [...]
  24. # https://github.com/micropython/micropython/issues/3493#issuecomment-352617624
  25. # https://github.com/micropython/micropython/commit/4ed586528047d3eced28a9f4af11dbbe64fa99bb
  26. utime.sleep_ms(11)
  27. def _get_current_daytime() -> int:
  28. # > [contradictory to] official micropython documentation, to set RTC,
  29. # > use particular tuple (year , month, day, week=0, hour, minute, second, timestamp=0)
  30. # https://community.m5stack.com/topic/3108/m5stack-core2-micropython-rtc-example
  31. hour, minute, seconds = rtc.now()[3:]
  32. return (hour * 60 + minute) * 60 + seconds
  33. class _AlarmConfig:
  34. def __init__(self):
  35. self._hour = 0
  36. self._minute = 0
  37. @property
  38. def hour(self) -> int:
  39. return self._hour
  40. @property
  41. def minute(self) -> int:
  42. return self._minute
  43. def shift_hour(self, delta: int) -> None:
  44. self._hour = (self._hour + delta) % 24
  45. def shift_minute(self, delta: int) -> None:
  46. self._minute = (self._minute + delta) % 60
  47. @property
  48. def daytime(self) -> int:
  49. return (self._hour * 60 + self._minute) * 60
  50. def load(self, path: str) -> None:
  51. with open(path, "r") as alarm_time_file:
  52. alarm_time = json.load(alarm_time_file)
  53. self._hour = alarm_time["hour"]
  54. self._minute = alarm_time["minute"]
  55. def save(self, path: str) -> None:
  56. with open(path, "w") as alarm_time_file:
  57. json.dump(
  58. {"hour": self._hour, "minute": self._minute},
  59. alarm_time_file,
  60. )
  61. class App:
  62. # pylint: disable=too-few-public-methods,too-many-instance-attributes
  63. def __init__(self) -> None:
  64. self._clock_text_box = None
  65. self._menu_position = 0
  66. self._wait_for_sleep_start_time_update = False
  67. self._sleep_start_time_seconds = 0.0
  68. self._alarm_config = _AlarmConfig()
  69. self._alarm_hour_text_box = None
  70. self._alarm_minute_text_box = None
  71. self._alarm_timer = None
  72. self._battery_status_text_box = None
  73. def _reschedule_sleep(self, interrupt: bool) -> None:
  74. if interrupt:
  75. self._wait_for_sleep_start_time_update = True
  76. micropython.schedule(self._reschedule_sleep, False)
  77. else:
  78. self._sleep_start_time_seconds = utime.time() + _AWAKE_SECONDS
  79. self._wait_for_sleep_start_time_update = False
  80. def _update_menu(self, event_arg: None) -> None:
  81. # pylint: disable=unused-argument; callback
  82. self._alarm_hour_text_box.setColor( # type: ignore
  83. lcd.GREEN
  84. if self._menu_position == 1
  85. else (lcd.RED if self._menu_position == 2 else _DEFAULT_FONT_COLOR)
  86. )
  87. self._alarm_minute_text_box.setColor( # type: ignore
  88. lcd.GREEN
  89. if self._menu_position == 3
  90. else (lcd.RED if self._menu_position == 4 else _DEFAULT_FONT_COLOR)
  91. )
  92. def _button_a_pressed(self) -> None:
  93. self._reschedule_sleep(interrupt=True)
  94. self._menu_position = (self._menu_position + 1) % 5
  95. # https://docs.micropython.org/en/latest/library/micropython.html#micropython.schedule
  96. micropython.schedule(self._update_menu, None)
  97. @staticmethod
  98. def _alert() -> None:
  99. print("ALARM")
  100. def _alarm(self, timer: machine.Timer) -> None:
  101. # pylint: disable=unused-argument; callback
  102. micropython.schedule(lambda n: self._alert(), None)
  103. micropython.schedule(lambda n: self._configure_alarm_timer(), None)
  104. def _configure_alarm_timer(self) -> None:
  105. seconds_until_alarm = (
  106. self._alarm_config.daytime - _get_current_daytime() - 1
  107. ) % (24 * 60 * 60) + 1
  108. print("alarm in ", seconds_until_alarm / 60, " min")
  109. # TODO configure wake from sleep
  110. self._alarm_timer.init( # type: ignore
  111. period=seconds_until_alarm * 1000, # ms
  112. mode=machine.Timer.ONE_SHOT,
  113. callback=self._alarm,
  114. )
  115. def _update_alarm_time(self, event_arg: None) -> None:
  116. # pylint: disable=unused-argument; callback
  117. self._alarm_hour_text_box.setText( # type: ignore
  118. "{:02d}".format(self._alarm_config.hour)
  119. )
  120. self._alarm_minute_text_box.setText( # type: ignore
  121. "{:02d}".format(self._alarm_config.minute)
  122. )
  123. self._alarm_config.save(_ALARM_TIME_PATH)
  124. self._configure_alarm_timer()
  125. def _button_b_pressed(self) -> None:
  126. self._reschedule_sleep(interrupt=True)
  127. if self._menu_position == 1:
  128. self._alarm_config.shift_hour(1)
  129. elif self._menu_position == 2:
  130. self._alarm_config.shift_hour(-1)
  131. elif self._menu_position == 3:
  132. self._alarm_config.shift_minute(1)
  133. elif self._menu_position == 4:
  134. self._alarm_config.shift_minute(-1)
  135. micropython.schedule(self._update_alarm_time, None)
  136. @staticmethod
  137. def _format_time() -> str:
  138. return "{:02d}:{:02d}".format(*rtc.now()[3:5])
  139. def _setup_clock(self) -> None:
  140. # https://github.com/m5stack/UIFlow-Code/wiki/M5UI#textbox
  141. self._clock_text_box = m5ui.M5TextBox(
  142. _SCREEN_WIDTH - 1,
  143. _LEFT_PADDING,
  144. self._format_time(),
  145. _LARGE_FONT,
  146. _DEFAULT_FONT_COLOR,
  147. rotate=90,
  148. )
  149. machine.Timer(0).init(
  150. period=4000, # ms
  151. mode=machine.Timer.PERIODIC,
  152. callback=lambda t: self._clock_text_box.setText(self._format_time()), # type: ignore
  153. )
  154. def _setup_alarm(self) -> None:
  155. if utils.exists(_ALARM_TIME_PATH):
  156. self._alarm_config.load(_ALARM_TIME_PATH)
  157. self._alarm_hour_text_box = m5ui.M5TextBox(
  158. _SCREEN_WIDTH // 2,
  159. _LEFT_PADDING,
  160. "{:02d}".format(self._alarm_config.hour),
  161. _LARGE_FONT,
  162. _DEFAULT_FONT_COLOR,
  163. rotate=90,
  164. )
  165. m5ui.M5TextBox(
  166. _SCREEN_WIDTH // 2,
  167. _LEFT_PADDING + 53,
  168. ":",
  169. _LARGE_FONT,
  170. _DEFAULT_FONT_COLOR,
  171. rotate=90,
  172. )
  173. self._alarm_minute_text_box = m5ui.M5TextBox(
  174. _SCREEN_WIDTH // 2,
  175. _LEFT_PADDING + 66,
  176. "{:02d}".format(self._alarm_config.minute),
  177. _LARGE_FONT,
  178. _DEFAULT_FONT_COLOR,
  179. rotate=90,
  180. )
  181. # machine.Timer(1).init(...) breaks button handling
  182. self._alarm_timer = machine.Timer(2)
  183. self._configure_alarm_timer()
  184. def _update_battery_status_info(self) -> None:
  185. self._battery_status_text_box.setText( # type: ignore
  186. "{:.02f}V".format(axp.getBatVoltage())
  187. )
  188. def _setup_battery_status_info(self) -> None:
  189. self._battery_status_text_box = m5ui.M5TextBox(
  190. 12,
  191. _SCREEN_HEIGHT - 20,
  192. "",
  193. _SMALL_FONT,
  194. _DEFAULT_FONT_COLOR,
  195. rotate=0,
  196. )
  197. def run(self) -> None:
  198. m5ui.setScreenColor(0x000000) # clear screen
  199. self._setup_clock()
  200. self._setup_alarm()
  201. self._setup_battery_status_info()
  202. btnA.wasPressed(self._button_a_pressed)
  203. btnB.wasPressed(self._button_b_pressed)
  204. # not sure whether ext0 would be better
  205. esp32.wake_on_ext1([btnA.pin], esp32.WAKEUP_ALL_LOW)
  206. self._reschedule_sleep(interrupt=False)
  207. while True:
  208. self._update_battery_status_info()
  209. while utime.time() < self._sleep_start_time_seconds:
  210. utime.sleep(1) # seconds
  211. # TODO turn off display
  212. axp.setLcdBrightness(30)
  213. machine.lightsleep()
  214. # TODO turn on display
  215. axp.setLcdBrightness(100)
  216. print("wake reason:", machine.wake_reason())
  217. _handle_pending_events()
  218. while self._wait_for_sleep_start_time_update:
  219. _handle_pending_events()
  220. App().run()