refactor: styles own their ringing tone directly (Pattern C)
Drop the injected AlarmSound Protocol and the wecker.Sound class. Each style now owns its tone by using pygame directly; the runner owns only the audio engine lifecycle (mixer init/quit), button sampling, and cleanup. This is the seam for future styles that handle their own tone — a 'talk' style would just import pygame and play speech, with no shared interface to extend. - styles/base.py: AlarmStyle.__init__(set_led, music_file) + start() + update() - styles/blink.py: start() loads+plays music via pygame.mixer.music - styles/simple.py: owns its beep — synthesises a square-wave buffer in module (stdlib array+math) and plays it via pygame.mixer.Sound - wecker.py: setup() brings up the mixer only; run_alarm constructs the style and guards start() with a clean log+exit on failure Tests: a shared tests/conftest.py stubs RPi.GPIO/pygame in sys.modules before any SUT import (order-independent, removes duplicated inline mocking); the wecker mock_pygame fixture patches one fresh pygame mock into wecker + both style modules so assertions see the same calls. README: tone-ownership and plugin-contract updated.
This commit is contained in:
+47
-17
@@ -1,3 +1,8 @@
|
||||
import array
|
||||
import math
|
||||
|
||||
import pygame
|
||||
|
||||
from styles.base import AlarmStyle
|
||||
|
||||
# Time between beep retriggers; a short beep followed by this gap gives the
|
||||
@@ -5,48 +10,73 @@ from styles.base import AlarmStyle
|
||||
BEEP_INTERVAL = 0.5
|
||||
|
||||
|
||||
def _make_beep_buffer(
|
||||
freq: int = 1000, duration: float = 0.15, rate: int = 44100
|
||||
) -> bytes:
|
||||
"""Synthesize a short square-wave beep as signed-16-bit stereo PCM.
|
||||
|
||||
Matches the mixer format used by wecker.setup() (rate, -16, 2 channels) so
|
||||
it feeds straight into ``pygame.mixer.Sound``. No audio file, no extra dep.
|
||||
"""
|
||||
n = int(rate * duration)
|
||||
buf = array.array("h")
|
||||
for i in range(n):
|
||||
v = 32767 if math.sin(2 * math.pi * freq * (i / rate)) >= 0 else -32767
|
||||
buf.append(v)
|
||||
buf.append(v) # duplicate to stereo
|
||||
return buf.tobytes()
|
||||
|
||||
|
||||
_BEEP_BUFFER = _make_beep_buffer()
|
||||
|
||||
|
||||
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``.
|
||||
This style plays no music file; it owns its tone via a synthesised square
|
||||
wave played through ``pygame.mixer.Sound``.
|
||||
"""
|
||||
|
||||
def __init__(self, set_led, sound, music_file=None):
|
||||
super().__init__(set_led, sound, music_file)
|
||||
def __init__(self, set_led, music_file=None):
|
||||
super().__init__(set_led, music_file)
|
||||
self._next_beep = 0.0
|
||||
self._beep = None
|
||||
|
||||
def start(self):
|
||||
# LED solid on so the button is findable in the dark.
|
||||
self.set_led(True)
|
||||
self._beep = pygame.mixer.Sound(_BEEP_BUFFER)
|
||||
|
||||
def update(self, now, is_pressed):
|
||||
self.set_led(True)
|
||||
if now >= self._next_beep:
|
||||
self.sound.play_beep()
|
||||
self._beep.play()
|
||||
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:
|
||||
# Self-check: the timer beeps on the interval and a press stops it.
|
||||
# Bypasses start() (which needs the audio engine) by stubbing the beep.
|
||||
|
||||
class _Beep:
|
||||
def __init__(self):
|
||||
self.beeps = 0
|
||||
self.plays = 0
|
||||
|
||||
def play_music(self, path):
|
||||
raise AssertionError("simple style must never play music")
|
||||
def play(self):
|
||||
self.plays += 1
|
||||
|
||||
def play_beep(self):
|
||||
self.beeps += 1
|
||||
style = SimpleStyle(lambda on: None)
|
||||
style._beep = _Beep()
|
||||
|
||||
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._beep.plays == 1 # first tick beeps
|
||||
|
||||
assert style.update(0.4, False) is True # before interval -> no beep
|
||||
assert sound.beeps == 1
|
||||
assert style._beep.plays == 1
|
||||
|
||||
assert style.update(0.5, False) is True # at interval -> beep
|
||||
assert sound.beeps == 2
|
||||
assert style._beep.plays == 2
|
||||
|
||||
assert style.update(0.6, True) is False # press -> stop
|
||||
print("simple self-check ok")
|
||||
|
||||
Reference in New Issue
Block a user