|
| 1 | +# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import board |
| 5 | +from digitalio import DigitalInOut, Direction, Pull |
| 6 | +import neopixel |
| 7 | +import usb_midi |
| 8 | +import adafruit_midi |
| 9 | +from adafruit_midi.note_on import NoteOn |
| 10 | +from adafruit_midi.note_off import NoteOff |
| 11 | + |
| 12 | +# USB MIDI setup |
| 13 | +midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) |
| 14 | + |
| 15 | +# NeoPixel setup |
| 16 | +pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.02, auto_write=False) |
| 17 | + |
| 18 | +# setup for digital inputs |
| 19 | +inputs = [] |
| 20 | +cpx_pins = [board.A1, board.A2, board.A3, board.A4] |
| 21 | + |
| 22 | +for pin in cpx_pins: |
| 23 | + cpx_pin = DigitalInOut(pin) |
| 24 | + cpx_pin.direction = Direction.INPUT |
| 25 | + cpx_pin.pull = Pull.UP |
| 26 | + inputs.append(cpx_pin) |
| 27 | + |
| 28 | +# NeoPixel colors |
| 29 | +GREEN = (0, 255, 0) |
| 30 | +CYAN = (0, 255, 255) |
| 31 | +BLUE = (0, 0, 255) |
| 32 | +PURPLE = (180, 0, 255) |
| 33 | + |
| 34 | +# debounce states for inputs |
| 35 | +state_A1 = False |
| 36 | +state_A2 = False |
| 37 | +state_A3 = False |
| 38 | +state_A4 = False |
| 39 | + |
| 40 | +# array of NeoPixel colors |
| 41 | +colors = [GREEN, CYAN, BLUE, PURPLE] |
| 42 | +# array of MIDI notes |
| 43 | +midi_notes = [60, 64, 67, 72] |
| 44 | +# array of input states |
| 45 | +input_states = [state_A1, state_A2, state_A3, state_A4] |
| 46 | + |
| 47 | +while True: |
| 48 | + |
| 49 | + # iterate through colors, inputs and MIDI notes |
| 50 | + for i in range(4): |
| 51 | + # reset the state of the input and send NoteOff msg |
| 52 | + # after the input is released |
| 53 | + if inputs[i].value and input_states[i]: |
| 54 | + input_states[i] = False |
| 55 | + midi.send(NoteOff(midi_notes[i], 120)) |
| 56 | + |
| 57 | + # if an input is pressed... |
| 58 | + if not inputs[i].value and not input_states[i]: |
| 59 | + # change thhe colors of the NeoPixels |
| 60 | + pixels.fill(colors[i]) |
| 61 | + pixels.show() |
| 62 | + # send the NoteOn msg |
| 63 | + midi.send(NoteOn(midi_notes[i], 120)) |
| 64 | + # update input state |
| 65 | + input_states[i] = True |
0 commit comments