|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import os |
| 6 | +import board |
| 7 | +import audiocore |
| 8 | +import audiobusio |
| 9 | +from digitalio import DigitalInOut, Direction |
| 10 | +import displayio |
| 11 | +from adafruit_bitmap_font import bitmap_font |
| 12 | +from adafruit_display_text import label |
| 13 | +import adafruit_displayio_ssd1306 |
| 14 | +from adafruit_seesaw import seesaw, rotaryio |
| 15 | +import vectorio |
| 16 | +import keypad |
| 17 | + |
| 18 | +#display setup |
| 19 | +displayio.release_displays() |
| 20 | +# enable external power pin |
| 21 | +# provides power to the external components |
| 22 | +external_power = DigitalInOut(board.EXTERNAL_POWER) |
| 23 | +external_power.direction = Direction.OUTPUT |
| 24 | +external_power.value = False |
| 25 | + |
| 26 | +# rotary encoder |
| 27 | +i2c = board.STEMMA_I2C() |
| 28 | +seesaw = seesaw.Seesaw(i2c, addr=0x36) |
| 29 | +encoder = rotaryio.IncrementalEncoder(seesaw) |
| 30 | +last_position = 0 |
| 31 | + |
| 32 | +# oled |
| 33 | +oled_reset = board.D9 |
| 34 | +display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset) |
| 35 | +WIDTH = 128 |
| 36 | +HEIGHT = 64 |
| 37 | +display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT) |
| 38 | + |
| 39 | +# trigger button |
| 40 | +switch = keypad.Keys((board.EXTERNAL_BUTTON,), value_when_pressed=False, pull=True) |
| 41 | + |
| 42 | +# audio! |
| 43 | +wavs = [] |
| 44 | +wav_names = [] |
| 45 | +for filename in os.listdir('/wavs'): |
| 46 | + if filename.lower().endswith('.wav') and not filename.startswith('.'): |
| 47 | + wavs.append("/wavs/"+filename) |
| 48 | + wav_names.append(filename.replace('.wav', '')) |
| 49 | +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) |
| 50 | +num_wavs = len(wavs) |
| 51 | +wav_index = 0 |
| 52 | +# function to open and play the audio files |
| 53 | +def open_audio(num): |
| 54 | + n = wavs[num] |
| 55 | + f = open(n, "rb") |
| 56 | + w = audiocore.WaveFile(f) |
| 57 | + return w |
| 58 | +wave = open_audio(wav_index) |
| 59 | +audio.play(wave) |
| 60 | + |
| 61 | +# make the display context |
| 62 | +splash = displayio.Group() |
| 63 | +display.root_group = splash |
| 64 | +palette = displayio.Palette(1) |
| 65 | +palette[0] = 0xFFFFFF |
| 66 | +rect = vectorio.Rectangle(pixel_shader=palette, width=display.width, height=23, x=6, y=21) |
| 67 | +splash.append(rect) |
| 68 | +font = bitmap_font.load_font("/Arial-14.bdf") |
| 69 | +color = 0xFFFFFF |
| 70 | +text_0 = wav_names[(wav_index - 1) % num_wavs] |
| 71 | +text_area_top_left = label.Label(font, text=text_0, color=color) |
| 72 | +text_area_top_left.anchor_point = (0.0, 0.0) |
| 73 | +text_area_top_left.anchored_position = (6, 0) |
| 74 | +splash.append(text_area_top_left) |
| 75 | + |
| 76 | +text_1 = wav_names[wav_index] |
| 77 | +text_area_middle_left = label.Label(font, text=text_1,color=0x000000) |
| 78 | +text_area_middle_left.anchor_point = (0.0, 0.5) |
| 79 | +text_area_middle_left.anchored_position = (6, display.height / 2) |
| 80 | +splash.append(text_area_middle_left) |
| 81 | + |
| 82 | +text_2 = wav_names[(wav_index+1) % num_wavs] |
| 83 | +text_area_bottom_left = label.Label(font, text=text_2, color=color) |
| 84 | +text_area_bottom_left.anchor_point = (0.0, 1.0) |
| 85 | +text_area_bottom_left.anchored_position = (6, display.height) |
| 86 | +splash.append(text_area_bottom_left) |
| 87 | + |
| 88 | +while True: |
| 89 | + |
| 90 | + event = switch.events.get() |
| 91 | + position = encoder.position |
| 92 | + if position != last_position: |
| 93 | + if position > last_position: |
| 94 | + wav_index = (wav_index + 1) % num_wavs |
| 95 | + else: |
| 96 | + wav_index = (wav_index - 1) % num_wavs |
| 97 | + text_area_top_left.text = wav_names[(wav_index-1) % num_wavs] |
| 98 | + text_area_middle_left.text = wav_names[wav_index] |
| 99 | + text_area_bottom_left.text = wav_names[(wav_index+1) % num_wavs] |
| 100 | + last_position = position |
| 101 | + if event: |
| 102 | + if event.pressed: |
| 103 | + external_power.value = True |
| 104 | + wave = open_audio(wav_index) |
| 105 | + audio.play(wave) |
| 106 | + if event.released: |
| 107 | + external_power.value = False |
0 commit comments