code.py
1 # SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import os 6 7 import adafruit_sdcard 8 import board 9 import busio 10 import digitalio 11 import storage 12 13 # Use any pin that is not taken by SPI 14 SD_CS = board.D0 15 16 # Connect to the card and mount the filesystem. 17 spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 18 cs = digitalio.DigitalInOut(SD_CS) 19 sdcard = adafruit_sdcard.SDCard(spi, cs) 20 vfs = storage.VfsFat(sdcard) 21 storage.mount(vfs, "/sd") 22 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 28 29 def print_directory(path, tabs=0): 30 for file in os.listdir(path): 31 stats = os.stat(path + "/" + file) 32 filesize = stats[6] 33 isdir = stats[0] & 0x4000 34 35 if filesize < 1000: 36 sizestr = str(filesize) + " by" 37 elif filesize < 1000000: 38 sizestr = "%0.1f KB" % (filesize / 1000) 39 else: 40 sizestr = "%0.1f MB" % (filesize / 1000000) 41 42 prettyprintname = "" 43 for _ in range(tabs): 44 prettyprintname += " " 45 prettyprintname += file 46 if isdir: 47 prettyprintname += "/" 48 print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr)) 49 50 # recursively print directory contents 51 if isdir: 52 print_directory(path + "/" + file, tabs + 1) 53 54 55 print("Files on filesystem:") 56 print("====================") 57 print_directory("/sd")