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)
Пример #2
0
#!/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()