48
48
import gc
49
49
import board
50
50
import busio
51
+ import storage
51
52
import microcontroller
52
53
from digitalio import DigitalInOut
53
54
import pulseio
54
55
import adafruit_touchscreen
55
56
import neopixel
57
+ import adafruit_sdcard
56
58
57
59
from adafruit_esp32spi import adafruit_esp32spi
58
60
import adafruit_esp32spi .adafruit_esp32spi_requests as requests
@@ -207,6 +209,17 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
207
209
208
210
requests .set_interface (self ._esp )
209
211
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
+
210
223
if self ._debug :
211
224
print ("Init display" )
212
225
self .splash = displayio .Group (max_size = 5 )
@@ -488,11 +501,12 @@ def get_local_time(self, location=None):
488
501
response = None
489
502
gc .collect ()
490
503
491
- def wget (self , url , filename ):
504
+ def wget (self , url , filename , * , chunk_size = 12000 ):
492
505
"""Download a url and save to filename location, like the command wget.
493
506
494
507
:param url: The URL from which to obtain the data.
495
508
: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.
496
510
497
511
"""
498
512
print ("Fetching stream from" , url )
@@ -506,18 +520,19 @@ def wget(self, url, filename):
506
520
remaining = content_length
507
521
print ("Saving data to " , filename )
508
522
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 ()
521
536
522
537
r .close ()
523
538
stamp = time .monotonic () - stamp
@@ -606,8 +621,17 @@ def fetch(self):
606
621
print ("convert URL:" , image_url )
607
622
# convert image to bitmap and cache
608
623
#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 \n No 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 )
611
635
except ValueError as error :
612
636
print ("Error displaying cached image. " + error .args [0 ])
613
637
self .set_background (self ._default_bg )
0 commit comments