|
| 1 | +import time |
| 2 | +import board |
| 3 | +import busio |
| 4 | +from adafruit_mcp230xx.mcp23017 import MCP23017 |
| 5 | +from digitalio import Direction |
| 6 | +import adafruit_ble |
| 7 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 8 | +import adafruit_ble_midi |
| 9 | + |
| 10 | +# These import auto-register the message type with the MIDI machinery. |
| 11 | +# pylint: disable=unused-import |
| 12 | +import adafruit_midi |
| 13 | +from adafruit_midi.control_change import ControlChange |
| 14 | +from adafruit_midi.midi_message import MIDIUnknownEvent |
| 15 | +from adafruit_midi.note_off import NoteOff |
| 16 | +from adafruit_midi.note_on import NoteOn |
| 17 | +from adafruit_midi.pitch_bend import PitchBend |
| 18 | + |
| 19 | +# i2c setup |
| 20 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 21 | + |
| 22 | +# i2c addresses for muxes |
| 23 | +mcp1 = MCP23017(i2c, address=0x20) |
| 24 | +mcp2 = MCP23017(i2c, address=0x21) |
| 25 | + |
| 26 | +# 1st solenoid array, corresponds with 1st mux |
| 27 | +noids0 = [] |
| 28 | + |
| 29 | +for pin in range(16): |
| 30 | + noids0.append(mcp1.get_pin(pin)) |
| 31 | +for n in noids0: |
| 32 | + n.direction = Direction.OUTPUT |
| 33 | + |
| 34 | +# 2nd solenoid array, corresponds with 2nd mux |
| 35 | +noids1 = [] |
| 36 | + |
| 37 | +for pin in range(16): |
| 38 | + noids1.append(mcp2.get_pin(pin)) |
| 39 | +for n in noids1: |
| 40 | + n.direction = Direction.OUTPUT |
| 41 | + |
| 42 | +# MIDI note arrays. notes0 = noids0; notes1 = noids1 |
| 43 | +notes0 = [55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70] |
| 44 | +notes1 = [71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86] |
| 45 | + |
| 46 | +# setup MIDI BLE service |
| 47 | +midi_service = adafruit_ble_midi.MIDIService() |
| 48 | +advertisement = ProvideServicesAdvertisement(midi_service) |
| 49 | + |
| 50 | +# BLE connection setup |
| 51 | +ble = adafruit_ble.BLERadio() |
| 52 | +if ble.connected: |
| 53 | + for c in ble.connections: |
| 54 | + c.disconnect() |
| 55 | + |
| 56 | +# MIDI in setup |
| 57 | +midi = adafruit_midi.MIDI(midi_in=midi_service, in_channel=0) |
| 58 | + |
| 59 | +# start BLE advertising |
| 60 | +print("advertising") |
| 61 | +ble.start_advertising(advertisement) |
| 62 | + |
| 63 | +# delay for solenoids |
| 64 | +speed = 0.01 |
| 65 | + |
| 66 | +while True: |
| 67 | + |
| 68 | + # waiting for BLE connection |
| 69 | + print("Waiting for connection") |
| 70 | + while not ble.connected: |
| 71 | + pass |
| 72 | + print("Connected") |
| 73 | + # delay after connection established |
| 74 | + time.sleep(1.0) |
| 75 | + |
| 76 | + while ble.connected: |
| 77 | + |
| 78 | + # msg holds MIDI messages |
| 79 | + msg = midi.receive() |
| 80 | + |
| 81 | + for i in range(16): |
| 82 | + # states for solenoid on/off |
| 83 | + # noid0 = mux1 |
| 84 | + # noid1 = mux2 |
| 85 | + noid0_output = noids0[i] |
| 86 | + noid1_output = noids1[i] |
| 87 | + |
| 88 | + # states for MIDI note recieved |
| 89 | + # notes0 = mux1 |
| 90 | + # notes1 = mux2 |
| 91 | + notes0_played = notes0[i] |
| 92 | + notes1_played = notes1[i] |
| 93 | + |
| 94 | + # if NoteOn msg comes in and the MIDI note # matches with predefined notes: |
| 95 | + if isinstance(msg, NoteOn) and msg.note is notes0_played: |
| 96 | + print(time.monotonic(), msg.note) |
| 97 | + |
| 98 | + # solenoid is triggered |
| 99 | + noid0_output.value = True |
| 100 | + # quick delay |
| 101 | + time.sleep(speed) |
| 102 | + # solenoid retracts |
| 103 | + noid0_output.value = False |
| 104 | + |
| 105 | + # identical to above if statement but for mux2 |
| 106 | + if isinstance(msg, NoteOn) and msg.note is notes1_played: |
| 107 | + print(time.monotonic(), msg.note) |
| 108 | + |
| 109 | + noid1_output.value = True |
| 110 | + |
| 111 | + time.sleep(speed) |
| 112 | + |
| 113 | + noid1_output.value = False |
| 114 | + |
| 115 | + # if BLE disconnects try reconnecting |
| 116 | + print("Disconnected") |
| 117 | + print() |
| 118 | + ble.start_advertising(advertisement) |
0 commit comments