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:
+35
-7
@@ -1,20 +1,48 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Callable
|
||||
from typing import Callable, Protocol
|
||||
|
||||
|
||||
class AlarmSound(Protocol):
|
||||
"""Audio capability injected into a style so it can own its ringing tone.
|
||||
|
||||
Styles stay free of pygame/GPIO imports (the API process imports them only
|
||||
for name validation); the runner supplies a concrete implementation.
|
||||
"""
|
||||
|
||||
def play_music(self, path: str) -> None:
|
||||
"""Load and play a music file on an endless loop."""
|
||||
...
|
||||
|
||||
def play_beep(self) -> None:
|
||||
"""Play one short beep tone."""
|
||||
...
|
||||
|
||||
|
||||
class AlarmStyle(ABC):
|
||||
"""A pluggable alarm behaviour.
|
||||
|
||||
A style decides *when* the alarm stops and what the LED does while it
|
||||
rings. It never touches hardware directly: ``set_led`` is injected by the
|
||||
runner, so styles stay import-safe in the API process (no GPIO/pygame).
|
||||
A style owns *its own ringing tone* (via the injected ``sound``) and
|
||||
decides when the alarm stops and what the LED does. It never touches
|
||||
hardware directly: ``set_led`` and ``sound`` are injected by the runner, so
|
||||
styles stay import-safe in the API process (no GPIO/pygame).
|
||||
|
||||
Lifecycle: construct once with ``set_led``, then call ``update`` every
|
||||
tick. Return ``False`` from ``update`` to stop the alarm.
|
||||
Lifecycle: construct once with ``set_led``/``sound``/``music_file``; call
|
||||
``start()`` to begin ringing (kick off the tone and initial LED); then call
|
||||
``update`` every tick. Return ``False`` from ``update`` to stop the alarm.
|
||||
"""
|
||||
|
||||
def __init__(self, set_led: Callable[[bool], None]):
|
||||
def __init__(
|
||||
self,
|
||||
set_led: Callable[[bool], None],
|
||||
sound: AlarmSound,
|
||||
music_file: str | None = None,
|
||||
):
|
||||
self.set_led = set_led
|
||||
self.sound = sound
|
||||
self.music_file = music_file
|
||||
|
||||
def start(self) -> None:
|
||||
"""Begin ringing. Override to start the style's tone / initial LED."""
|
||||
|
||||
@abstractmethod
|
||||
def update(self, now: float, is_pressed: bool) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user