Skip to content

Instantly share code, notes, and snippets.

@mkassner
Created March 21, 2014 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkassner/9682161 to your computer and use it in GitHub Desktop.
Save mkassner/9682161 to your computer and use it in GitHub Desktop.
Pupil_OSC_bridge
'''
simple python osc test server that will print pupil/norm_pos mssages
installing pyOSC:
git clone git://gitorious.org/pyosc/devel.git pyosc
cd pyosc
python setup.py install (may need sudo)
'''
from OSC import OSCServer
import sys
from time import sleep
server = OSCServer( ("localhost", 7110) )
server.timeout = 0
run = True
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True
# funny python's way to add a method to an instance of a class
import types
server.handle_timeout = types.MethodType(handle_timeout, server)
def callback(path, tags, args, source):
print path,args
server.addMsgHandler( "/pupil/norm_pos", callback )
while run:
# do the game stuff:
sleep(0.01)
# call user script
# clear timed_out flag
server.timed_out = False
# handle all pending requests then return
while not server.timed_out:
server.handle_request()
server.close()
'''
a script that will replay pupil server messages to a osc server.
as implemented here only the pupil_norm_pos is relayed.
implementeing other messages to be send as well is a matter of renaming the vaiables.
installing pyOSC:
git clone git://gitorious.org/pyosc/devel.git pyosc
cd pyosc
python setup.py install (may need sudo)
'''
#zmq setup
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:5000")
#filter by messages by stating string 'STRING'. '' receives all messages
socket.setsockopt(zmq.SUBSCRIBE, '')
#osc setup
from OSC import OSCClient, OSCMessage
client = OSCClient()
client.connect( ("localhost", 7110) )
while True:
msg = socket.recv()
print "raw msg:\n", msg
items = msg.split("\n")
msg_type = items.pop(0)
items = dict([i.split(':') for i in items[:-1] ])
if msg_type == 'Pupil':
if items.get('norm_pupil', 'None') != "None":
pupil_x,pupil_y = map(float,items['norm_pupil'][1:-1].split(','))
client.send( OSCMessage("/pupil/norm_pos", (pupil_y,pupil_y)) )
else:
# process event msgs from plugins here
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment