Skip to content

Commit 74f5d77

Browse files
authored
Merge pull request #2622 from kattni/metro-rp2040-sd-demo
Adding SD card demo code.
2 parents 49d71b7 + f4defbb commit 74f5d77

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Adafruit_Metro_RP2040/SD_Read/code.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
CircuitPython Essentials SD Card Read Demo
6+
"""
7+
8+
import os
9+
import digitalio
10+
import board
11+
import storage
12+
import adafruit_sdcard
13+
14+
# The SD_CS pin is the chip select line.
15+
SD_CS = board.SD_CS
16+
17+
# Connect to the card and mount the filesystem.
18+
cs = digitalio.DigitalInOut(SD_CS)
19+
sdcard = adafruit_sdcard.SDCard(board.SPI(), cs)
20+
vfs = storage.VfsFat(sdcard)
21+
storage.mount(vfs, "/sd")
22+
23+
# Use the filesystem as normal! Our files are under /sd
24+
25+
# This helper function will print the contents of the SD
26+
def print_directory(path, tabs=0):
27+
for file in os.listdir(path):
28+
stats = os.stat(path + "/" + file)
29+
filesize = stats[6]
30+
isdir = stats[0] & 0x4000
31+
32+
if filesize < 1000:
33+
sizestr = str(filesize) + " bytes"
34+
elif filesize < 1000000:
35+
sizestr = "%0.1f KB" % (filesize / 1000)
36+
else:
37+
sizestr = "%0.1f MB" % (filesize / 1000000)
38+
39+
prettyprintname = ""
40+
for _ in range(tabs):
41+
prettyprintname += " "
42+
prettyprintname += file
43+
if isdir:
44+
prettyprintname += "/"
45+
print("{0:<40} Size: {1:>10}".format(prettyprintname, sizestr))
46+
47+
# recursively print directory contents
48+
if isdir:
49+
print_directory(path + "/" + file, tabs + 1)
50+
51+
52+
print("Files on filesystem:")
53+
print("====================")
54+
print_directory("/sd")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""
5+
CircuitPython Essentials SD Card Write Demo
6+
"""
7+
8+
import time
9+
import adafruit_sdcard
10+
import board
11+
import digitalio
12+
import microcontroller
13+
import storage
14+
15+
# The SD_CS pin is the chip select line.
16+
SD_CS = board.SD_CS
17+
18+
# Connect to the card and mount the filesystem.
19+
cs = digitalio.DigitalInOut(SD_CS)
20+
sdcard = adafruit_sdcard.SDCard(board.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+
print("Logging temperature to filesystem")
27+
# append to the file!
28+
while True:
29+
# open file for append
30+
with open("/sd/temperature.txt", "a") as f:
31+
t = microcontroller.cpu.temperature
32+
print("Temperature = %0.1f" % t)
33+
f.write("%0.1f\n" % t)
34+
# file is saved
35+
time.sleep(1)

0 commit comments

Comments
 (0)