Simple test

Ensure your device works with this simple test.

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