-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
117 lines (108 loc) · 3.6 KB
/
utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import socket
import struct
from multiprocessing import Process, Queue
import pyqtgraph as pg
from pylsl import StreamInlet, resolve_byprop
import traceback
import time
def dataReaderLSL(streamName, q):
while True:
print("Waiting for LSL stream")
try:
results = resolve_byprop(prop='name', value=streamName)
while len(results) == 0:
time.sleep(0.25)
info = results[0]
inlet = StreamInlet(info, recover=False)
print("Streaming...")
# Read data in forever
try:
while True:
data = inlet.pull_sample()
if data:
q.put(np.array(data[0]))
time.sleep(1/200)
except Exception as e:
print(e)
pass
except Exception as e:
print(e)
pass
def dataReaderLSLChunk(streamName, q):
while True:
print("Waiting for LSL stream")
try:
results = resolve_byprop(prop='name', value=streamName)
while len(results) == 0:
time.sleep(0.25)
info = results[0]
inlet = StreamInlet(info, recover=False)
print("Streaming...")
# Read data in forever
try:
while True:
chunk, timestamps = inlet.pull_chunk()
if len(chunk) > 0:
q.put(np.array(chunk[len(chunk) - 1]))
time.sleep(1/120)
except Exception as e:
print(e)
pass
except Exception as e:
print(e)
pass
def dataReaderLSLWithChannelInfo(streamName, q, channelLabels):
while True:
print("Waiting for LSL stream")
try:
results = resolve_byprop(prop='name', value=streamName)
while len(results) == 0:
results = resolve_byprop(prop='name', value=streamName)
time.sleep(0.25)
info = results[0]
inlet = StreamInlet(info, recover=False)
info = inlet.info()
ch = info.desc().child("channels").child("channel")
for k in range(info.channel_count()):
channelLabels.append(ch.child_value("label"))
ch = ch.next_sibling()
#print("In dataReader: ")
#print(channelLabels)
print("Streaming...")
# Read data in forever
try:
while True:
data = inlet.pull_sample()
if data:
q.put(np.array(data[0]))
time.sleep(1/1000000)
except Exception as e:
print(e)
pass
except Exception as e:
print(traceback.format_exc())
pass
def dataReaderTCP(ip, port, n, q):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (ip, port)
sock.bind(server_address)
sock.listen(1)
print("Waiting for a TCP connection")
connection, client_address = sock.accept()
print("Streaming...")
unpacker = struct.Struct("!"+str(n) + "d")
try:
while True:
try:
print("Waiting for sample")
print(unpacker.size)
data = connection.recv(unpacker.size)
values = unpacker.unpack(data)
q.put(values)
print(q.qsize())
except Exception as e:
print(e)
pass
finally:
connection.close()