-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch2osc.py
executable file
·68 lines (56 loc) · 1.69 KB
/
scratch2osc.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
#!/usr/bin/env python
import sys
import os
import scratch
import time
from OSC import OSCClient, OSCMessage
VERSION = "0.0.1"
OSC_HOST = "127.0.0.1"
OSC_PORT = 8000
OSC_ADDR = '/scratch/test'
def connect():
try:
return scratch.Scratch()
except scratch.ScratchError:
print _("Error: Unable to connect to Scratch. Scratch may be not running or the remote sensor connections may be not enabled.")
return None
def _listen(s):
while True:
try:
yield s.receive()
except scratch.ScratchError:
print _("Error: Disconnected from Scratch.")
raise StopIteration
def listen(s, osc_client):
for msg in _listen(s):
if (msg):
print "Received: %s" % str(msg)
if msg[0] == 'broadcast':
if msg[1] == 'goo':
osc_client.send(OSCMessage(OSC_ADDR, 'goo'))
print "Send goo to OSC."
if msg[1] == 'choki'
osc_client.send(OSCMessage(OSC_ADDR, 'choki'))
print "Send choki to OSC."
elif msg[1] == 'par':
osc_client.send(OSCMessage(OSC_ADDR, 'par'))
print "Send par to OSC."
elif msg[0] == 'sensor-update':
print "sensor-update"
def main():
print "================="
print "Sratch2OSC %s" % VERSION
print "================="
print ""
while True:
s = connect()
osc_client = OSCClient()
osc_client.connect(OSC_HOST, OSC_PORT)
if (s):
print _("Connected to Scratch")
s.broadcast("goo")
s.broadcast("choki")
s.broadcast("par")
listen(s, osc_client)
time.sleep(5)
main()