ソースを参照

random vibration pattern

Fabian Peter Hammerle 2 年 前
コミット
4e8ca811c3
1 ファイル変更11 行追加2 行削除
  1. 11 2
      vibrating_alarm_m5stickc.py

+ 11 - 2
vibrating_alarm_m5stickc.py

@@ -9,6 +9,7 @@ import esp32
 import m5ui
 import machine
 import micropython
+import uos
 import utils
 import utime
 from m5stack import axp, btnA, btnB, lcd, rtc
@@ -49,6 +50,14 @@ def _sleep(duration_seconds: int) -> None:
     print("wake reason:", machine.wake_reason())
 
 
+def _random() -> float:  # like random.random
+    return ord(uos.urandom(1)) / (1 << 8)
+
+
+def _randrange(start: float, stop: float) -> float:  # like random.randrange
+    return _random() * (stop - start) + start
+
+
 class _AlarmConfig:
     def __init__(self):
         self._hour = 0
@@ -145,9 +154,9 @@ class App:
         self._alarm_active = True
         while self._alarm_active:
             self._vibration_motor_pin.on()  # type: ignore
-            utime.sleep(0.5)
+            utime.sleep(_randrange(0.2, 0.8))
             self._vibration_motor_pin.off()  # type: ignore
-            utime.sleep(0.5)
+            utime.sleep(_randrange(0.2, 0.8))
         self._next_alarm_time = self._alarm_config.next_time
 
     def _update_alarm_time(self, event_arg: None) -> None: