Skip to content

Commit 2991849

Browse files
committed
try caching to SD first to avoid QSPI writes if possible
1 parent 01d77b1 commit 2991849

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

adafruit_pyportal.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
import gc
4949
import board
5050
import busio
51+
import storage
5152
import microcontroller
5253
from digitalio import DigitalInOut
5354
import pulseio
5455
import adafruit_touchscreen
5556
import neopixel
57+
import adafruit_sdcard
5658

5759
from adafruit_esp32spi import adafruit_esp32spi
5860
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
@@ -207,6 +209,17 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
207209

208210
requests.set_interface(self._esp)
209211

212+
if self._debug:
213+
print("Init SD Card")
214+
sd_cs = DigitalInOut(board.SD_CS)
215+
self._sdcard = None
216+
try:
217+
self._sdcard = adafruit_sdcard.SDCard(spi, sd_cs)
218+
vfs = storage.VfsFat(self._sdcard)
219+
storage.mount(vfs, "/sd")
220+
except OSError as e:
221+
print("No SD card found:", e)
222+
210223
if self._debug:
211224
print("Init display")
212225
self.splash = displayio.Group(max_size=5)
@@ -488,11 +501,12 @@ def get_local_time(self, location=None):
488501
response = None
489502
gc.collect()
490503

491-
def wget(self, url, filename):
504+
def wget(self, url, filename, *, chunk_size=12000):
492505
"""Download a url and save to filename location, like the command wget.
493506
494507
:param url: The URL from which to obtain the data.
495508
:param filename: The name of the file to save the data to.
509+
:param chunk_size: how much data to read/write at a time.
496510
497511
"""
498512
print("Fetching stream from", url)
@@ -506,18 +520,19 @@ def wget(self, url, filename):
506520
remaining = content_length
507521
print("Saving data to ", filename)
508522
stamp = time.monotonic()
509-
with open(filename, "wb") as file:
510-
for i in r.iter_content(min(remaining, 12000)): # huge chunks!
511-
self.neo_status((0, 100, 100))
512-
remaining -= len(i)
513-
file.write(i)
514-
if self._debug:
515-
print("Read %d bytes, %d remaining" % (content_length-remaining, remaining))
516-
else:
517-
print(".", end='')
518-
if not remaining:
519-
break
520-
self.neo_status((100, 100, 0))
523+
file = open(filename, "wb")
524+
for i in r.iter_content(min(remaining, chunk_size)): # huge chunks!
525+
self.neo_status((0, 100, 100))
526+
remaining -= len(i)
527+
file.write(i)
528+
if self._debug:
529+
print("Read %d bytes, %d remaining" % (content_length-remaining, remaining))
530+
else:
531+
print(".", end='')
532+
if not remaining:
533+
break
534+
self.neo_status((100, 100, 0))
535+
file.close()
521536

522537
r.close()
523538
stamp = time.monotonic() - stamp
@@ -606,8 +621,17 @@ def fetch(self):
606621
print("convert URL:", image_url)
607622
# convert image to bitmap and cache
608623
#print("**not actually wgetting**")
609-
self.wget(image_url, "/cache.bmp")
610-
self.set_background("/cache.bmp")
624+
filename = "/cache.bmp"
625+
chunk_size = 12000 # default chunk size is 12K (for QSPI)
626+
if self._sdcard:
627+
filename = "/sd" + filename
628+
chunk_size = 512 # current bug in big SD writes -> stick to 1 block
629+
try:
630+
self.wget(image_url, filename, chunk_size=chunk_size)
631+
except OSError as e:
632+
print(e)
633+
raise OSError("""\n\nNo writable filesystem found for saving datastream. Insert an SD card or set internal filesystem to be unsafe by setting 'disable_concurrent_write_protection' in the mount options in boot.py""") # pylint: disable=line-too-long
634+
self.set_background(filename)
611635
except ValueError as error:
612636
print("Error displaying cached image. " + error.args[0])
613637
self.set_background(self._default_bg)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ adafruit-circuitpython-touchscreen
44
adafruit-circuitpython-esp32spi
55
adafruit-circuitpython-bitmap-font
66
adafruit-circuitpython-neopixel
7+
adafruit-circuitpython-sdcard

0 commit comments

Comments
 (0)