Skip to content

Commit 2a92546

Browse files
authored
Merge pull request #1188 from jepler/sdcardio-sdioio-guide
New files for upcoming guide
2 parents 9e84353 + e9b5577 commit 2a92546

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
import mount_sd
4+
5+
def print_directory(path, tabs=0):
6+
for file in os.listdir(path):
7+
stats = os.stat(path + "/" + file)
8+
filesize = stats[6]
9+
isdir = stats[0] & 0x4000
10+
11+
if filesize < 1000:
12+
sizestr = str(filesize) + " by"
13+
elif filesize < 1000000:
14+
sizestr = "%0.1f KB" % (filesize / 1000)
15+
else:
16+
sizestr = "%0.1f MB" % (filesize / 1000000)
17+
18+
prettyprintname = ""
19+
for _ in range(tabs):
20+
prettyprintname += " "
21+
prettyprintname += file
22+
if isdir:
23+
prettyprintname += "/"
24+
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
25+
26+
# recursively print directory contents
27+
if isdir:
28+
print_directory(path + "/" + file, tabs + 1)
29+
30+
31+
print("Files on filesystem:")
32+
print("====================")
33+
print_directory("/sd")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import time
2+
3+
import board
4+
import digitalio
5+
import microcontroller
6+
import mount_sd
7+
8+
led = digitalio.DigitalInOut(board.D13)
9+
led.direction = digitalio.Direction.OUTPUT
10+
11+
# Use the filesystem as normal! Our files are under /sd
12+
13+
print("Logging temperature to filesystem")
14+
# append to the file!
15+
while True:
16+
# open file for append
17+
with open("/sd/temperature.txt", "a") as f:
18+
led.value = True # turn on LED to indicate we're writing to the file
19+
t = microcontroller.cpu.temperature
20+
print("Temperature = %0.1f" % t)
21+
f.write("%0.1f\n" % t)
22+
led.value = False # turn off LED to indicate we're done
23+
# file is saved
24+
time.sleep(1)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import time
3+
4+
import board
5+
import digitalio
6+
import mount_sd
7+
8+
# Updating the display can interfere with MP3 playback if it is not
9+
# done carefully
10+
try:
11+
board.DISPLAY.auto_refresh = False
12+
except:
13+
pass
14+
15+
from audiomp3 import MP3Decoder
16+
17+
try:
18+
from audioio import AudioOut
19+
except ImportError:
20+
try:
21+
from audiopwmio import PWMAudioOut as AudioOut
22+
except ImportError:
23+
pass # not always supported by every board!
24+
25+
# The mp3 files on the sd card will be played in alphabetical order
26+
mp3files = sorted("/sd/" + filename for filename in os.listdir("/sd")
27+
if filename.lower().endswith("mp3"))
28+
29+
voodoo = [1,2,3]
30+
31+
# You have to specify some mp3 file when creating the decoder
32+
mp3 = open(mp3files[0], "rb")
33+
decoder = MP3Decoder(mp3)
34+
audio = AudioOut(board.A0, right_channel=board.A1)
35+
36+
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
37+
speaker_enable.switch_to_output(True)
38+
39+
while True:
40+
for filename in mp3files:
41+
print("Playing", filename)
42+
43+
# Updating the .file property of the existing decoder
44+
# helps avoid running out of memory (MemoryError exception)
45+
decoder.file = open(filename, "rb")
46+
audio.play(decoder)
47+
48+
# This allows you to do other things while the audio plays!
49+
while audio.playing:
50+
time.sleep(1)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
import time
3+
4+
import board
5+
import digitalio
6+
import displayio
7+
import mount_sd
8+
9+
display = board.DISPLAY
10+
11+
# The bmp files on the sd card will be shown in alphabetical order
12+
bmpfiles = sorted("/sd/" + filename for filename in os.listdir("/sd")
13+
if filename.lower().endswith("bmp"))
14+
15+
while True:
16+
for filename in bmpfiles:
17+
print("showing", filename)
18+
19+
bitmap_file = open(filename, "rb")
20+
bitmap = displayio.OnDiskBitmap(bitmap_file)
21+
tile_grid = displayio.TileGrid(bitmap,
22+
pixel_shader=displayio.ColorConverter())
23+
group = displayio.Group()
24+
group.append(tile_grid)
25+
display.show(group)
26+
27+
# Show the image for 10 seconds
28+
time.sleep(10)

0 commit comments

Comments
 (0)