vibrating_alarm_m5stickc.py 6.2 KB

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