-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
132 lines (120 loc) · 3.67 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
# For Raspberry Pi Pico...
# GPIO2-14 are stackable outputs mapped to MIDI CC1-13 Ch1
# GPIO15-27 are non-stackable inputs mapped to MIDI CC14-26 Ch1
# When outputs are shorted to inputs, the matching pair of MIDI CC transmit
# level 127. When disconnected, transmits level 0.
# Use with Stoermelder's Pack-Tau T7 modules to map to module ports in VCVRack
# Attach tip connector of TRS socket to each GPIO pin. Short with mono 3.5mm patch leads!
import board
import time
import usb_midi
import adafruit_midi
from adafruit_midi.control_change import ControlChange
from digitalio import DigitalInOut, Direction, Pull
midi = adafruit_midi.MIDI(
midi_in=usb_midi.ports[0], in_channel=0, midi_out=usb_midi.ports[1], out_channel=0
)
# Convert channel numbers at the presentation layer to the ones musicians use
print("Default output channel:", midi.out_channel + 1)
outpins = (board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8,
board.GP9, board.GP10, board.GP11, board.GP12, board.GP13, board.GP14)
out_pin_start = 0
out_pin_count = len(outpins)
inpins = (board.GP15, board.GP16, board.GP17, board.GP18, board.GP19, board.GP20,
board.GP21, board.GP22, board.GP23, board.GP24, board.GP25, board.GP26,
board.GP27)
in_pin_start = 0
in_pin_count = len(inpins)
statematrix = {}
outswitches = []
inswitches = []
def build_state_matrix():
global in_pin_start
global in_pin_count
global out_pin_start
global out_pin_count
global statematrix
x = 0
while x < out_pin_count:
y = in_pin_start
row = {}
while y < in_pin_count:
z = f"{y}"
row[z] = 0
y += 1
m = f"{x}"
statematrix[m] = row
x += 1
def build_gpio_matrix():
global in_pin_start
global in_pin_count
global out_pin_start
global out_pin_count
global outswitches
global inswitches
x = out_pin_start
while x < out_pin_count:
outswitch = DigitalInOut(outpins[x])
outswitch.direction = Direction.INPUT
outswitch.pull = Pull.UP
outswitches.append(outswitch)
x += 1
y = in_pin_start
while y < in_pin_count:
inswitch = DigitalInOut(inpins[y])
inswitch.direction = Direction.INPUT
inswitch.pull = Pull.UP
inswitches.append(inswitch)
y += 1
def do_MIDI(outpin, inpin, state):
global midi
out_cc = outpin + 1
in_cc = inpin + out_pin_count + 1
midi.send(ControlChange(out_cc, state))
time.sleep(0.1)
midi.send(ControlChange(in_cc, state))
if state == 127:
status = "Connected"
else:
status = "Disconnected"
print(f"cc{out_cc} (output) to cc{in_cc} (input) - {status}")
def checkpin(outpin):
global in_pin_start
global in_pin_count
global statematrix
global outswitches
global inswitches
switch = outswitches[outpin]
switch.direction = Direction.OUTPUT
switch.value = 0
y = in_pin_start
row = {}
curr_row = statematrix[f"{outpin}"]
while y < in_pin_count:
inp = inswitches[y]
val = inp.value
z = f"{y}"
if val == 0:
row[z] = 1
else:
row[z] = 0
if row[z] != curr_row[z]:
if val == 0:
state = 127
else:
state = 0
do_MIDI(outpin, y, state)
y += 1
statematrix[f"{outpin}"] = row
switch.direction = Direction.INPUT
switch.pull = Pull.UP
build_state_matrix()
build_gpio_matrix()
print("Monitoring matrix for connections...")
y = out_pin_start
while y < out_pin_count:
checkpin(y)
if y == out_pin_count - 1:
y = out_pin_start
else:
y += 1