New files for guide
This commit is contained in:
parent
9e843534e7
commit
e9b5577695
4 changed files with 135 additions and 0 deletions
33
CircuitPython_sdcardio_sdioio/list_files.py
Normal file
33
CircuitPython_sdcardio_sdioio/list_files.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import os
|
||||
|
||||
import mount_sd
|
||||
|
||||
def print_directory(path, tabs=0):
|
||||
for file in os.listdir(path):
|
||||
stats = os.stat(path + "/" + file)
|
||||
filesize = stats[6]
|
||||
isdir = stats[0] & 0x4000
|
||||
|
||||
if filesize < 1000:
|
||||
sizestr = str(filesize) + " by"
|
||||
elif filesize < 1000000:
|
||||
sizestr = "%0.1f KB" % (filesize / 1000)
|
||||
else:
|
||||
sizestr = "%0.1f MB" % (filesize / 1000000)
|
||||
|
||||
prettyprintname = ""
|
||||
for _ in range(tabs):
|
||||
prettyprintname += " "
|
||||
prettyprintname += file
|
||||
if isdir:
|
||||
prettyprintname += "/"
|
||||
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
|
||||
|
||||
# recursively print directory contents
|
||||
if isdir:
|
||||
print_directory(path + "/" + file, tabs + 1)
|
||||
|
||||
|
||||
print("Files on filesystem:")
|
||||
print("====================")
|
||||
print_directory("/sd")
|
||||
24
CircuitPython_sdcardio_sdioio/log_temperature.py
Normal file
24
CircuitPython_sdcardio_sdioio/log_temperature.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import time
|
||||
|
||||
import board
|
||||
import digitalio
|
||||
import microcontroller
|
||||
import mount_sd
|
||||
|
||||
led = digitalio.DigitalInOut(board.D13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
# Use the filesystem as normal! Our files are under /sd
|
||||
|
||||
print("Logging temperature to filesystem")
|
||||
# append to the file!
|
||||
while True:
|
||||
# open file for append
|
||||
with open("/sd/temperature.txt", "a") as f:
|
||||
led.value = True # turn on LED to indicate we're writing to the file
|
||||
t = microcontroller.cpu.temperature
|
||||
print("Temperature = %0.1f" % t)
|
||||
f.write("%0.1f\n" % t)
|
||||
led.value = False # turn off LED to indicate we're done
|
||||
# file is saved
|
||||
time.sleep(1)
|
||||
50
CircuitPython_sdcardio_sdioio/play_mp3s.py
Normal file
50
CircuitPython_sdcardio_sdioio/play_mp3s.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import os
|
||||
import time
|
||||
|
||||
import board
|
||||
import digitalio
|
||||
import mount_sd
|
||||
|
||||
# Updating the display can interfere with MP3 playback if it is not
|
||||
# done carefully
|
||||
try:
|
||||
board.DISPLAY.auto_refresh = False
|
||||
except:
|
||||
pass
|
||||
|
||||
from audiomp3 import MP3Decoder
|
||||
|
||||
try:
|
||||
from audioio import AudioOut
|
||||
except ImportError:
|
||||
try:
|
||||
from audiopwmio import PWMAudioOut as AudioOut
|
||||
except ImportError:
|
||||
pass # not always supported by every board!
|
||||
|
||||
# The mp3 files on the sd card will be played in alphabetical order
|
||||
mp3files = sorted("/sd/" + filename for filename in os.listdir("/sd")
|
||||
if filename.lower().endswith("mp3"))
|
||||
|
||||
voodoo = [1,2,3]
|
||||
|
||||
# You have to specify some mp3 file when creating the decoder
|
||||
mp3 = open(mp3files[0], "rb")
|
||||
decoder = MP3Decoder(mp3)
|
||||
audio = AudioOut(board.A0, right_channel=board.A1)
|
||||
|
||||
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
|
||||
speaker_enable.switch_to_output(True)
|
||||
|
||||
while True:
|
||||
for filename in mp3files:
|
||||
print("Playing", filename)
|
||||
|
||||
# Updating the .file property of the existing decoder
|
||||
# helps avoid running out of memory (MemoryError exception)
|
||||
decoder.file = open(filename, "rb")
|
||||
audio.play(decoder)
|
||||
|
||||
# This allows you to do other things while the audio plays!
|
||||
while audio.playing:
|
||||
time.sleep(1)
|
||||
28
CircuitPython_sdcardio_sdioio/show_bitmaps.py
Normal file
28
CircuitPython_sdcardio_sdioio/show_bitmaps.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import os
|
||||
import time
|
||||
|
||||
import board
|
||||
import digitalio
|
||||
import displayio
|
||||
import mount_sd
|
||||
|
||||
display = board.DISPLAY
|
||||
|
||||
# The bmp files on the sd card will be shown in alphabetical order
|
||||
bmpfiles = sorted("/sd/" + filename for filename in os.listdir("/sd")
|
||||
if filename.lower().endswith("bmp"))
|
||||
|
||||
while True:
|
||||
for filename in bmpfiles:
|
||||
print("showing", filename)
|
||||
|
||||
bitmap_file = open(filename, "rb")
|
||||
bitmap = displayio.OnDiskBitmap(bitmap_file)
|
||||
tile_grid = displayio.TileGrid(bitmap,
|
||||
pixel_shader=displayio.ColorConverter())
|
||||
group = displayio.Group()
|
||||
group.append(tile_grid)
|
||||
display.show(group)
|
||||
|
||||
# Show the image for 10 seconds
|
||||
time.sleep(10)
|
||||
Loading…
Reference in a new issue