from time import time from astral import LocationInfo from astral.sun import sun from datetime import date from grove.gpio import GPIO from grove.display.jhd1802 import JHD1802 city = LocationInfo("Seinäjoki", "Finland", "Europe/Helsinki", 62.7900, 22.8400) s = sun(city.observer, date=date.today()) print(f"City: {city.name}, {city.region}") print(f"Timezone: {city.timezone}") print(f"Latitude: {city.latitude:.6f}; Longitude: {city.longitude:.6f}") def isSunUp() -> bool: from datetime import datetime now = datetime.now(city.tzinfo) return s["sunrise"] <= now <= s["sunset"] # sunset and sunrise class AlarmClock: ledGpio: GPIO buttonGpio: GPIO relayGpio: GPIO lastKnownButtonState: bool = False lcd: JHD1802 lastButtonPressTime: float = 0.0 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.setLcdText("AlarmClock Init") def start(self): print("AlarmClock started") while True: self.loop() def setRelayState(self, state: bool): self.relayGpio.write(1 if state else 0) def loop(self): currentButtonState = not self.buttonGpio.read() if currentButtonState != self.lastKnownButtonState: self.lastKnownButtonState = currentButtonState self.onButtonPress("button", currentButtonState) def onButtonPress(self, button: str, state: bool): print(f"Button '{button}' pressed state: {state}") if button == "button" and state == True: self.lastButtonPressTime = time() self.setRelayState(not self.relayGpio.read()) self.setLcdText(self.lastButtonPressTime.__str__()) def setLcdText(self, text: str): self.lcd.clear() self.lcd.write(text) print(f"LCD: {text}") pins = { "button": 6, "led": 5, "buzzer": 12, } alarm_clock = AlarmClock(pins) alarm_clock.start()