feat: make command parameter optional in set_alarm mutation

This commit is contained in:
Markus Graf
2026-05-11 15:05:29 +02:00
parent 5b5b845e41
commit ff79224560
10 changed files with 152 additions and 98 deletions
+43 -32
View File
@@ -8,11 +8,8 @@ import sys
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s',
handlers=[
logging.FileHandler("wecker.log"),
logging.StreamHandler()
]
format="%(asctime)s - %(message)s",
handlers=[logging.FileHandler("wecker.log"), logging.StreamHandler()],
)
# GPIO Setup
@@ -33,7 +30,7 @@ try:
except Exception as e:
logging.error(f"Error loading audio file: {e}")
# Don't exit in test mode
if 'pytest' not in sys.modules:
if "pytest" not in sys.modules:
sys.exit(1)
# State Machine states for the flow
@@ -50,14 +47,16 @@ user_presses = 0
last_interaction_time = 0
button_was_pressed = False
def set_led(on):
"""Turns the LED on or off (LOW = ON, HIGH = OFF)"""
if 'GPIO' in globals() and hasattr(GPIO, 'output'):
if "GPIO" in globals() and hasattr(GPIO, "output"):
if on:
GPIO.output(LED_PIN, GPIO.LOW)
else:
GPIO.output(LED_PIN, GPIO.HIGH)
def blink_led(times):
"""Blinks the LED a specific number of times (blocking)"""
for _ in range(times):
@@ -66,6 +65,7 @@ def blink_led(times):
set_led(False)
time.sleep(0.3) # LED off for 300ms
class AlarmClock:
def __init__(self):
self.state = STATE_RINGING
@@ -84,66 +84,76 @@ class AlarmClock:
if self.state == STATE_RINGING:
if button_just_pressed:
logging.info("Alarm button pressed! Puzzle started. Waiting 3 seconds...")
logging.info(
"Alarm button pressed! Puzzle started. Waiting 3 seconds..."
)
self.state = STATE_WAIT_BEFORE_BLINK
self.last_interaction_time = now
elif self.state == STATE_WAIT_BEFORE_BLINK:
if now - self.last_interaction_time >= 3.0:
self.target_blinks = random.randint(1, 7)
logging.info(f"Blinking {self.target_blinks} times...")
self.state = STATE_BLINKING
elif self.state == STATE_BLINKING:
# Move the blink_led out of the update loop for testability,
# Move the blink_led out of the update loop for testability,
# or just call it directly. Here we call it.
blink_led(self.target_blinks)
logging.info("Blinking finished. Waiting for input...")
self.state = STATE_WAIT_FOR_INPUT
self.user_presses = 0
self.last_interaction_time = time.time() # Use time.time() to account for blocking blink_led
if 'GPIO' in globals() and hasattr(GPIO, 'input'):
self.button_was_pressed = (GPIO.input(BUTTON_PIN) == GPIO.LOW)
self.last_interaction_time = (
time.time()
) # Use time.time() to account for blocking blink_led
if "GPIO" in globals() and hasattr(GPIO, "input"):
self.button_was_pressed = GPIO.input(BUTTON_PIN) == GPIO.LOW
elif self.state == STATE_WAIT_FOR_INPUT:
set_led(is_pressed)
if button_just_pressed:
self.user_presses += 1
logging.info(f"Button pressed: {self.user_presses} times")
if is_pressed:
self.last_interaction_time = now
if not is_pressed and (now - self.last_interaction_time >= 3.0):
self.state = STATE_EVALUATING
elif self.state == STATE_EVALUATING:
logging.info(f"Evaluation: Target={self.target_blinks}, Entered={self.user_presses}")
logging.info(
f"Evaluation: Target={self.target_blinks}, Entered={self.user_presses}"
)
if self.user_presses == self.target_blinks:
logging.info("Puzzle solved correctly! Alarm clock is stopping.")
if 'pygame' in globals() and hasattr(pygame, 'mixer') and pygame.mixer.get_init():
if (
"pygame" in globals()
and hasattr(pygame, "mixer")
and pygame.mixer.get_init()
):
pygame.mixer.music.stop()
set_led(False)
return False # Indicate we should stop running
return False # Indicate we should stop running
else:
logging.info("Incorrect input! Waiting 3 seconds before retrying...")
self.state = STATE_WAIT_BEFORE_RETRY
self.last_interaction_time = time.time()
set_led(False)
elif self.state == STATE_WAIT_BEFORE_RETRY:
if now - self.last_interaction_time >= 5.0:
self.target_blinks = random.randint(1, 7)
logging.info(f"New attempt! Blinking {self.target_blinks} times...")
self.state = STATE_BLINKING
return True # Keep running
return True # Keep running
def run_alarm(test_mode=False):
logging.info("Alarm clock started. Music is playing in an endless loop.")
if 'pygame' in globals() and hasattr(pygame, 'mixer') and pygame.mixer.get_init():
if "pygame" in globals() and hasattr(pygame, "mixer") and pygame.mixer.get_init():
pygame.mixer.music.play(-1)
set_led(False) # LED off at start
@@ -153,22 +163,23 @@ def run_alarm(test_mode=False):
try:
while True:
now = time.time()
if 'GPIO' in globals() and hasattr(GPIO, 'input'):
is_pressed = (GPIO.input(BUTTON_PIN) == GPIO.LOW)
if "GPIO" in globals() and hasattr(GPIO, "input"):
is_pressed = GPIO.input(BUTTON_PIN) == GPIO.LOW
else:
is_pressed = False
keep_running = clock.update(now, is_pressed)
if not keep_running or test_mode:
break
time.sleep(0.02)
except KeyboardInterrupt:
logging.info("Manually aborted (CTRL+C).")
if __name__ == "__main__":
try:
run_alarm()