def __init__(self, pathFunc, channelIndices, zmqContext=None): if zmqContext == None: zmqContext = zmq.Context() self.socket = zmqContext.socket(zmq.SUB) for i in channelIndices: self.socket.connect(pathFunc(i)) self.socket.setsockopt(zmq.SUBSCRIBE, "") self._wb = SpikeWaveBuffer() self.callbacks = []
def update(self, dt): nSpikes = Spiker.update(self, dt) if nSpikes: t = time.time() * 44100 # convert time to microseconds for i in xrange(nSpikes): wb = SpikeWaveBuffer() wb.channel_id = self.id wb.time_stamp = int(t) for i in xrange(100): wb.wave_sample.append(random.random()) self.socket.send(wb.SerializeToString()) return nSpikes
class SpikeListener: def __init__(self, pathFunc, channelIndices, zmqContext=None): if zmqContext == None: zmqContext = zmq.Context() self.socket = zmqContext.socket(zmq.SUB) for i in channelIndices: self.socket.connect(pathFunc(i)) self.socket.setsockopt(zmq.SUBSCRIBE, "") self._wb = SpikeWaveBuffer() self.callbacks = [] def update(self): try: packet = self.socket.recv(zmq.NOBLOCK) self._wb.ParseFromString(packet) self.process_spike(self._wb) [c(self._wb) for c in self.callbacks] return 1 except zmq.ZMQError: return 0 def process_spike(self, wb): pass def register_callback(self, func): """ Register a function that will get called when a spike is received. the function should accept a single argument of type SpikeWaveBuffer """ self.callbacks.append(func)
#!/usr/bin/env python import os, random, time import zmq from spike_wave_pb2 import SpikeWaveBuffer ipcPath = "ipc:///tmp/spike_channels/0" if not os.path.exists('/tmp/spike_channels/'): os.makedirs('/tmp/spike_channels/') context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind(ipcPath) while True: wb = SpikeWaveBuffer() wb.channel_id = 0 wb.time_stamp = int(time.time()) for i in xrange(100): wb.wave_sample.append(random.random()) socket.send(wb.SerializeToString()) time.sleep(0.5)
#!/usr/bin/env python from pylab import * import zmq from spike_wave_pb2 import SpikeWaveBuffer ipcPath = "ipc:///tmp/spike_channels/0" context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect(ipcPath) socket.setsockopt(zmq.SUBSCRIBE, "") figure() ion() count = 0 while True: wb = SpikeWaveBuffer() wb.ParseFromString(socket.recv()) print wb plot(wb.wave_sample) draw()