From fdc539ccd44e882eb0296231553ac38712a4363e Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Fri, 5 Jan 2018 06:48:21 -0700 Subject: [PATCH 1/3] Added examples folder and an example .py --- examples/read_directory.py | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/read_directory.py diff --git a/examples/read_directory.py b/examples/read_directory.py new file mode 100644 index 0000000..c82bad0 --- /dev/null +++ b/examples/read_directory.py @@ -0,0 +1,55 @@ +import os +import busio +import digitalio +import board +import storage +import adafruit_sdcard + +# The SD_CS pin is the chip select line. +# +# The Adalogger Featherwing with ESP8266 Feather, the SD CS pin is on board.D15 +# The Adalogger Featherwing with Atmel M0 Feather, it's on board.D10 +# The Adafruit Feather M0 Adalogger use board.SD_CS +# For the breakout boards use any pin that is not taken by SPI + +SD_CS = board.SD_CS # setup for M0 Adalogger; change as needed + +# Connect to the card and mount the filesystem. +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +cs = digitalio.DigitalInOut(SD_CS) +sdcard = adafruit_sdcard.SDCard(spi, cs) +vfs = storage.VfsFat(sdcard) +storage.mount(vfs, "/sd") + +# Use the filesystem as normal! Our files are under /sd + +# This helper function will print the contents of the 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") From bade8ba83072b887f7bee24a3c220a5da63dda21 Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Fri, 5 Jan 2018 07:26:20 -0700 Subject: [PATCH 2/3] fixed the pylint complaint --- adafruit_sdcard.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_sdcard.py b/adafruit_sdcard.py index 3234750..dac853f 100644 --- a/adafruit_sdcard.py +++ b/adafruit_sdcard.py @@ -337,6 +337,8 @@ def _write(self, token, buf, start=0, end=None): while cmd[0] == 0: spi.readinto(cmd, end=1, write_value=0xff) + return 0 # worked + def count(self): """Returns the total number of sectors. From e29610af151c310ad816edd7588a666a43668989 Mon Sep 17 00:00:00 2001 From: mrmcwethy Date: Sat, 6 Jan 2018 13:24:04 -0700 Subject: [PATCH 3/3] fixed spelling issue --- examples/read_directory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/read_directory.py b/examples/read_directory.py index c82bad0..8a1fcf9 100644 --- a/examples/read_directory.py +++ b/examples/read_directory.py @@ -31,7 +31,7 @@ def print_directory(path, tabs=0): isdir = stats[0] & 0x4000 if filesize < 1000: - sizestr = str(filesize) + " by" + sizestr = str(filesize) + " bytes" elif filesize < 1000000: sizestr = "%0.1f KB" % (filesize/1000) else: