Skip to content

Commit 2532abe

Browse files
authored
Merge pull request #5 from mrmcwethy/examples
Added examples folder and an example .py
2 parents 761efb5 + e29610a commit 2532abe

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

adafruit_sdcard.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ def _write(self, token, buf, start=0, end=None):
337337
while cmd[0] == 0:
338338
spi.readinto(cmd, end=1, write_value=0xff)
339339

340+
return 0 # worked
341+
340342
def count(self):
341343
"""Returns the total number of sectors.
342344

examples/read_directory.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)