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.
parent
d6f7250693
commit
d22a792984
|
@ -29,7 +29,7 @@ import os
|
||||||
import WIFI_CONFIG
|
import WIFI_CONFIG
|
||||||
import inky_frame
|
import inky_frame
|
||||||
import uasyncio
|
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.
|
# Length of time between updates in minutes.
|
||||||
# Frequent updates will reduce battery life!
|
# 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 on the SD card for the image. Probably doesn't need changing.
|
||||||
FILENAME = "/sd/frame.png"
|
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 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_offset = -5
|
||||||
tz_seconds = (tz_offset * 3600)
|
|
||||||
|
|
||||||
|
|
||||||
def display_image(j, filename):
|
def display_image(j, filename):
|
||||||
# Open the PNG file
|
# Open the PNG file
|
||||||
|
@ -52,11 +50,12 @@ def display_image(j, filename):
|
||||||
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)
|
year, month, day, hour, minute, second, dow, _ = time.localtime(time.time())
|
||||||
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)
|
||||||
graphics.text(f"{hour:02}:{minute:02} {year}-{month}-{day}", 230, 10, scale=0.5)
|
graphics.text(f"{hour:02}:{minute:02} {year}-{month}-{day}", 230, 10, scale=0.5)
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
def show_error(text,y):
|
def show_error(text,y):
|
||||||
WIDTH = 600
|
WIDTH = 600
|
||||||
|
@ -74,6 +73,7 @@ def fetch(url):
|
||||||
Returns True only if everything succeeds.
|
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)
|
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler, client_timeout=60)
|
||||||
try:
|
try:
|
||||||
uasyncio.get_event_loop().run_until_complete(network_manager.client(WIFI_CONFIG.SSID, WIFI_CONFIG.PSK))
|
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:
|
except Exception as e:
|
||||||
print("join network failed")
|
print("join network failed")
|
||||||
print(e)
|
print(e)
|
||||||
|
inky_frame.button_b.led_off()
|
||||||
return False
|
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}")
|
print(f"{hour:02}:{minute:02} {year}-{month}-{day}")
|
||||||
|
|
||||||
if year < 2023:
|
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.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)
|
year, month, day, hour, minute, second, dow, _ = time.localtime(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("no need to fetch")
|
||||||
return False
|
return False
|
||||||
|
del year, month, day, hour, minute, second, tz_seconds
|
||||||
print( "fetching " + url )
|
print( "fetching " + url )
|
||||||
|
inky_frame.button_c.led_on()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Grab the image
|
# Grab the image
|
||||||
socket = urequest.urlopen(url)
|
socket = urequest.urlopen(url)
|
||||||
|
@ -110,6 +118,8 @@ def fetch(url):
|
||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
os.remove(FILENAME)
|
os.remove(FILENAME)
|
||||||
|
inky_frame.button_c.led_off()
|
||||||
|
inky_frame.button_d.led_on()
|
||||||
try:
|
try:
|
||||||
data = bytearray(1024)
|
data = bytearray(1024)
|
||||||
with open(FILENAME, "wb+") as f:
|
with open(FILENAME, "wb+") as f:
|
||||||
|
@ -126,14 +136,14 @@ def fetch(url):
|
||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
del socket
|
del socket
|
||||||
|
inky_frame.button_d.led_off()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def status_handler(mode, status, ip):
|
def status_handler(mode, status, ip):
|
||||||
print(mode, status, ip)
|
print(mode, status, ip)
|
||||||
|
|
||||||
inky_frame.led_busy.on()
|
def mount_sd():
|
||||||
|
|
||||||
# set up the SD card
|
# set up the SD card
|
||||||
try:
|
try:
|
||||||
sd_spi = SPI(0, sck=Pin(18, Pin.OUT), mosi=Pin(19, Pin.OUT), miso=Pin(16, Pin.OUT))
|
sd_spi = SPI(0, sck=Pin(18, Pin.OUT), mosi=Pin(19, Pin.OUT), miso=Pin(16, Pin.OUT))
|
||||||
|
@ -146,14 +156,27 @@ except Exception as e:
|
||||||
files = os.listdir("/sd")
|
files = os.listdir("/sd")
|
||||||
if len(files) == 0:
|
if len(files) == 0:
|
||||||
show_error("SD didn't mount?", 5)
|
show_error("SD didn't mount?", 5)
|
||||||
|
|
||||||
del files
|
del files
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
graphics = None
|
graphics = None
|
||||||
gc.collect()
|
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):
|
if fetch(IMG_URL):
|
||||||
|
gc.collect()
|
||||||
|
inky_frame.button_e.led_on()
|
||||||
|
|
||||||
# set up the display
|
# set up the display
|
||||||
graphics = PicoGraphics(DISPLAY)
|
graphics = PicoGraphics(DISPLAY)
|
||||||
|
|
||||||
|
@ -164,8 +187,13 @@ while True:
|
||||||
# Display the result
|
# Display the result
|
||||||
graphics.update()
|
graphics.update()
|
||||||
|
|
||||||
|
inky_frame.button_e.led_off()
|
||||||
inky_frame.led_busy.off()
|
inky_frame.led_busy.off()
|
||||||
# Go to sleep if on battery power
|
# 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)
|
inky_frame.sleep_for(UPDATE_INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue