I am learning CircuitPython and I start my first exercice with an SD card.
Here is my code
Code: Select all
# Insert Module
import time
import board
import digitalio
import adafruit_sdcard
import busio
import storage
# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
# sdcard = adafruit_sdcard.SDCard(spi, cs)
# vfs = storage.VfsFat(sdcard)
# storage.mount(vfs, "/sd")
If I uncomment this
Code: Select all
# sdcard = adafruit_sdcard.SDCard(spi, cs)
Then the idea, is to have a condition and make sure that the script continuesTraceback (most recent call last):
File "main.py", line 24, in <module>
File "adafruit_sdcard.py", line 109, in __init__
File "adafruit_sdcard.py", line 130, in _init_card
OSError: no SD card
Code: Select all
if inserted or exist
SDInserted = True
else
SDInserted = False
Code: Select all
adafruit_sdcard.SDCard(spi, cs)
or I should use an exception?
I tried the following, does it make sense? Is a good practice?
Code: Select all
# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
try:
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
SDIserted = True
except Exception:
SDIserted = False
if SDIserted:
print('[INFO] SD is inserted')
else:
print('[INFO] SD is NOT Inserted')