From 00e973cf42cec7448969dee7b0369bae6d2997b3 Mon Sep 17 00:00:00 2001 From: Aaro Varis Date: Thu, 5 Feb 2026 09:45:57 +0200 Subject: [PATCH] a --- init.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/init.py b/init.py index 95c17ac..0277bb0 100644 --- a/init.py +++ b/init.py @@ -88,14 +88,16 @@ class AlarmClock: longPressThreshold: float = 1.0 # 1 second for long press buttonPressStartTime: float = 0.0 configMode: str = "normal" # modes: normal, set_hour, set_minute - alarmTime: datetime = datetime.now(ZoneInfo("Europe/Helsinki")).replace(hour=9, minute=32, second=0, microsecond=0) + alarmTime: datetime = datetime.now(ZoneInfo("Europe/Helsinki")).replace(hour=9, minute=48, second=0, microsecond=0) actionsResetForToday: bool = False + alarmActive: bool = False def __init__(self, pins: dict = {}): self.ledGpio = GPIO(pins.get("led", 5), GPIO.OUT) self.buttonGpio = GPIO(pins.get("button", 6), GPIO.IN) self.relayGpio = GPIO(pins.get("relay", 16), GPIO.OUT) self.lcd = JHD1802() self.wakeupActions = [] + self.addWakeUpAction(0, lambda: self.triggerAlarm()) # Activate alarm state self.setLcdText("AlarmClock Init") def addWakeUpAction(self, offsetSeconds: int, action: Callable[[], None]): @@ -103,12 +105,23 @@ class AlarmClock: self.wakeupActions.append(WakeUpAction(offsetSeconds, action)) def resetWakeUpActions(self): - """Reset all wakeup actions so they can trigger again""" for action in self.wakeupActions: action.triggered = False self.actionsResetForToday = True print("Wakeup actions reset") + def triggerAlarm(self): + if self.alarmActive: + return + self.alarmActive = True + print("Alarm is now active!") + + def dismissAlarm(self): + if not self.alarmActive: + return + self.alarmActive = False + print("Alarm dismissed") + def checkWakeUpActions(self): """Check and trigger any wakeup actions that are due""" if not self.wakeupActions: @@ -163,7 +176,10 @@ class AlarmClock: currentHour = datetime.now(ZoneInfo(city.timezone)).hour currrentSecond = datetime.now(ZoneInfo(city.timezone)).second - self.setLcdText(f"Time {currentHour:02d}:{currentMinute:02d}:{currrentSecond:02d}\nAlarm {self.alarmTime.hour:02d}:{self.alarmTime.minute:02d}") + if self.alarmActive: + self.setLcdText(f"!! ALARM !!\nPress to dismiss") + else: + self.setLcdText(f"Time {currentHour:02d}:{currentMinute:02d}:{currrentSecond:02d}\nAlarm {self.alarmTime.hour:02d}:{self.alarmTime.minute:02d}") # Check and trigger wakeup actions self.checkWakeUpActions() @@ -201,9 +217,13 @@ class AlarmClock: def onShortPress(self): """Handle short button press based on current mode""" if self.configMode == "normal": - # Normal mode: toggle relay - self.setRelayState(not self.relayGpio.read()) - setShellyPlugState(shelly_deviceId, self.relayGpio.read() == 0) + if self.alarmActive: + # Dismiss active alarm + self.dismissAlarm() + else: + # Normal mode: toggle relay + self.setRelayState(not self.relayGpio.read()) + setShellyPlugState(shelly_deviceId, self.relayGpio.read() == 0) elif self.configMode == "set_hour": # Increment hour self.alarmTime = self.alarmTime.replace(hour=(self.alarmTime.hour + 1) % 24) @@ -260,6 +280,7 @@ alarm_clock.addWakeUpAction(120, lambda: print("2 minutes to alarm!")) # 2 minu alarm_clock.addWakeUpAction(90, lambda: setShellyPlugState(shelly_deviceId, True)) # activate plug 90s before alarm alarm_clock.addWakeUpAction(30, lambda: print("30 seconds to alarm!")) # 30 seconds before alarm alarm_clock.addWakeUpAction(0, lambda: print("Alarm triggered!")) # At alarm time + alarm_clock.addWakeUpAction(0, lambda: playAudio("./boxing_bell_multiple.wav")) # Play sound at alarm time alarm_clock.start()