-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
92 lines (78 loc) · 2.32 KB
/
switch.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
#!/usr/bin/python
import sys
import time
import datetime
import optparse
import OSC
try:
import RPi.GPIO as GPIO
except ImportError:
pass
from utils import shoot, spinner, log_post
def send_osc(client, msg):
msg.setAddress("/action")
if 'event' not in msg:
msg.extend(['event', 1])
try:
client.send(msg)
shoot('\n')
log_post('DEBUG: sending to the client: %s' % msg)
except OSC.OSCClientError as err:
log_post(
"ERROR: %s OSC.OSCClientError %s\n" % (
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), err
),
output=sys.stderr
)
def main(opts):
# connect to SuperCollider
client = OSC.OSCClient()
client.connect((opts.ip_addres, int(opts.port)))
oscmsg = OSC.OSCMessage()
log_post('INFO: connected to client to %s:%s' % (
opts.ip_addres, opts.port
))
# setup GPIO
pin = int(opts.pin_number)
state = False
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
state = GPIO.input(pin)
except NameError as err:
log_post('ERROR: %s' % err)
log_post('INFO: waiting to state change...')
spinner_ = spinner()
while True:
# read GPIO.
try:
read_state = GPIO.input(pin)
except NameError:
read_state = False
if state != read_state:
state = read_state
send_osc(client, oscmsg)
sys.stdout.write(spinner_.next())
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\b')
try:
GPIO.cleanup()
except NameError:
pass
if __name__ == '__main__':
cmdparser = optparse.OptionParser(usage="usage: %prog [OPTIONS]")
cmdparser.add_option(
"-a", "--addres", action="store", dest="ip_addres", default='127.0.0.1',
help="IP address of the receiver [default \'%default\']"
)
cmdparser.add_option(
"-p", "--port", action="store", dest="port", default=57120,
help="IP port of the receiver [default \'%default\']"
)
cmdparser.add_option(
"-n", "--pin", action="store", dest="pin_number", default=18,
help="GPIO pin number [default \'%default\']"
)
opts, args = cmdparser.parse_args()
main(opts)