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 network_manager import NetworkManager
import time
import ntptime
import inky_frame
import pngdec
import gc
@ -31,14 +32,15 @@ import inky_frame
import uasyncio
global graphics
def display_image(j, CONFIG.FILENAME):
def display_image(j):
# Open the PNG file
j.open_file(CONFIG.FILENAME)
# Decode the PNG
j.decode()
# 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_font('sans')
# 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)
gc.collect()
def fetch(url):
def fetch():
""" Fetch the image, store it onboard.
Returns False at first error.
Returns True only if everything succeeds.
@ -74,29 +76,40 @@ def fetch(url):
return False
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}")
count = 0
if year < 2023:
# blink b when we are setting the time
inky_frame.button_b.led_on()
inky_frame.set_time() # set time from network
inky_frame.button_b.led_off()
# blink d when we are setting the time
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
except OSError as e:
print( "x", end="" )
count += 1
inky_frame.button_d.led_off()
tz_seconds = (CONFIG.TZ_OFFSET * 3600)
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time() + tz_seconds)
if count >= 5:
# 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
if hour < 5 or hour > 22:
print("no need to fetch")
print(f"hour {hour:02}, no need to fetch")
return False
del year, month, day, hour, minute, second, tz_seconds
print( "fetching " + url )
del year, month, day, hour, minute, second, tz_seconds, count
print( "fetching " + CONFIG.IMG_URL )
inky_frame.button_c.led_on()
try:
# Grab the image
socket = urequest.urlopen(url)
socket = urequest.urlopen(CONFIG.IMG_URL)
gc.collect()
except OSError as e:
print("Unable open URL. OSErr: ")
@ -161,7 +174,7 @@ while True:
mount_sd()
gc.collect()
inky_frame.button_a.led_off()
if fetch(CONFIG.IMG_URL):
if fetch():
gc.collect()
inky_frame.button_e.led_on()
@ -171,7 +184,7 @@ while True:
# Create a new PNG decoder for our PicoGraphics
j = pngdec.PNG(graphics)
display_image(j, CONFIG.FILENAME)
display_image(j)
# Display the result
graphics.update()