Created
September 21, 2020 17:56
-
-
Save hierophect/ef38920b27a9d0cf4770138525f7084c to your computer and use it in GitHub Desktop.
SD card test sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### SD CARD ---------------- | |
import busio | |
import sdcardio | |
import board | |
import storage | |
import sys | |
import os | |
# Connect to the card and mount the filesystem. | |
spi = busio.SPI(clock=board.IO5, MOSI=board.IO4, MISO=board.IO3) | |
sdcard = sdcardio.SDCard(spi, board.IO6) | |
vfs = storage.VfsFat(sdcard) | |
storage.mount(vfs, "/sd") | |
sys.path.append("/sd") | |
sys.path.append("/sd/lib") | |
os.listdir("/sd") | |
def print_directory(path, tabs=0): | |
for file in os.listdir(path): | |
stats = os.stat(path + "/" + file) | |
filesize = stats[6] | |
isdir = stats[0] & 0x4000 | |
if filesize < 1000: | |
sizestr = str(filesize) + " by" | |
elif filesize < 1000000: | |
sizestr = "%0.1f KB" % (filesize / 1000) | |
else: | |
sizestr = "%0.1f MB" % (filesize / 1000000) | |
prettyprintname = "" | |
for _ in range(tabs): | |
prettyprintname += " " | |
prettyprintname += file | |
if isdir: | |
prettyprintname += "/" | |
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr)) | |
# recursively print directory contents | |
if isdir: | |
print_directory(path + "/" + file, tabs + 1) | |
print("Files on filesystem:") | |
print("====================") | |
print_directory("/sd") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment