2026-04-30 13:32:32 +02:00
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
|
import time
|
|
|
|
|
import logging
|
|
|
|
|
import pygame
|
|
|
|
|
import random
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
# Configure logging
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
format='%(asctime)s - %(message)s',
|
|
|
|
|
handlers=[
|
|
|
|
|
logging.FileHandler("wecker.log"),
|
|
|
|
|
logging.StreamHandler()
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# GPIO Setup
|
|
|
|
|
BUTTON_PIN = 17
|
|
|
|
|
LED_PIN = 27
|
|
|
|
|
|
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
|
GPIO.setup(LED_PIN, GPIO.OUT)
|
|
|
|
|
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
|
|
|
|
|
|
|
|
|
# Audio Setup (with optimized buffer)
|
|
|
|
|
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
|
|
|
|
|
pygame.mixer.init()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
pygame.mixer.music.load("Laid Back - Sunshine Reggae.mp3")
|
|
|
|
|
pygame.mixer.music.set_volume(1.0)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"Error loading audio file: {e}")
|
2026-05-09 22:31:28 +02:00
|
|
|
# Don't exit in test mode
|
|
|
|
|
if 'pytest' not in sys.modules:
|
|
|
|
|
sys.exit(1)
|
2026-04-30 13:32:32 +02:00
|
|
|
|
|
|
|
|
# State Machine states for the flow
|
|
|
|
|
STATE_RINGING = 0
|
|
|
|
|
STATE_WAIT_BEFORE_BLINK = 1
|
|
|
|
|
STATE_BLINKING = 2
|
|
|
|
|
STATE_WAIT_FOR_INPUT = 3
|
|
|
|
|
STATE_EVALUATING = 4
|
|
|
|
|
STATE_WAIT_BEFORE_RETRY = 5
|
|
|
|
|
|
|
|
|
|
state = STATE_RINGING
|
|
|
|
|
target_blinks = 0
|
|
|
|
|
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)"""
|
2026-05-09 22:31:28 +02:00
|
|
|
if 'GPIO' in globals() and hasattr(GPIO, 'output'):
|
|
|
|
|
if on:
|
|
|
|
|
GPIO.output(LED_PIN, GPIO.LOW)
|
|
|
|
|
else:
|
|
|
|
|
GPIO.output(LED_PIN, GPIO.HIGH)
|
2026-04-30 13:32:32 +02:00
|
|
|
|
|
|
|
|
def blink_led(times):
|
|
|
|
|
"""Blinks the LED a specific number of times (blocking)"""
|
|
|
|
|
for _ in range(times):
|
|
|
|
|
set_led(True)
|
|
|
|
|
time.sleep(0.3) # LED on for 300ms
|
|
|
|
|
set_led(False)
|
|
|
|
|
time.sleep(0.3) # LED off for 300ms
|
|
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
class AlarmClock:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.state = STATE_RINGING
|
|
|
|
|
self.target_blinks = 0
|
|
|
|
|
self.user_presses = 0
|
|
|
|
|
self.last_interaction_time = 0
|
|
|
|
|
self.button_was_pressed = False
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
def update(self, now, is_pressed):
|
2026-04-30 13:32:32 +02:00
|
|
|
button_just_pressed = False
|
2026-05-09 22:31:28 +02:00
|
|
|
if is_pressed and not self.button_was_pressed:
|
|
|
|
|
self.button_was_pressed = True
|
2026-04-30 13:32:32 +02:00
|
|
|
button_just_pressed = True
|
2026-05-09 22:31:28 +02:00
|
|
|
elif not is_pressed and self.button_was_pressed:
|
|
|
|
|
self.button_was_pressed = False
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
if self.state == STATE_RINGING:
|
2026-04-30 13:32:32 +02:00
|
|
|
if button_just_pressed:
|
|
|
|
|
logging.info("Alarm button pressed! Puzzle started. Waiting 3 seconds...")
|
2026-05-09 22:31:28 +02:00
|
|
|
self.state = STATE_WAIT_BEFORE_BLINK
|
|
|
|
|
self.last_interaction_time = now
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
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
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
elif self.state == STATE_BLINKING:
|
|
|
|
|
# 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)
|
2026-04-30 13:32:32 +02:00
|
|
|
logging.info("Blinking finished. Waiting for input...")
|
2026-05-09 22:31:28 +02:00
|
|
|
self.state = STATE_WAIT_FOR_INPUT
|
|
|
|
|
self.user_presses = 0
|
2026-05-10 07:41:00 +02:00
|
|
|
self.last_interaction_time = time.time() # Use time.time() to account for blocking blink_led
|
2026-05-09 22:31:28 +02:00
|
|
|
if 'GPIO' in globals() and hasattr(GPIO, 'input'):
|
|
|
|
|
self.button_was_pressed = (GPIO.input(BUTTON_PIN) == GPIO.LOW)
|
|
|
|
|
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
elif self.state == STATE_WAIT_FOR_INPUT:
|
2026-04-30 13:32:32 +02:00
|
|
|
set_led(is_pressed)
|
|
|
|
|
|
|
|
|
|
if button_just_pressed:
|
2026-05-09 22:31:28 +02:00
|
|
|
self.user_presses += 1
|
|
|
|
|
logging.info(f"Button pressed: {self.user_presses} times")
|
2026-04-30 13:32:32 +02:00
|
|
|
|
|
|
|
|
if is_pressed:
|
2026-05-09 22:31:28 +02:00
|
|
|
self.last_interaction_time = now
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
if not is_pressed and (now - self.last_interaction_time >= 3.0):
|
|
|
|
|
self.state = STATE_EVALUATING
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
elif self.state == STATE_EVALUATING:
|
|
|
|
|
logging.info(f"Evaluation: Target={self.target_blinks}, Entered={self.user_presses}")
|
|
|
|
|
if self.user_presses == self.target_blinks:
|
2026-04-30 13:32:32 +02:00
|
|
|
logging.info("Puzzle solved correctly! Alarm clock is stopping.")
|
2026-05-09 22:31:28 +02:00
|
|
|
if 'pygame' in globals() and hasattr(pygame, 'mixer') and pygame.mixer.get_init():
|
|
|
|
|
pygame.mixer.music.stop()
|
2026-04-30 13:32:32 +02:00
|
|
|
set_led(False)
|
2026-05-09 22:31:28 +02:00
|
|
|
return False # Indicate we should stop running
|
2026-04-30 13:32:32 +02:00
|
|
|
else:
|
|
|
|
|
logging.info("Incorrect input! Waiting 3 seconds before retrying...")
|
2026-05-09 22:31:28 +02:00
|
|
|
self.state = STATE_WAIT_BEFORE_RETRY
|
|
|
|
|
self.last_interaction_time = time.time()
|
2026-04-30 13:32:32 +02:00
|
|
|
set_led(False)
|
|
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
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
|
2026-04-30 13:32:32 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
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():
|
|
|
|
|
pygame.mixer.music.play(-1)
|
|
|
|
|
|
|
|
|
|
set_led(False) # LED off at start
|
|
|
|
|
|
|
|
|
|
clock = AlarmClock()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
finally:
|
|
|
|
|
pygame.mixer.quit()
|
|
|
|
|
GPIO.cleanup()
|
|
|
|
|
logging.info("Program finished.")
|