|
| 1 | +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +import time |
| 6 | +import os |
| 7 | +import random |
| 8 | +import board |
| 9 | +from digitalio import DigitalInOut, Direction |
| 10 | +import neopixel |
| 11 | +import audiocore |
| 12 | +import audiobusio |
| 13 | +import keypad |
| 14 | +import adafruit_lis3dh |
| 15 | + |
| 16 | +from rainbowio import colorwheel |
| 17 | +hue = 0 |
| 18 | +# enable external power pin |
| 19 | +# provides power to the external components |
| 20 | +external_power = DigitalInOut(board.EXTERNAL_POWER) |
| 21 | +external_power.direction = Direction.OUTPUT |
| 22 | +external_power.value = True |
| 23 | + |
| 24 | +# external neopixels |
| 25 | +num_pixels = 24 |
| 26 | +pixels = neopixel.NeoPixel(board.EXTERNAL_NEOPIXELS, num_pixels, brightness=0.4, auto_write=True) |
| 27 | + |
| 28 | +# external button |
| 29 | +switch = keypad.Keys((board.EXTERNAL_BUTTON,), value_when_pressed=False, pull=True) |
| 30 | + |
| 31 | +wavs = [] |
| 32 | +wav_names = [] |
| 33 | +for filename in os.listdir('/wavs'): |
| 34 | + if filename.lower().endswith('.wav') and not filename.startswith('.'): |
| 35 | + wavs.append("/wavs/"+filename) |
| 36 | + wav_names.append(filename.replace('.wav', '')) |
| 37 | + |
| 38 | +audio = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA) |
| 39 | + |
| 40 | +num_wavs = len(wavs) |
| 41 | +wav_index = 0 |
| 42 | + |
| 43 | +def open_audio(num): |
| 44 | + n = wavs[num] |
| 45 | + f = open(n, "rb") |
| 46 | + w = audiocore.WaveFile(f) |
| 47 | + wn = wav_names[num] |
| 48 | + return w, wn |
| 49 | +wave, wave_name = open_audio(wav_index) |
| 50 | + |
| 51 | +colors = [ |
| 52 | + {'label': "BLUE", 'color': 0x0000FF}, |
| 53 | + {'label': "RED", 'color': 0xFF0000}, |
| 54 | + {'label': "GREEN", 'color': 0x00FF00}, |
| 55 | + {'label': "YELLOW", 'color': 0xFFFF00}, |
| 56 | + {'label': "AQUA", 'color': 0x00FFFF}, |
| 57 | + {'label': "PURPLE", 'color': 0xFF00FF}, |
| 58 | + {'label': "PINK", 'color': 0xFF0055}, |
| 59 | + {'label': "ORANGE", 'color': 0xFF5500}, |
| 60 | + {'label': "WHITE", 'color': 0x555555}, |
| 61 | + ] |
| 62 | + |
| 63 | +i2c = board.I2C() |
| 64 | +int1 = DigitalInOut(board.ACCELEROMETER_INTERRUPT) |
| 65 | +lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
| 66 | +lis3dh.range = adafruit_lis3dh.RANGE_2_G |
| 67 | + |
| 68 | +while True: |
| 69 | + event = switch.events.get() |
| 70 | + if event: |
| 71 | + if event.pressed: |
| 72 | + wave, wave_name = open_audio(random.randint(0, 8)) |
| 73 | + audio.play(wave) |
| 74 | + for color in colors: |
| 75 | + if color['label'] in wave_name: |
| 76 | + pixels.fill(color['color']) |
| 77 | + else: |
| 78 | + pass |
| 79 | + time.sleep(.7) |
| 80 | + pixels.fill((0, 0, 0)) |
| 81 | + print('pressed') |
| 82 | + if event.released: |
| 83 | + pass |
| 84 | + if lis3dh.shake(shake_threshold=12): |
| 85 | + for v in wav_names: |
| 86 | + name = wav_names.index(v) |
| 87 | + if "SHAKE" in v: |
| 88 | + wave, wave_name = open_audio(name) |
| 89 | + audio.play(wave) |
| 90 | + for i in range(num_pixels): |
| 91 | + pixels[i] = colorwheel(hue) |
| 92 | + hue = (hue + 30) % 256 |
| 93 | + print(hue) |
| 94 | + time.sleep(.7) |
| 95 | + pixels.fill((0, 0, 0)) |
| 96 | + print('shake') |
0 commit comments