59
59
from adafruit_display_text .text_area import TextArea
60
60
from adafruit_bitmap_font import bitmap_font
61
61
62
+ import storage
63
+ import adafruit_sdcard
62
64
import displayio
63
65
import audioio
64
66
import rtc
65
67
import supervisor
66
68
67
69
try :
68
- from settings import settings
70
+ from secrets import secrets
69
71
except 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""" )
72
74
raise
73
75
74
76
__version__ = "0.0.0-auto.0"
@@ -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 error :
221
+ print ("No SD card found:" , error )
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
@@ -529,9 +544,9 @@ def _connect_esp(self):
529
544
while not self ._esp .is_connected :
530
545
if self ._debug :
531
546
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
533
548
self .neo_status ((100 , 0 , 0 )) # red = not connected
534
- self ._esp .connect (settings )
549
+ self ._esp .connect (secrets )
535
550
536
551
def fetch (self ):
537
552
"""Fetch data from the url we initialized with, perfom any parsing,
@@ -575,15 +590,15 @@ def fetch(self):
575
590
supervisor .reload ()
576
591
577
592
if self ._regexp_path :
578
- import ure
593
+ import re
579
594
580
595
# extract desired text/values from json
581
596
if self ._json_path :
582
597
for path in self ._json_path :
583
598
values .append (PyPortal ._json_traverse (json_out , path ))
584
599
elif self ._regexp_path :
585
600
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 ))
587
602
else :
588
603
values = r .text
589
604
@@ -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 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 )
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