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