-
Notifications
You must be signed in to change notification settings - Fork 0
/
blink.py
66 lines (56 loc) · 1.67 KB
/
blink.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
import sys
import time
import mido
from pymata4 import pymata4
"""
Setup a pin for digital output and a MIDI port for input.
Note on messages turn on the pin, likewise for note offs.
Each Arduino pin to be used is assigned to a particular
MIDI note.
"""
"""
Set arduino pin numbers connected to valves, and the MIDI
notes which will connect to them through the virtual port
opened by MIDO
"""
DIGITAL_PINS = {60:6, # note 60 is MIDI for C3
62:7,
64:8,
65:13}
def blink(my_board, pins):
"""
This function will toggle a digital pin.
:param my_board: an PymataExpress instance
:param pins: dictionary mapping MIDI notes to pins
"""
# set the pin mode
for (note,pin) in pins.items():
my_board.set_pin_mode_digital_output(pin)
# open a MIDI port and control the LED
clio_port = mido.open_input('Clio', virtual=True)
while True:
message = clio_port.receive()
m_type = message.type
# if message is not a note on or note off type, ignore it
if not m_type in ['note_on', 'note_off']:
continue
m_note = message.note
# if message uses unassigned note, ignore it
try:
m_pin = pins[m_note]
except KeyError:
print("unassigned note: "+str(m_note))
continue
# send message to Arduino
if m_type == 'note_on':
my_board.digital_write(m_pin, 1)
elif m_type == 'note_off':
my_board.digital_write(m_pin, 0)
else:
pass
board = pymata4.Pymata4()
try:
blink(board, DIGITAL_PINS)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)