Last active
May 16, 2025 15:51
-
-
Save gallaugher/ee70df9f35c2b2b025c3fe8c87b1eeb8 to your computer and use it in GitHub Desktop.
display_and_sd_card.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you comment out either the audio block or display block, code runs fine. | |
# But if you try to run both, as is shown below, the error is: | |
# File "code.py", line 57, in <module> | |
# ValueError: SPI peripheral in use | |
import board, busio, sdcardio, storage, os, time, digitalio | |
import displayio, pwmio, terminalio, adafruit_st7735r | |
from adafruit_display_text import label | |
from audiomp3 import MP3Decoder | |
### \/ start comment below 'til you see the up pointing V to comment out audio code \/ | |
# Choose appropriate library for AudioOut object | |
try: | |
from audioio import AudioOut | |
except ImportError: | |
try: | |
from audiopwmio import PWMAudioOut as AudioOut | |
except ImportError: | |
print("This board does not support audio") | |
# use speaker location to creat an AudioOut object named audio | |
audio = AudioOut(board.GP15) | |
# SPI SD_CS pin for SD card on Adalogger Cowbell | |
SD_CS = board.GP17 | |
# SPI setup for SD card | |
spi1 = busio.SPI(board.GP18, board.GP19, board.GP16) | |
sdcard = sdcardio.SDCard(spi1, SD_CS) | |
vfs = storage.VfsFat(sdcard) | |
try: | |
storage.mount(vfs, "/sd") | |
print("😎 sd card mounted") | |
except ValueError: | |
print("❌ no SD card") | |
# Setup path & filenames | |
path = "/sd/robot_sounds/" | |
# setup the MP#Decoder object first & only once | |
filename = "0.mp3" | |
mp3_file = open(path + filename, "rb") # read in data from file | |
decoder = MP3Decoder(mp3_file) | |
def play_mp3(filename): | |
decoder.file = open(path + filename, "rb") | |
audio.play(decoder) | |
while audio.playing: | |
pass # replace with function name if you want concurrent operation | |
print("🐮 Cowbell Adalogger SD Test") | |
play_mp3("0.mp3") | |
play_mp3("1.mp3") | |
play_mp3("2.mp3") | |
### /\ end comment above 'til you see the up pointing V to comment out audio code /\ | |
### \/ start comment below 'til you see the up pointing V to comment out display code \/ | |
displayio.release_displays() | |
# SPI setup (SPI0) | |
spi0 = busio.SPI(clock=board.GP2, MOSI=board.GP3) | |
# PWM Backlight | |
backlight = pwmio.PWMOut(board.GP9, frequency=5000, duty_cycle=65535) | |
DISPLAY_CS = board.GP7 | |
# Display bus using ST7735R wiring | |
display_bus = displayio.FourWire( | |
spi0, | |
command=board.GP6, # DC | |
chip_select=DISPLAY_CS, # CS | |
reset=board.GP8 # RST | |
) | |
# THE CRUCIAL LINE (use ST7735R, native portrait, rotate into landscape) | |
display = adafruit_st7735r.ST7735R( | |
display_bus, | |
width=160, | |
height=128, | |
bgr=True, | |
rotation=90, | |
colstart=0, | |
rowstart=0 | |
) | |
# Root group | |
splash = displayio.Group() | |
display.root_group = splash | |
# Background fill | |
bg_bitmap = displayio.Bitmap(160, 128, 1) | |
bg_palette = displayio.Palette(1) | |
bg_palette[0] = 0x0033FF # Blue | |
bg = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette) | |
splash.append(bg) | |
# Text label | |
text_group = displayio.Group(scale=2, x=20, y=70) | |
text = label.Label(terminalio.FONT, text="Make Awesome!!", color=0xFFFF00) | |
text_group.append(text) | |
splash.append(text_group) | |
while True: | |
pass | |
### /\ end comment above 'til you see the up pointing V to comment out display code /\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment