2026-08-02 21:50:00 +02:00
|
|
|
import array
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
import pygame
|
|
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
from styles.base import AlarmStyle
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
# Time between beep retriggers; a short beep followed by this gap gives the
|
|
|
|
|
# classic alarm-clock "beep ... beep ... beep".
|
|
|
|
|
BEEP_INTERVAL = 0.5
|
|
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
class SimpleStyle(AlarmStyle):
|
2026-08-02 16:29:40 +02:00
|
|
|
"""Press the button once to stop. Rings with a repeating beep.
|
2026-07-31 16:19:14 +02:00
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
This style plays no music file; it owns its tone via a synthesised square
|
|
|
|
|
wave played through ``pygame.mixer.Sound``.
|
2026-08-02 16:29:40 +02:00
|
|
|
"""
|
|
|
|
|
|
2026-08-02 22:09:46 +02:00
|
|
|
def __init__(self, set_led, **kwargs):
|
|
|
|
|
super().__init__(set_led, **kwargs)
|
2026-08-02 16:29:40 +02:00
|
|
|
self._next_beep = 0.0
|
2026-08-02 21:50:00 +02:00
|
|
|
self._beep = None
|
2026-08-02 16:29:40 +02:00
|
|
|
|
|
|
|
|
def start(self):
|
2026-07-31 16:19:14 +02:00
|
|
|
# LED solid on so the button is findable in the dark.
|
|
|
|
|
self.set_led(True)
|
2026-08-02 21:50:00 +02:00
|
|
|
self._beep = pygame.mixer.Sound(_BEEP_BUFFER)
|
2026-08-02 16:29:40 +02:00
|
|
|
|
|
|
|
|
def update(self, now, is_pressed):
|
|
|
|
|
self.set_led(True)
|
|
|
|
|
if now >= self._next_beep:
|
2026-08-02 21:50:00 +02:00
|
|
|
self._beep.play()
|
2026-08-02 16:29:40 +02:00
|
|
|
self._next_beep = now + BEEP_INTERVAL
|
2026-07-31 16:19:14 +02:00
|
|
|
return not is_pressed # first press -> False -> stop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2026-08-02 21:50:00 +02:00
|
|
|
# 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:
|
2026-08-02 16:29:40 +02:00
|
|
|
def __init__(self):
|
2026-08-02 21:50:00 +02:00
|
|
|
self.plays = 0
|
2026-08-02 16:29:40 +02:00
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
def play(self):
|
|
|
|
|
self.plays += 1
|
2026-08-02 16:29:40 +02:00
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
style = SimpleStyle(lambda on: None)
|
|
|
|
|
style._beep = _Beep()
|
2026-08-02 16:29:40 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
assert style.update(0.0, False) is True
|
2026-08-02 21:50:00 +02:00
|
|
|
assert style._beep.plays == 1 # first tick beeps
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
assert style.update(0.4, False) is True # before interval -> no beep
|
2026-08-02 21:50:00 +02:00
|
|
|
assert style._beep.plays == 1
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
assert style.update(0.5, False) is True # at interval -> beep
|
2026-08-02 21:50:00 +02:00
|
|
|
assert style._beep.plays == 2
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
assert style.update(0.6, True) is False # press -> stop
|
2026-07-31 16:19:14 +02:00
|
|
|
print("simple self-check ok")
|