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:
+9
-11
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
from styles.base import AlarmStyle
|
||||
|
||||
# State Machine states for the puzzle flow.
|
||||
@@ -21,8 +23,8 @@ class BlinkStyle(AlarmStyle):
|
||||
exactly that many times. A wrong count starts a fresh sequence.
|
||||
"""
|
||||
|
||||
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.state = STATE_RINGING
|
||||
self.target_blinks = 0
|
||||
self.user_presses = 0
|
||||
@@ -35,7 +37,9 @@ class BlinkStyle(AlarmStyle):
|
||||
def start(self):
|
||||
# The puzzle rings with music on an endless loop; the LED stays off
|
||||
# until the button is pressed to start the blink sequence.
|
||||
self.sound.play_music(self.music_file)
|
||||
pygame.mixer.music.load(self.music_file)
|
||||
pygame.mixer.music.set_volume(1.0)
|
||||
pygame.mixer.music.play(-1)
|
||||
self.set_led(False)
|
||||
|
||||
def update(self, now, is_pressed):
|
||||
@@ -130,14 +134,8 @@ class BlinkStyle(AlarmStyle):
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Self-check: a correct blink sequence stops the alarm.
|
||||
class _StubSound:
|
||||
def play_music(self, path):
|
||||
pass
|
||||
|
||||
def play_beep(self):
|
||||
pass
|
||||
|
||||
style = BlinkStyle(lambda on: None, _StubSound(), "x.mp3")
|
||||
# (Does not call start(), which needs the audio engine.)
|
||||
style = BlinkStyle(lambda on: None, "x.mp3")
|
||||
|
||||
assert style.update(100.0, True) is True
|
||||
assert style.state == STATE_WAIT_BEFORE_BLINK
|
||||
|
||||
Reference in New Issue
Block a user