Move audio ownership out of the shared runner and into each style via an injected Sound capability (play_music(path) / play_beep()). The runner now only owns button sampling and cleanup; the style kicks off its own tone in start() and drives the LED. - simple: a repeating square-wave beep (ordinary alarm-clock tone), no music file required. The beep is synthesised in memory as signed-16-bit stereo PCM (stdlib array+math) and played via pygame.mixer.Sound — no shipped audio asset, no new dependency. - blink: unchanged behaviour — music on an endless loop from --music-file / MUSIC_FILE / the default track, via pygame.mixer.music. - AlarmStyle contract gains sound + music_file in __init__ and a start() lifecycle hook; AlarmSound Protocol documents the audio seam for future styles that handle their own tone. - setup() no longer loads music (that is the style's job now). README updated: per-style ringing tone, the --music-file note (blink only), and the extended plugin contract.
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
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")
|