diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 42c2171..5c62b0a 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -63,8 +63,15 @@ import rtc import supervisor from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError -import adafruit_sdcard +try: + import sdcardio + + NATIVE_SD = True +except ImportError: + import adafruit_sdcard as sdcardio + + NATIVE_SD = False if hasattr(board, "TOUCH_XL"): import adafruit_touchscreen @@ -328,10 +335,12 @@ def __init__( if self._debug: print("Init SD Card") - sd_cs = DigitalInOut(board.SD_CS) + sd_cs = board.SD_CS + if not NATIVE_SD: + sd_cs = DigitalInOut(sd_cs) self._sdcard = None try: - self._sdcard = adafruit_sdcard.SDCard(spi, sd_cs) + self._sdcard = sdcardio.SDCard(spi, sd_cs) vfs = storage.VfsFat(self._sdcard) storage.mount(vfs, "/sd") except OSError as error: @@ -660,10 +669,10 @@ def get_local_time(self, location=None): try: aio_username = secrets["aio_username"] aio_key = secrets["aio_key"] - except KeyError: + except KeyError as error: raise KeyError( "\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'" # pylint: disable=line-too-long - ) + ) from error location = secrets.get("timezone", location) if location: @@ -686,10 +695,10 @@ def get_local_time(self, location=None): year_day = int(times[2]) week_day = int(times[3]) is_dst = None # no way to know yet - except KeyError: + except KeyError as error: raise KeyError( "Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones" # pylint: disable=line-too-long - ) + ) from error year, month, mday = [int(x) for x in the_date.split("-")] the_time = the_time.split(".")[0] hours, minutes, seconds = [int(x) for x in the_time.split(":")] @@ -778,10 +787,10 @@ def image_converter_url(image_url, width, height, color_depth=16): try: aio_username = secrets["aio_username"] aio_key = secrets["aio_key"] - except KeyError: + except KeyError as error: raise KeyError( "\n\nOur image converter service require a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'" # pylint: disable=line-too-long - ) + ) from error return IMAGE_CONVERTER_SERVICE % ( aio_username, @@ -813,10 +822,10 @@ def push_to_io(self, feed_key, data): try: aio_username = secrets["aio_username"] aio_key = secrets["aio_key"] - except KeyError: + except KeyError as error: raise KeyError( "Adafruit IO secrets are kept in secrets.py, please add them there!\n\n" - ) + ) from error wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( self._esp, secrets, None @@ -970,13 +979,11 @@ def fetch(self, refresh_url=None, timeout=10): try: self.wget(image_url, filename, chunk_size=chunk_size) except OSError as error: - print(error) 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 - ) + ) from error except RuntimeError as error: - print(error) - raise RuntimeError("wget didn't write a complete file") + raise RuntimeError("wget didn't write a complete file") from error if iwidth < iheight: pwidth = int( self._image_resize[1] diff --git a/docs/conf.py b/docs/conf.py index 36fbb1a..c355d20 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -37,6 +37,7 @@ "secrets", "adafruit_sdcard", "storage", + "sdcardio", "adafruit_io", "adafruit_cursorcontrol", "adafruit_requests",