feat: styles own their ringing tone (simple beeps, blink plays music)

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.
This commit is contained in:
2026-08-02 16:29:40 +02:00
parent 88a57dab6e
commit 92eba762ce
8 changed files with 291 additions and 87 deletions
+36 -10
View File
@@ -1,23 +1,49 @@
from unittest.mock import MagicMock
from styles.simple import SimpleStyle
def _make_style():
sound = MagicMock()
style = SimpleStyle(lambda on: None, sound)
return style, sound
def test_simple_keeps_ringing_when_not_pressed():
calls = []
style = SimpleStyle(lambda on: calls.append(on))
style, _ = _make_style()
assert style.update(0.0, False) is True
assert calls[-1] is True # LED solid on while ringing
def test_simple_stops_on_first_press():
calls = []
style = SimpleStyle(lambda on: calls.append(on))
style, _ = _make_style()
assert style.update(0.0, True) is False # press -> stop
def test_simple_led_off_on_release_after_press():
# After a press the style stops; verify it never turns the LED off itself
# (the runner's cleanup handles that). It only ever asserts LED on.
def test_simple_led_solid_on():
calls = []
style = SimpleStyle(lambda on: calls.append(on))
style.update(0.0, False)
sound = MagicMock()
style = SimpleStyle(lambda on: calls.append(on), sound)
style.start()
assert calls == [True]
style.update(0.0, False)
assert calls[-1] is True # stays on every tick
def test_simple_beeps_on_interval():
style, sound = _make_style()
style.start()
style.update(100.0, False) # first tick -> beep
assert sound.play_beep.called
sound.play_beep.reset_mock()
style.update(100.4, False) # before interval -> no beep
assert not sound.play_beep.called
style.update(100.5, False) # at interval -> beep
assert sound.play_beep.called
def test_simple_never_plays_music():
style, sound = _make_style()
style.start()
style.update(0.0, False)
style.update(0.0, True) # press -> stop
assert not sound.play_music.called