Remove files found not needed(builtin options). Implement sleeping and catch some errors.

This commit is contained in:
2023-11-04 17:49:11 -04:00
parent e28f647a60
commit 70ce4477a8
6 changed files with 242 additions and 332 deletions

88
main.py
View File

@@ -1,51 +1,63 @@
import uNextcloud
import inky_helper as ih
import gc
import sdcard, uos
import inky_helper as ih
import inky_frame
import gc
import random
from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY
from jpegdec import JPEG
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT), miso=machine.Pin(16, machine.Pin.OUT))
sd = sdcard.SDCard(sd_spi, machine.Pin(22))
uos.mount(sd, "/sd")
gc.collect()
from secrets import WIFI_SSID, WIFI_PASSWORD, NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD
try:
ih.network_connect_better(WIFI_SSID, WIFI_PASSWORD)
except RuntimeError as e:
print(e)
with open('/sd/errors.log', 'a+') as error_log:
error_log.write(f'{e} \n')
#Likely a network oops, try starting again
machine.soft_reset()
def get_new_image():
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT),
miso=machine.Pin(16, machine.Pin.OUT))
sd = sdcard.SDCard(sd_spi, machine.Pin(22))
uos.mount(sd, "/sd")
gc.collect()
ih.network_connect(WIFI_SSID, WIFI_PASSWORD)
try:
nc = uNextcloud.uNextcloud("https://cloud.eyecreate.org")
nc.set_auth(NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD)
response = nc.get_folder_items("eink-frame")
except OSError as e:
print(e)
with open('/sd/errors.log', 'a+') as error_log:
error_log.write(f'{e} \n')
#Likely a network oops, try starting again
machine.soft_reset()
try:
last = open('/sd/last_wallpaper', 'r')
last_file = last.read()
last.close()
except OSError:
last_file = ""
try:
last = open('/sd/last_wallpaper', 'r')
last_file = last.read()
last.close()
except OSError:
last_file = ""
if response != None:
for item in response:
if item.url_path == last_file:
response.remove(item)
ran = random.choice(response)
if ran.mimetype == "image/jpeg":
update_last = open('/sd/last_wallpaper', 'w+')
if response != None:
for item in response:
if item.url_path == last_file:
response.remove(item)
ran = random.choice(response)
if ran.mimetype == "image/jpeg":
with open('/sd/last_wallpaper', 'w+') as update_last:
update_last.write(ran.url_path)
update_last.close()
print(ran.url_path)
gc.collect()
nc.download_file_to_path(ran.url_path, "/sd/current_image.jpg")
graphics = PicoGraphics(DISPLAY)
j = JPEG(graphics)
j.open_file("/sd/current_image.jpg")
j.decode()
graphics.update()
#sleep?
print(ran.url_path)
gc.collect()
nc.download_file_to_path(ran.url_path, "/sd/current_image.jpg")
gc.collect()
from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY
from jpegdec import JPEG
graphics = PicoGraphics(DISPLAY)
j = JPEG(graphics)
j.open_file("/sd/current_image.jpg")
j.decode()
graphics.update()
inky_frame.sleep_for(180)