moved variables to global config

Paco Hope 2023-11-30 22:05:29 -05:00
parent 59596c4e9f
commit 19ed82d69a
1 changed files with 12 additions and 24 deletions

View File

@ -26,26 +26,14 @@ import pngdec
import gc import gc
import sdcard import sdcard
import os import os
import WIFI_CONFIG import CONFIG
import inky_frame import inky_frame
import uasyncio import uasyncio
global UPDATE_INTERVAL, IMG_URL, FILENAME, graphics, tz_offset global graphics
# Length of time between updates in minutes. def display_image(j, CONFIG.FILENAME):
# Frequent updates will reduce battery life!
UPDATE_INTERVAL = 60
# URL that has the image
IMG_URL = "http://hub.home.paco.to/"
# IMG_URL = "https://house.paco.to/local/frame.png"
# 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 = -5
def display_image(j, filename):
# Open the PNG file # Open the PNG file
j.open_file(filename) j.open_file(CONFIG.FILENAME)
# Decode the PNG # Decode the PNG
j.decode() j.decode()
@ -74,9 +62,9 @@ def fetch(url):
""" """
inky_frame.button_b.led_on() inky_frame.button_b.led_on()
network_manager = NetworkManager(WIFI_CONFIG.COUNTRY, status_handler=status_handler, client_timeout=60) network_manager = NetworkManager(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(CONFIG.SSID, CONFIG.PSK))
except RuntimeError: except RuntimeError:
pass pass
except Exception as e: except Exception as e:
@ -95,7 +83,7 @@ def fetch(url):
inky_frame.set_time() # set time from network inky_frame.set_time() # set time from network
inky_frame.button_b.led_off() inky_frame.button_b.led_off()
tz_seconds = (tz_offset * 3600) tz_seconds = (CONFIG.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
@ -117,12 +105,12 @@ def fetch(url):
print("other exception") print("other exception")
print(e) print(e)
return False return False
os.remove(FILENAME) os.remove(CONFIG.FILENAME)
inky_frame.button_c.led_off() inky_frame.button_c.led_off()
inky_frame.button_d.led_on() inky_frame.button_d.led_on()
try: try:
data = bytearray(1024) data = bytearray(1024)
with open(FILENAME, "wb+") as f: with open(CONFIG.FILENAME, "wb+") as f:
while True: while True:
if socket.readinto(data) == 0: if socket.readinto(data) == 0:
break break
@ -173,7 +161,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(IMG_URL): if fetch(CONFIG.IMG_URL):
gc.collect() gc.collect()
inky_frame.button_e.led_on() inky_frame.button_e.led_on()
@ -183,7 +171,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, FILENAME) display_image(j, CONFIG.FILENAME)
# Display the result # Display the result
graphics.update() graphics.update()
@ -195,5 +183,5 @@ while True:
inky_frame.button_c.led_off() inky_frame.button_c.led_off()
inky_frame.button_d.led_off() inky_frame.button_d.led_off()
inky_frame.button_e.led_off() inky_frame.button_e.led_off()
inky_frame.sleep_for(UPDATE_INTERVAL) inky_frame.sleep_for(CONFIG.UPDATE_INTERVAL)