Resolved time issues

main
Paco Hope 2023-12-01 08:52:43 -05:00
parent 156776f33f
commit 4ff4f3f122
1 changed files with 29 additions and 16 deletions

View File

@ -21,6 +21,7 @@ from urllib import urequest
from machine import Pin, SPI from machine import Pin, SPI
from network_manager import NetworkManager from network_manager import NetworkManager
import time import time
import ntptime
import inky_frame import inky_frame
import pngdec import pngdec
import gc import gc
@ -31,14 +32,15 @@ import inky_frame
import uasyncio import uasyncio
global graphics global graphics
def display_image(j, CONFIG.FILENAME): def display_image(j):
# Open the PNG file # Open the PNG file
j.open_file(CONFIG.FILENAME) j.open_file(CONFIG.FILENAME)
# Decode the PNG # Decode the PNG
j.decode() j.decode()
# write the date/time at the top # write the date/time at the top
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time()) tz_seconds = (CONFIG.TZ_OFFSET * 3600)
year, month, day, hour, minute, second, dow, _ = time.gmtime(time.time() + tz_seconds)
graphics.set_pen(0) graphics.set_pen(0)
graphics.set_font('sans') graphics.set_font('sans')
# display.text(text, x, y, wordwrap, scale, angle, spacing) # display.text(text, x, y, wordwrap, scale, angle, spacing)
@ -55,7 +57,7 @@ def show_error(text,y):
graphics.text(text, 5, 16 + HEIGHT, 400, 2) graphics.text(text, 5, 16 + HEIGHT, 400, 2)
gc.collect() gc.collect()
def fetch(url): def fetch():
""" Fetch the image, store it onboard. """ Fetch the image, store it onboard.
Returns False at first error. Returns False at first error.
Returns True only if everything succeeds. Returns True only if everything succeeds.
@ -74,29 +76,40 @@ def fetch(url):
return False return False
inky_frame.button_b.led_off() inky_frame.button_b.led_off()
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time()) tz_seconds = (CONFIG.TZ_OFFSET * 3600)
year, month, day, hour, minute, second, dow, _ = time.gmtime(time.time() + tz_seconds)
print(f"{hour:02}:{minute:02} {year}-{month}-{day}") print(f"{hour:02}:{minute:02} {year}-{month}-{day}")
count = 0
if year < 2023: if year < 2023:
# blink b when we are setting the time # blink d when we are setting the time
inky_frame.button_b.led_on() inky_frame.button_d.led_on()
ntptime.host = '0.us.pool.ntp.org'
while count < 5:
try:
inky_frame.set_time() # set time from network inky_frame.set_time() # set time from network
inky_frame.button_b.led_off() except OSError as e:
print( "x", end="" )
count += 1
inky_frame.button_d.led_off()
tz_seconds = (CONFIG.TZ_OFFSET * 3600) if count >= 5:
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time() + tz_seconds) # turn on E to indicate error. gets turned off later.
inky_frame.button_e.led_on()
year, month, day, hour, minute, second, dow, _ = time.gmtime(time.time() + tz_seconds)
# don't bother updating before 06:00 or after 23:00 # don't bother updating before 06:00 or after 23:00
if hour < 5 or hour > 22: if hour < 5 or hour > 22:
print("no need to fetch") print(f"hour {hour:02}, no need to fetch")
return False return False
del year, month, day, hour, minute, second, tz_seconds del year, month, day, hour, minute, second, tz_seconds, count
print( "fetching " + url ) print( "fetching " + CONFIG.IMG_URL )
inky_frame.button_c.led_on() inky_frame.button_c.led_on()
try: try:
# Grab the image # Grab the image
socket = urequest.urlopen(url) socket = urequest.urlopen(CONFIG.IMG_URL)
gc.collect() gc.collect()
except OSError as e: except OSError as e:
print("Unable open URL. OSErr: ") print("Unable open URL. OSErr: ")
@ -161,7 +174,7 @@ while True:
mount_sd() mount_sd()
gc.collect() gc.collect()
inky_frame.button_a.led_off() inky_frame.button_a.led_off()
if fetch(CONFIG.IMG_URL): if fetch():
gc.collect() gc.collect()
inky_frame.button_e.led_on() inky_frame.button_e.led_on()
@ -171,7 +184,7 @@ while True:
# Create a new PNG decoder for our PicoGraphics # Create a new PNG decoder for our PicoGraphics
j = pngdec.PNG(graphics) j = pngdec.PNG(graphics)
display_image(j, CONFIG.FILENAME) display_image(j)
# Display the result # Display the result
graphics.update() graphics.update()