Skip to content

Instantly share code, notes, and snippets.

@mkassner
Last active August 29, 2015 14:06
Show Gist options
  • Save mkassner/c66bce144846358d8f4f to your computer and use it in GitHub Desktop.
Save mkassner/c66bce144846358d8f4f to your computer and use it in GitHub Desktop.
pupil remote scripts

Pupil Remote

This collection of scripts emulates a toolchain where:

  • a UDP signal is used to broadcast the beginning of a recording. - udp_broadcast_server.py
  • a bridge script relays the information using 0MQ. - udp_serial_bridge.py
  • Pupil Capture Plugin Pupil_Remote will start the appropriate fn in Capture.

Pupil Remote can be emulated with test_zmq_server.py

Currently supported controls:

  • "R recording_name" : start a recording, sending "R" again will stop the recording
  • "T" : set timebase to 0
  • "C": start callibration
'''
This is a test server. It will listen for commands and echo them.
'''
import zmq
import time
port = "50020"
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:%s" % port)
while True:
try:
msg = socket.recv(flags=zmq.NOBLOCK)
except zmq.ZMQError :
msg = None
if msg:
print msg
socket.send("%s - confirmed"%msg)
time.sleep(.1)
print 'wait'
context.close()
'''
send dummy udp packets
'''
MYPORT = 50005
import sys, time
from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
while 1:
data = 'R:' + repr(time.time())
s.sendto(data, ('<broadcast>', MYPORT))
print "send %s"%data
time.sleep(5)
#UDP socket setup
import select, socket
import platform
port = 50005 # where do you expect to get a msg?
bufferSize = 1024 # whatever you need
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if platform.system() == 'Darwin':
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
else:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', port))
s.setblocking(0)
#zmq setup
import zmq
context = zmq.Context()
z = context.socket(zmq.REQ)
z.connect("tcp://localhost:50020") #configure for localhost on port 50020
while True:
result = select.select([s],[],[])
#get udp message string
msg = result[0][0].recv(bufferSize)
print msg
#relay to pupil remote.
z.send(msg)
#await confirmation
print z.recv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment