5959from adafruit_display_text .text_area import TextArea
6060from adafruit_bitmap_font import bitmap_font
6161
62+ import storage
63+ import adafruit_sdcard
6264import displayio
6365import audioio
6466import rtc
6567import supervisor
6668
6769try :
68- from settings import settings
70+ from secrets import secrets
6971except ImportError :
70- print ("""WiFi settings are kept in settings .py, please add them there!
71- the settings dictionary must contain 'ssid' and 'password' at a minimum""" )
72+ print ("""WiFi settings are kept in secrets .py, please add them there!
73+ the secrets dictionary must contain 'ssid' and 'password' at a minimum""" )
7274 raise
7375
7476__version__ = "0.0.0-auto.0"
@@ -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 error :
221+ print ("No SD card found:" , error )
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
@@ -529,9 +544,9 @@ def _connect_esp(self):
529544 while not self ._esp .is_connected :
530545 if self ._debug :
531546 print ("Connecting to AP" )
532- # settings dictionary must contain 'ssid' and 'password' at a minimum
547+ # secrets dictionary must contain 'ssid' and 'password' at a minimum
533548 self .neo_status ((100 , 0 , 0 )) # red = not connected
534- self ._esp .connect (settings )
549+ self ._esp .connect (secrets )
535550
536551 def fetch (self ):
537552 """Fetch data from the url we initialized with, perfom any parsing,
@@ -575,15 +590,15 @@ def fetch(self):
575590 supervisor .reload ()
576591
577592 if self ._regexp_path :
578- import ure
593+ import re
579594
580595 # extract desired text/values from json
581596 if self ._json_path :
582597 for path in self ._json_path :
583598 values .append (PyPortal ._json_traverse (json_out , path ))
584599 elif self ._regexp_path :
585600 for regexp in self ._regexp_path :
586- values .append (ure .search (regexp , r .text ).group (1 ))
601+ values .append (re .search (regexp , r .text ).group (1 ))
587602 else :
588603 values = r .text
589604
@@ -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 error :
632+ print (error )
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 )
611635 except ValueError as error :
612636 print ("Error displaying cached image. " + error .args [0 ])
613637 self .set_background (self ._default_bg )
0 commit comments