Refactor AlarmClock to remove relay functionality and add Relay class for GPIO control
This commit is contained in:
@@ -17,7 +17,6 @@ class AlarmClock:
|
||||
wakeup_actions: list[WakeUpAction] = []
|
||||
led_gpio: GPIO
|
||||
button_gpio: GPIO
|
||||
relay_gpio: GPIO
|
||||
last_known_button_state: bool = False
|
||||
lcd: JHD1802
|
||||
last_button_press_time: float = 0.0
|
||||
@@ -36,7 +35,6 @@ class AlarmClock:
|
||||
def __init__(self, pins: dict = {}):
|
||||
self.led_gpio = GPIO(pins.get("led", 5), GPIO.OUT)
|
||||
self.button_gpio = GPIO(pins.get("button", 6), GPIO.IN)
|
||||
self.relay_gpio = GPIO(pins.get("relay", 16), GPIO.OUT)
|
||||
self.lcd = JHD1802()
|
||||
self.wakeup_actions = []
|
||||
self.add_wakeup_action(0, lambda: self.trigger_alarm()) # Activate alarm state
|
||||
@@ -121,10 +119,6 @@ class AlarmClock:
|
||||
while True:
|
||||
self.loop()
|
||||
|
||||
def set_relay_state(self, state: bool):
|
||||
"""Set the relay GPIO state."""
|
||||
self.relay_gpio.write(1 if state else 0)
|
||||
|
||||
def loop(self):
|
||||
"""Main loop iteration - handles display and button input."""
|
||||
current_button_state = not self.button_gpio.read()
|
||||
@@ -185,9 +179,7 @@ class AlarmClock:
|
||||
# Dismiss active alarm
|
||||
self.dismiss_alarm()
|
||||
else:
|
||||
# Normal mode: toggle relay
|
||||
self.set_relay_state(not self.relay_gpio.read())
|
||||
set_shelly_plug_state(SHELLY_DEVICE_ID, self.relay_gpio.read() == 0)
|
||||
print("pressed in normal mode - no active alarm to dismiss")
|
||||
elif self.config_mode == "set_hour":
|
||||
# Increment hour
|
||||
self.alarm_time = self.alarm_time.replace(hour=(self.alarm_time.hour + 1) % 24)
|
||||
|
||||
22
src/main.py
22
src/main.py
@@ -2,16 +2,27 @@ from .config import SHELLY_DEVICE_ID
|
||||
from .shelly import ShellyDevice
|
||||
from .audio import AudioPlayer
|
||||
from .alarm_clock import AlarmClock
|
||||
from .relay import Relay
|
||||
from os import path
|
||||
|
||||
shellyDevice = ShellyDevice(SHELLY_DEVICE_ID)
|
||||
|
||||
audioFileAbsolutePath = path.abspath('./boxing_bell_multiple.wav')
|
||||
audio_player = AudioPlayer(audioFileAbsolutePath, loop=True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point for the alarm clock application."""
|
||||
|
||||
shellyDevice = ShellyDevice(SHELLY_DEVICE_ID)
|
||||
|
||||
relay = Relay(16)
|
||||
relay.off()
|
||||
|
||||
audioFileAbsolutePath = path.abspath('./boxing_bell_multiple.wav')
|
||||
audio_player = AudioPlayer(audioFileAbsolutePath, loop=True)
|
||||
|
||||
|
||||
alarm_clock = AlarmClock()
|
||||
|
||||
alarm_clock.add_wakeup_action(
|
||||
@@ -36,6 +47,11 @@ def main():
|
||||
dismiss_action=lambda: audio_player.stop()
|
||||
)
|
||||
|
||||
alarm_clock.add_wakeup_action(
|
||||
0,
|
||||
action=lambda: relay.on(),
|
||||
)
|
||||
|
||||
alarm_clock.start()
|
||||
|
||||
|
||||
|
||||
35
src/relay.py
Normal file
35
src/relay.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from grove.gpio import GPIO
|
||||
|
||||
|
||||
class Relay:
|
||||
"""Controller for a GPIO relay."""
|
||||
|
||||
_gpio: GPIO
|
||||
_state: bool = False
|
||||
|
||||
def __init__(self, pin: int = 16):
|
||||
self._gpio = GPIO(pin, GPIO.OUT)
|
||||
self._state = False
|
||||
|
||||
@property
|
||||
def state(self) -> bool:
|
||||
"""Get the current relay state."""
|
||||
return self._state
|
||||
|
||||
def set_state(self, state: bool):
|
||||
"""Set the relay state."""
|
||||
self._state = state
|
||||
self._gpio.write(1 if state else 0)
|
||||
|
||||
def toggle(self) -> bool:
|
||||
"""Toggle the relay state and return the new state."""
|
||||
self.set_state(not self._state)
|
||||
return self._state
|
||||
|
||||
def on(self):
|
||||
"""Turn the relay on."""
|
||||
self.set_state(True)
|
||||
|
||||
def off(self):
|
||||
"""Turn the relay off."""
|
||||
self.set_state(False)
|
||||
Reference in New Issue
Block a user