|
| 1 | +from __future__ import print_function, unicode_literals |
| 2 | +from collections import OrderedDict |
| 3 | +from itertools import chain, repeat |
| 4 | +try: |
| 5 | + from itertools import izip |
| 6 | +except ImportError: |
| 7 | + izip = zip # Python 3 |
| 8 | +import struct |
| 9 | + |
| 10 | +import zmq |
| 11 | + |
| 12 | + |
| 13 | +# Event types |
| 14 | +TTL = 3 |
| 15 | +SPIKE = 4 |
| 16 | +MESSAGE = 5 |
| 17 | +BINARY_MSG = 6 |
| 18 | + |
| 19 | + |
| 20 | +def unpacker(format, *fields): |
| 21 | + s = struct.Struct(format) |
| 22 | + |
| 23 | + def unpack(data): |
| 24 | + values = s.unpack(data[:s.size]) |
| 25 | + if (len(values) == 1) and (not fields): |
| 26 | + assert len(data) == s.size |
| 27 | + return values[0], '' |
| 28 | + assert len(values) <= len(fields) |
| 29 | + return (OrderedDict(izip(fields, chain(values, repeat(None)))), |
| 30 | + data[s.size:]) |
| 31 | + |
| 32 | + return unpack |
| 33 | + |
| 34 | + |
| 35 | +unpack_standard = unpacker('3BxB', |
| 36 | + 'node_id', |
| 37 | + 'event_id', |
| 38 | + 'event_channel', |
| 39 | + 'source_node_id', |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +unpack_ttl = unpacker('<Q') |
| 44 | + |
| 45 | + |
| 46 | +unpack_spike = unpacker('<2q2x5H3B2fH', |
| 47 | + 'timestamp', |
| 48 | + 'timestamp_software', |
| 49 | + 'n_channels', |
| 50 | + 'n_samples', |
| 51 | + 'sorted_id', |
| 52 | + 'electrode_id', |
| 53 | + 'channel', |
| 54 | + 'color_r', 'color_g', 'color_b', |
| 55 | + 'pc_proj_x', 'pc_proj_y', |
| 56 | + 'sampling_frequency_hz', |
| 57 | + 'data', |
| 58 | + 'gain', |
| 59 | + 'threshold', |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def run(hostname='localhost', port=5557): |
| 64 | + with zmq.Context() as ctx: |
| 65 | + with ctx.socket(zmq.SUB) as sock: |
| 66 | + sock.connect('tcp://%s:%d' % (hostname, port)) |
| 67 | + |
| 68 | + for etype in (TTL, SPIKE, MESSAGE): |
| 69 | + sock.setsockopt(zmq.SUBSCRIBE, chr(etype).encode('utf-8')) |
| 70 | + |
| 71 | + while True: |
| 72 | + try: |
| 73 | + parts = sock.recv_multipart() |
| 74 | + assert len(parts) == 3 |
| 75 | + |
| 76 | + etype = ord(parts[0]) |
| 77 | + timestamp_seconds = struct.unpack('d', parts[1])[0] |
| 78 | + body = parts[2] |
| 79 | + |
| 80 | + if etype == SPIKE: |
| 81 | + spike, body = unpack_spike(body) |
| 82 | + print('%g: Spike: %s' % (timestamp_seconds, spike)) |
| 83 | + body = '' # TODO: unpack other data |
| 84 | + |
| 85 | + else: |
| 86 | + header, body = unpack_standard(body) |
| 87 | + |
| 88 | + if etype == TTL: |
| 89 | + word, body = unpack_ttl(body) |
| 90 | + print('%g: TTL: Channel %d: %s' % |
| 91 | + (timestamp_seconds, |
| 92 | + header['event_channel'] + 1, |
| 93 | + 'ON' if header['event_id'] else 'OFF')) |
| 94 | + |
| 95 | + elif etype == MESSAGE: |
| 96 | + msg, body = body.decode('utf-8'), '' |
| 97 | + print('%g: Message: %s' % (timestamp_seconds, msg)) |
| 98 | + |
| 99 | + |
| 100 | + # Check that all data was consumed |
| 101 | + assert len(body) == 0 |
| 102 | + |
| 103 | + except KeyboardInterrupt: |
| 104 | + print() # Add final newline |
| 105 | + break |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + run() |
0 commit comments