Neotrellis soundboard
This file contains 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
import adafruit_imageload | |
import audioio | |
import audiocore | |
import board | |
import busio | |
import digitalio | |
import displayio | |
import os | |
import random | |
import time | |
from adafruit_neotrellis.neotrellis import NeoTrellis | |
# Color definitions | |
OFF = (0, 0, 0) | |
RED = (25, 0, 0) | |
YELLOW = (20, 15, 0) | |
GREEN = (0, 25, 0) | |
CYAN = (0, 25, 25) | |
BLUE = (0, 0, 25) | |
PURPLE = (18, 0, 25) | |
WHITE = (127, 127, 127) | |
COLORS = [RED, YELLOW, GREEN, CYAN, BLUE, PURPLE] | |
# Set up Trellis | |
i2c_bus = busio.I2C(board.SCL, board.SDA) | |
trellis = NeoTrellis(i2c_bus) | |
# Set up Display | |
display = board.DISPLAY | |
# Load files | |
# All files are in the `/media/` directory. image-sound pairs have same basename with different | |
# extensions: .bmp for images, .wav for sounds. | |
all_items = [file.split('.')[0] for file in os.listdir("/media/") if (file.endswith(".bmp"))] | |
items = [] | |
for i in range(16): | |
rand_item = random.randint(0, len(all_items)-1) | |
items.append(all_items.pop(rand_item)) | |
def update_display(imagename="bear"): | |
bitmap, palette = adafruit_imageload.load('media/' + imagename + '.bmp', | |
bitmap=displayio.Bitmap, | |
palette= displayio.Palette) | |
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) | |
group = displayio.Group(scale=2) | |
group.append(tile_grid) | |
display.show(group) | |
# Set up audio | |
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) | |
speaker_enable.direction = digitalio.Direction.OUTPUT | |
speaker_enable.value = True | |
myaudio = audioio.AudioOut(board.A0) | |
audio_file = None | |
def play_file(audio_filename="bear"): | |
global audio_file | |
print("attempting to play " + audio_filename) | |
if myaudio.playing: | |
myaudio.stop() | |
if audio_file: | |
audio_file.close() | |
audio_file = open('media/'+audio_filename+'.wav', 'rb') | |
wav = audiocore.WaveFile(audio_file) | |
myaudio.play(wav) | |
def blink(event): | |
if event.edge == NeoTrellis.EDGE_RISING: | |
trellis.pixels[event.number] = WHITE | |
update_display(items[event.number]) | |
play_file(items[event.number]) | |
elif event.edge == NeoTrellis.EDGE_FALLING: | |
trellis.pixels[event.number] = trellis_colors[event.number] | |
# Initialize Trellis | |
trellis_colors = [] | |
for i in range(16): | |
trellis.activate_key(i, NeoTrellis.EDGE_RISING) | |
trellis.activate_key(i, NeoTrellis.EDGE_FALLING) | |
trellis.callbacks[i] = blink | |
trellis_colors.append(random.choice(COLORS)) | |
trellis.pixels[i] = trellis_colors[i] | |
time.sleep(0.05) | |
while True: | |
trellis.sync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment