|
| 1 | +import os |
| 2 | +import busio |
| 3 | +import digitalio |
| 4 | +import board |
| 5 | +import storage |
| 6 | +import adafruit_sdcard |
| 7 | + |
| 8 | +# The SD_CS pin is the chip select line. |
| 9 | +# |
| 10 | +# The Adalogger Featherwing with ESP8266 Feather, the SD CS pin is on board.D15 |
| 11 | +# The Adalogger Featherwing with Atmel M0 Feather, it's on board.D10 |
| 12 | +# The Adafruit Feather M0 Adalogger use board.SD_CS |
| 13 | +# For the breakout boards use any pin that is not taken by SPI |
| 14 | + |
| 15 | +SD_CS = board.SD_CS # setup for M0 Adalogger; change as needed |
| 16 | + |
| 17 | +# Connect to the card and mount the filesystem. |
| 18 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 19 | +cs = digitalio.DigitalInOut(SD_CS) |
| 20 | +sdcard = adafruit_sdcard.SDCard(spi, cs) |
| 21 | +vfs = storage.VfsFat(sdcard) |
| 22 | +storage.mount(vfs, "/sd") |
| 23 | + |
| 24 | +# Use the filesystem as normal! Our files are under /sd |
| 25 | + |
| 26 | +# This helper function will print the contents of the SD |
| 27 | +def print_directory(path, tabs=0): |
| 28 | + for file in os.listdir(path): |
| 29 | + stats = os.stat(path+"/"+file) |
| 30 | + filesize = stats[6] |
| 31 | + isdir = stats[0] & 0x4000 |
| 32 | + |
| 33 | + if filesize < 1000: |
| 34 | + sizestr = str(filesize) + " bytes" |
| 35 | + elif filesize < 1000000: |
| 36 | + sizestr = "%0.1f KB" % (filesize/1000) |
| 37 | + else: |
| 38 | + sizestr = "%0.1f MB" % (filesize/1000000) |
| 39 | + |
| 40 | + prettyprintname = "" |
| 41 | + for _ in range(tabs): |
| 42 | + prettyprintname += " " |
| 43 | + prettyprintname += file |
| 44 | + if isdir: |
| 45 | + prettyprintname += "/" |
| 46 | + print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr)) |
| 47 | + |
| 48 | + # recursively print directory contents |
| 49 | + if isdir: |
| 50 | + print_directory(path+"/"+file, tabs+1) |
| 51 | + |
| 52 | + |
| 53 | +print("Files on filesystem:") |
| 54 | +print("====================") |
| 55 | +print_directory("/sd") |
0 commit comments