-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
356 lines (315 loc) · 9.83 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Copyright (c) 2023 Michael Bell
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import board
import digitalio
from digitalio import DigitalInOut, Direction, Pull
from adafruit_mcp230xx.mcp23017 import MCP23017
import usb_midi
import adafruit_midi
from adafruit_midi.control_change import ControlChange
from adafruit_midi.note_on import NoteOn
from adafruit_midi.note_off import NoteOff
import busio
from analogio import AnalogIn
#set up expression pedals
swellShoe = AnalogIn(board.GP26)
swellValue = swellShoe.value // 512
choirShoe = AnalogIn(board.GP27)
choirValue = choirShoe.value // 512
cnclLast = 1
usb_midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=10)
#set up lists
setStatus = 1
presets = [0,0,0,0,0,0,0,0,0,0,0,0]
noteStatus = []
notePins = []
midiNum = 35
mcpAddresses = []
mcpCodes = [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27]
#board.what? Should be pin number from what I can see,
#run dir(board) from serial to check for button numbers.
#i2c codes:
#using format i2c = busio.I2C(board.SCL, board.SDA)
i2c0 = busio.I2C(board.GP1, board.GP0, frequency=1000000)
i2c1 = busio.I2C(board.GP3, board.GP2, frequency=1000000)
#cancel button
cncl = DigitalInOut(board.GP4)
cncl.direction = Direction.INPUT
cncl.pull = Pull.UP
#Set button
st = DigitalInOut(board.GP5)
st.direction = Direction.INPUT
st.pull = Pull.UP
#for loop for presets 0-11:
pinsPresets = [board.GP12,board.GP8,board.GP9,board.GP14,board.GP10,board.GP11,board.GP13,board.GP19,board.GP20,board.GP21,board.GP15,board.GP22]
for x in range(12):
presets[x] = DigitalInOut(pinsPresets[x])
presets[x].direction = Direction.INPUT
presets[x].pull = Pull.UP
#free memory by removing pinsPresets
del pinsPresets
#pin setup for mcp23017’s looks like we need 14 for this.
a = 0
z = 0
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c0, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
#begin second i2c bank
a = 0
mcp = MCP23017(i2c1, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
#for chip 12 we need 7 inputs, the rest are unused.
for b in range(7):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
for b in range(16):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
a = a + 1
mcp = MCP23017(i2c1, address = mcpCodes[a])
#this is for the toe studs, remove 205-209, and the ending from the toe-stud removal comment onwards if not using.
for b in range(12):
mcpAddresses.append(mcp.get_pin(b))
mcpAddresses[z].direction = digitalio.Direction.INPUT
mcpAddresses[z].pull = digitalio.Pull.UP
z = z + 1
#free memory deleting unneeded variable for hex addresses
del mcpCodes
#set initial values for noteStatus using pin
for x in range(183):
noteStatus.append(1)
#st.value if needed? 1 should be correct
#noteStatus.append(st.value)
for x in range(183, 215):
noteStatus.append(0)
#main program loop
while True:
#reset noteNum at start of loop
noteNum = 1
midiNum = 36
#set midi channel to 16 for cancel/set/memlevel
USB_MIDI_channel = 16
usb_midi.out_channel = USB_MIDI_channel - 1
#expression pedals
try:
currentSwell = swellShoe.value
except:
error = 1
if currentSwell // 512 != swellValue and currentSwell >= swellValue+3 or currentSwell <= swellValue-3:
swellValue = currentSwell // 512
usb_midi.send(ControlChange(1, swellValue))
try:
currentChoir = choirShoe.value
except:
error = 1
if currentChoir // 512 != choirValue and currentChoir >=choirValue+3 or currentChoir <= choirValue-3:
choirValue = currentChoir // 512
usb_midi.send(ControlChange(2, choirValue))
#cancel then set then mem minus then memplus buttons.
try:
curCncl = cncl.value
except:
error = 1
if curCncl == 0:
usb_midi.send(NoteOn(60, 64))
cnclLast = 0
if curCncl == 1 and cnclLast == 0:
usb_midi.send(NoteOff(60, 64))
cnclLast = 1
try:
curSt = st.value
except:
error = 1
if curSt == 0 and curSt != setStatus:
usb_midi.send(NoteOn(61, 64))
setStatus = 0
if curSt == 1 and curSt != setStatus:
usb_midi.send(NoteOff(61, 64))
setStatus = 1
#check presets and increment z to send unique midi signal for each preset button
z = 64
try:
for x in range(len(presets)):
if presets[x].value == 0:
usb_midi.send(NoteOn(z, 64))
z = z + 1
except:
pass
#begin looping through keyboards going swell great choir pedals:
USB_MIDI_channel = 1
midiNum = 36
usb_midi.out_channel = USB_MIDI_channel - 1
try:
for x in range(61):
y = mcpAddresses[x].value
if y == 0 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOn(midiNum, 64))
elif y == 1 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOff(midiNum, 64))
midiNum = midiNum + 1
except:
error = 1
#begin great
USB_MIDI_channel = 2
midiNum = 36
usb_midi.out_channel = USB_MIDI_channel - 1
try:
for x in range(61, 122):
y = mcpAddresses[x].value
if y == 0 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOn(midiNum, 64))
elif y == 1 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOff(midiNum, 64))
midiNum = midiNum + 1
except:
error = 1
#begin choir
USB_MIDI_channel = 3
midiNum = 36
usb_midi.out_channel = USB_MIDI_channel - 1
try:
for x in range(122, 183):
y = mcpAddresses[x].value
if y == 0 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOn(midiNum, 64))
elif y == 1 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOff(midiNum, 64))
midiNum = midiNum + 1
except:
error = 1
#begin pedals
USB_MIDI_channel = 4
midiNum = 36
usb_midi.out_channel = USB_MIDI_channel - 1
try:
for x in range(183, 215):
y = mcpAddresses[x].value
if y == 1 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOn(midiNum, 64))
elif y == 0 and y != noteStatus[x]:
noteStatus[x] = y
usb_midi.send(NoteOff(midiNum, 64))
midiNum = midiNum + 1
except:
error = 1
#begin footswitches: cut from here to end if you don't want foot switches, as well as lines 205-209 (it is marked with a comment)
USB_MIDI_channel = 16
usb_midi.out_channel = USB_MIDI_channel - 1
#check presets and increment z to send unique midi signal for each preset button
z = 64
try:
for x in range(215, 227)):
if mcpAddresses[x].value == 0:
usb_midi.send(NoteOn(z, 64))
z = z + 1
except:
error = 1