Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gallaugher/be4c249540447f28f3cca2c1c3930093 to your computer and use it in GitHub Desktop.
Save gallaugher/be4c249540447f28f3cca2c1c3930093 to your computer and use it in GitHub Desktop.
code_for_both_cowbell_adaloger_SD_and_st7735r_display.py
import board
import busio
import sdcardio
import storage
import os
import time, digitalio
from audiomp3 import MP3Decoder
# st7735r_display_160x128.py
import displayio
import pwmio
import terminalio
from adafruit_display_text import label
import adafruit_st7735r
displayio.release_displays()
# SPI setup (SPI0)
spi_display = busio.SPI(clock=board.GP2, MOSI=board.GP3)
# PWM Backlight
backlight = pwmio.PWMOut(board.GP9, frequency=5000, duty_cycle=65535)
# Display bus using ST7735R wiring
display_bus = displayio.FourWire(
spi_display,
command=board.GP6, # DC
chip_select=board.GP7, # 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)
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
SD_CS = board.GP13
# SPI setup for SD card
spi_sd = busio.SPI(clock=board.GP10, MOSI=board.GP11, MISO=board.GP12)
# โœ… Only attempt to initialize the SD card once inside the try block
try:
sdcard = sdcardio.SDCard(spi_sd, SD_CS)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
print("๐Ÿ˜Ž sd card mounted")
except ValueError:
print("โŒ no SD card")
# โœ… All the following code must be OUTSIDE the `except` block
path = "/sd/robot_sounds/"
# setup the MP3Decoder 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment