Files
wecker/styles/simple.py
T

53 lines
1.6 KiB
Python
Raw Normal View History

from styles.base import AlarmStyle
# Time between beep retriggers; a short beep followed by this gap gives the
# classic alarm-clock "beep ... beep ... beep".
BEEP_INTERVAL = 0.5
class SimpleStyle(AlarmStyle):
"""Press the button once to stop. Rings with a repeating beep.
This style plays no music file; it owns its tone via ``sound.play_beep``.
"""
def __init__(self, set_led, sound, music_file=None):
super().__init__(set_led, sound, music_file)
self._next_beep = 0.0
def start(self):
# LED solid on so the button is findable in the dark.
self.set_led(True)
def update(self, now, is_pressed):
self.set_led(True)
if now >= self._next_beep:
self.sound.play_beep()
self._next_beep = now + BEEP_INTERVAL
return not is_pressed # first press -> False -> stop
if __name__ == "__main__":
# Self-check: not pressed keeps ringing and beeps; a press stops it.
class _StubSound:
def __init__(self):
self.beeps = 0
def play_music(self, path):
raise AssertionError("simple style must never play music")
def play_beep(self):
self.beeps += 1
sound = _StubSound()
style = SimpleStyle(lambda on: None, sound)
style.start()
assert style.update(0.0, False) is True
assert sound.beeps == 1 # first tick beeps
assert style.update(0.4, False) is True # before interval -> no beep
assert sound.beeps == 1
assert style.update(0.5, False) is True # at interval -> beep
assert sound.beeps == 2
assert style.update(0.6, True) is False # press -> stop
print("simple self-check ok")