75 lines
No EOL
2 KiB
Python
75 lines
No EOL
2 KiB
Python
import machine
|
|
import neopixel
|
|
import config
|
|
import ntptime
|
|
import time
|
|
|
|
def set_time(hour, minute):
|
|
np_hour = neopixel.NeoPixel(machine.Pin(23), 24, bpp=4)
|
|
np_hour[(hour % 12) * 2 + int(minute / 30)] = config.HOUR_COLOR
|
|
np_hour.write()
|
|
|
|
np_minute = neopixel.NeoPixel(machine.Pin(32), 30, bpp=4)
|
|
np_minute[int(minute / 2)] = config.MINUTE_COLOR
|
|
np_minute.write()
|
|
|
|
def sync_ntptime():
|
|
try:
|
|
print("Syncing time...")
|
|
ntptime.settime()
|
|
except:
|
|
print("Could not sync time")
|
|
|
|
def check_daylight_saving(current_time):
|
|
for day in range(25, 32):
|
|
t = time.mktime((current_time[0], 3, day, 2, 0, 0, 0, 0))
|
|
if time.localtime(t)[6] == 6:
|
|
summer_time = t
|
|
break
|
|
|
|
for day in range(25, 32):
|
|
t = time.mktime((current_time[0], 10, day, 2, 0, 0, 0, 0))
|
|
if time.localtime(t)[6] == 6:
|
|
winter_time = t
|
|
break
|
|
|
|
comp_time = time.mktime(current_time)
|
|
if summer_time <= comp_time <= winter_time:
|
|
return True
|
|
return False
|
|
|
|
def check_wifi_connection():
|
|
import network
|
|
sta_if = network.WLAN(network.STA_IF)
|
|
return sta_if.isconnected
|
|
|
|
# Test LEDs before startup
|
|
for hour in range(0, 11):
|
|
set_time(hour, 0)
|
|
time.sleep(0.1)
|
|
for minute in range(0, 59):
|
|
set_time(0, minute)
|
|
time.sleep(0.05)
|
|
set_time(0, 0)
|
|
|
|
# Main Loop
|
|
while True:
|
|
|
|
if not check_wifi_connection():
|
|
machine.reset()
|
|
|
|
current_time = time.localtime()
|
|
|
|
# Sync time if we are on full hour
|
|
if current_time[4] == 0 and current_time[5] == 0 or current_time[0] == 2000:
|
|
sync_ntptime()
|
|
current_time = time.localtime()
|
|
|
|
if check_daylight_saving(current_time):
|
|
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET_DAYLIGHT)
|
|
else:
|
|
current_time = time.localtime(time.mktime(current_time) + config.TIMEZONE_OFFSET)
|
|
|
|
print("Set Time:", current_time)
|
|
set_time(current_time[3], current_time[4])
|
|
time.sleep(1) |