-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSL-Relay-backup.py
84 lines (59 loc) · 1.88 KB
/
LSL-Relay-backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import socket
import sys
import json
from pylsl import StreamInfo, StreamOutlet, local_clock
import pylsl
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((HOST, PORT))
except socket.error as msg:
print('Bind failed. Error code: ' + str(msg[0]) + ' Message ' + msg[1])
sys.exit()
print('Socket bind complete')
s.listen(10)
conn, addr = s.accept()
out = open('x.txt', 'w')
l = []
print('Connected with ' + addr[0] + ':' + str(addr[1]))
srate = 1000
name = 'MindLogger'
type = 'live_event'
n_channels = 3
info = StreamInfo(name, type, n_channels, srate, 'float32', 'ml-' + socket.gethostname())
ml_channels = info.desc().append_child('channels')
for label in ['x', 'y', 'time']:
ch = ml_channels.append_child('channel')
ch.append_child_value('label', label)
ch.append_child_value('unit', 'pixels')
ch.append_child_value('type', 'live_event')
outlet = StreamOutlet(info)
print('Now sending ML data...')
cont = True
conn.settimeout(.01)
while cont:
now = local_clock()
try:
r = conn.recv(4096).decode('utf-8')
print(r)
if 'live_event' in r:
if '$$$' in r:
r = r.split('$$$')
else:
r = [r]
for x in r:
if len(x) > 0 and x[-1] == '}':
item = json.loads(x)
values = [item['data']['x'], item['data']['y'], item['data']['time']]
# values = [item['data'][k] for k in item['data'].keys()]
out.write(str(values) + '\n')
outlet.push_sample(pylsl.vectord(values), now, True)
except KeyboardInterrupt:
cont = False
except socket.timeout:
values = [0, 0, 0]
outlet.push_sample(pylsl.vectord(values), now, True)
except json.decoder.JSONDecodeError:
print('Decoding error')
out.close()