Added progress and more garbage collection

* Added LEDs for progress.
* Added more gc.collect() in various places
* Modified the TZ code, but I'm not sure it's right.
Paco Hope 2023-11-28 20:30:42 -05:00
parent d6f7250693
commit d22a792984
1 changed files with 51 additions and 23 deletions

View File

@ -29,7 +29,7 @@ import os
import WIFI_CONFIG
import inky_frame
import uasyncio
global UPDATE_INTERVAL, IMG_URL, FILENAME, graphics, tz_seconds
global UPDATE_INTERVAL, IMG_URL, FILENAME, graphics, tz_offset
# Length of time between updates in minutes.
# Frequent updates will reduce battery life!
@ -41,9 +41,7 @@ IMG_URL = "http://hub.home.paco.to/"
# Filename on the SD card for the image. Probably doesn't need changing.
FILENAME = "/sd/frame.png"
# tz_offset is hours from GMT. E.g., US PST is -7 (UTC-07:00) and CEST (Paris) is 1 (UTC+01:00)
tz_offset = 0
tz_seconds = (tz_offset * 3600)
tz_offset = -5
def display_image(j, filename):
# Open the PNG file
@ -52,11 +50,12 @@ def display_image(j, filename):
j.decode()
# write the date/time at the top
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time() + tz_seconds)
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time())
graphics.set_pen(0)
graphics.set_font('sans')
# display.text(text, x, y, wordwrap, scale, angle, spacing)
graphics.text(f"{hour:02}:{minute:02} {year}-{month}-{day}", 230, 10, scale=0.5)
gc.collect()
def show_error(text,y):
WIDTH = 600
@ -74,6 +73,7 @@ def fetch(url):
Returns True only if everything succeeds.
"""
inky_frame.button_b.led_on()
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler, client_timeout=60)
try:
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
@ -82,22 +82,30 @@ def fetch(url):
except Exception as e:
print("join network failed")
print(e)
inky_frame.button_b.led_off()
return False
inky_frame.button_b.led_off()
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time() + tz_seconds)
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time())
print(f"{hour:02}:{minute:02} {year}-{month}-{day}")
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()
tz_seconds = (tz_offset * 3600)
year, month, day, hour, minute, second, dow, _ = time.localtime(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")
return False
del year, month, day, hour, minute, second, tz_seconds
print( "fetching " + url )
inky_frame.button_c.led_on()
print( "fetching " + url )
try:
# Grab the image
socket = urequest.urlopen(url)
@ -110,6 +118,8 @@ def fetch(url):
print(e)
return False
os.remove(FILENAME)
inky_frame.button_c.led_off()
inky_frame.button_d.led_on()
try:
data = bytearray(1024)
with open(FILENAME, "wb+") as f:
@ -126,34 +136,47 @@ def fetch(url):
print(e)
return False
del socket
inky_frame.button_d.led_off()
gc.collect()
return True
def status_handler(mode, status, ip):
print(mode, status, ip)
inky_frame.led_busy.on()
# set up the SD card
try:
sd_spi = SPI(0, sck=Pin(18, Pin.OUT), mosi=Pin(19, Pin.OUT), miso=Pin(16, Pin.OUT))
sd = sdcard.SDCard(sd_spi, Pin(22))
os.mount(sd, "/sd")
except Exception as e:
print("mounting SD card")
print(e)
def mount_sd():
# set up the SD card
try:
sd_spi = SPI(0, sck=Pin(18, Pin.OUT), mosi=Pin(19, Pin.OUT), miso=Pin(16, Pin.OUT))
sd = sdcard.SDCard(sd_spi, Pin(22))
os.mount(sd, "/sd")
except Exception as e:
print("mounting SD card")
print(e)
files = os.listdir("/sd")
if len(files) == 0:
show_error("SD didn't mount?", 5)
del files
files = os.listdir("/sd")
if len(files) == 0:
show_error("SD didn't mount?", 5)
del files
while True:
graphics = None
gc.collect()
inky_frame.led_busy.off()
inky_frame.button_a.led_off()
inky_frame.button_b.led_off()
inky_frame.button_c.led_off()
inky_frame.button_d.led_off()
inky_frame.button_e.led_off()
inky_frame.led_busy.on()
inky_frame.button_a.led_on()
mount_sd()
gc.collect()
inky_frame.button_a.led_off()
if fetch(IMG_URL):
gc.collect()
inky_frame.button_e.led_on()
# set up the display
graphics = PicoGraphics(DISPLAY)
@ -164,8 +187,13 @@ while True:
# Display the result
graphics.update()
inky_frame.button_e.led_off()
inky_frame.led_busy.off()
# Go to sleep if on battery power
inky_frame.button_a.led_off()
inky_frame.button_b.led_off()
inky_frame.button_c.led_off()
inky_frame.button_d.led_off()
inky_frame.button_e.led_off()
inky_frame.sleep_for(UPDATE_INTERVAL)