70 lines
1.7 KiB
Python
70 lines
1.7 KiB
Python
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
|
|
lastKnownButtonState: bool = False
|
|
lcd: JHD1802
|
|
def __init__(self, pins: dict = {}):
|
|
self.ledGpio = GPIO(pins.get("led", 5), GPIO.OUT)
|
|
self.buttonGpio = GPIO(pins.get("button", 6), GPIO.IN)
|
|
self.lcd = JHD1802()
|
|
self.setLcdText("AlarmClock Init")
|
|
|
|
|
|
def start(self):
|
|
print("AlarmClock started")
|
|
while True:
|
|
self.loop()
|
|
|
|
def loop(self):
|
|
currentButtonState = not self.buttonGpio.read()
|
|
if currentButtonState != self.lastKnownButtonState:
|
|
self.lastKnownButtonState = currentButtonState
|
|
if currentButtonState:
|
|
self.onButtonPress("button")
|
|
|
|
|
|
def onButtonPress(self, button: str):
|
|
print(f"Button {button} pressed")
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|