Introduce a styles/ plugin package: - styles/base.py: AlarmStyle ABC (injected set_led, update()->bool contract) - styles/blink.py: BlinkStyle, the existing count-the-blinks puzzle moved out of wecker.py's AlarmClock - styles/simple.py: SimpleStyle, press-once-to-stop (the new default style) - styles/__init__.py: STYLES registry + get_style() validator + LEGACY_STYLE wecker.py now resolves the style via the registry and owns only the shared music/button/cleanup loop; the --style CLI arg defaults to blink so existing cron entries keep their behaviour. Unknown styles raise before hardware init.
19 lines
565 B
Python
19 lines
565 B
Python
from styles.base import AlarmStyle
|
|
|
|
|
|
class SimpleStyle(AlarmStyle):
|
|
"""Press the button once to stop the alarm."""
|
|
|
|
def update(self, now, is_pressed):
|
|
# LED solid on so the button is findable in the dark.
|
|
self.set_led(True)
|
|
return not is_pressed # first press -> False -> stop
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Self-check: not pressed keeps ringing; a press stops it.
|
|
style = SimpleStyle(lambda on: None)
|
|
assert style.update(0.0, False) is True
|
|
assert style.update(0.1, True) is False
|
|
print("simple self-check ok")
|