-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscpin.py
executable file
·79 lines (69 loc) · 2.09 KB
/
oscpin.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
#!/usr/bin/env python3
import sys, time, os
from housepy import osc, config, log, process
log.info("Starting up...")
try:
import RPi.GPIO as GPIO
except ImportError:
log.warning("--> using fake GPIO")
class GPIO(object):
BCM = 303
OUT = "out"
HIGH, LOW = 1, 0
PUD_UP = None
IN = 1
def setmode(n):
pass
def setup(n, m, pull_up_down=None):
pass
def output(n, m):
pass
def cleanup():
pass
def input(pin):
return None
process.secure_pid(os.path.abspath(os.path.join(os.path.dirname(__file__), "run")))
# https://www.raspberrypi.org/documentation/usage/gpio/
log.info("Setting up pins...")
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
outputs = [2, 3]#, 4, 17, 27, 22, 10, 9, 11]
inputs = [14, 15]#, 18, 23, 24, 25, 8, 7]
for pin in outputs:
log.info("--> %s output" % pin)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
for pin in inputs:
log.info("--> %s input" % pin)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
log.info("--> done")
def tap_on(pin):
GPIO.output(pin, GPIO.LOW)
if config['recording']:
sender.send("/noteon", [pin, str(time.time())])
def tap_off(pin):
GPIO.output(pin, GPIO.HIGH)
if config['recording']:
sender.send("/noteoff", [pin, str(time.time())])
def on_message(location, address, data):
pin = int(data[0])
if address == "/noteon":
tap_on(pin)
if address == "/noteoff":
tap_off(pin)
osc.Receiver(23232, on_message)
if config['recording']:
sender = osc.Sender(config['recorder'], 23232)
state = {pin: False for pin in inputs}
while True:
for p, pin in enumerate(inputs):
if state[pin] != GPIO.input(pin):
state[pin] = not state[pin]
if not state[pin]: # it's reverse
tap_on(outputs[p])
else:
tap_off(outputs[p])
time.sleep(0.01) # as fast as possible
else:
while True:
time.sleep(1)