Simple test¶
Ensure your device works with this simple test.
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# This helper function will print the contents of the SD
30def print_directory(path, tabs=0):
31 for file in os.listdir(path):
32 stats = os.stat(path + "/" + file)
33 filesize = stats[6]
34 isdir = stats[0] & 0x4000
35
36 if filesize < 1000:
37 sizestr = str(filesize) + " bytes"
38 elif filesize < 1000000:
39 sizestr = "%0.1f KB" % (filesize / 1000)
40 else:
41 sizestr = "%0.1f MB" % (filesize / 1000000)
42
43 prettyprintname = ""
44 for _ in range(tabs):
45 prettyprintname += " "
46 prettyprintname += file
47 if isdir:
48 prettyprintname += "/"
49 print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
50
51 # recursively print directory contents
52 if isdir:
53 print_directory(path + "/" + file, tabs + 1)
54
55
56print("Files on filesystem:")
57print("====================")
58print_directory("/sd")