-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEphysSocket.cpp
More file actions
381 lines (315 loc) · 11.4 KB
/
EphysSocket.cpp
File metadata and controls
381 lines (315 loc) · 11.4 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#ifdef _WIN32
#include <Windows.h>
#endif
#include "EphysSocket.h"
#include "EphysSocketEditor.h"
using namespace EphysSocketNode;
DataThread* EphysSocket::createDataThread (SourceNode* sn)
{
return new EphysSocket (sn);
}
EphysSocket::EphysSocket (SourceNode* sn) : DataThread (sn), socket ("socket_thread", this)
{
port = DEFAULT_PORT;
sample_rate = DEFAULT_SAMPLE_RATE;
total_samples = 0;
eventState = 0;
data_scale = DEFAULT_DATA_SCALE;
data_offset = DEFAULT_DATA_OFFSET;
sourceBuffers.add (new DataBuffer (socket.num_channels, sample_rate * bufferSizeInSeconds)); // start with 2 channels and automatically resize
}
std::unique_ptr<GenericEditor> EphysSocket::createEditor (SourceNode* sn)
{
std::unique_ptr<EphysSocketEditor> editor = std::make_unique<EphysSocketEditor> (sn, this);
return editor;
}
EphysSocket::~EphysSocket()
{
}
void EphysSocket::registerParameters()
{
addIntParameter (Parameter::PROCESSOR_SCOPE, "port", "Port", "Port number to connect to", DEFAULT_PORT, MIN_PORT, MAX_PORT);
addFloatParameter (Parameter::PROCESSOR_SCOPE, "sample_rate", "Sample Rate", "Sample rate of incoming data", "Hz", DEFAULT_SAMPLE_RATE, MIN_SAMPLE_RATE, MAX_SAMPLE_RATE, 1.0f);
addFloatParameter (Parameter::PROCESSOR_SCOPE, "data_scale", "Scale", "Scale of incoming data", "", DEFAULT_DATA_SCALE, MIN_DATA_SCALE, MAX_DATA_SCALE, 0.1f);
addFloatParameter (Parameter::PROCESSOR_SCOPE, "data_offset", "Offset", "Offset of incoming data", "", DEFAULT_DATA_OFFSET, MIN_DATA_OFFSET, MAX_DATA_OFFSET, 1.0f);
}
void EphysSocket::disconnectSocket()
{
socket.signalThreadShouldExit();
socket.waitForThreadToExit(1000);
socket.disconnectSocket();
getParameter ("port")->setEnabled (true);
getParameter ("sample_rate")->setEnabled (true);
getParameter ("data_scale")->setEnabled (true);
getParameter ("data_offset")->setEnabled (true);
if (sn->getEditor() != nullptr) // check if headless
static_cast<EphysSocketEditor*> (sn->getEditor())->disconnected();
}
bool EphysSocket::connectSocket (bool printOutput)
{
if (socket.connectSocket (port, printOutput))
{
getParameter ("port")->setEnabled (false);
getParameter ("sample_rate")->setEnabled (false);
getParameter ("data_scale")->setEnabled (false);
getParameter ("data_offset")->setEnabled (false);
if (sn->getEditor() != nullptr) // check if headless
static_cast<EphysSocketEditor*> (sn->getEditor())->connected();
return true;
}
return false;
}
bool EphysSocket::errorFlag()
{
return socket.isError();
}
void EphysSocket::resizeBuffers()
{
sourceBuffers[0]->resize (socket.num_channels, sample_rate * bufferSizeInSeconds);
convbuf.resize (socket.num_channels * socket.num_samp);
sampleNumbers.resize (socket.num_samp);
timestamps.clear();
timestamps.insertMultiple (0, 0.0, socket.num_samp);
ttlEventWords.resize (socket.num_samp);
}
void EphysSocket::updateSettings (OwnedArray<ContinuousChannel>* continuousChannels,
OwnedArray<EventChannel>* eventChannels,
OwnedArray<SpikeChannel>* spikeChannels,
OwnedArray<DataStream>* sourceStreams,
OwnedArray<DeviceInfo>* devices,
OwnedArray<ConfigurationObject>* configurationObjects)
{
continuousChannels->clear();
eventChannels->clear();
devices->clear();
spikeChannels->clear();
configurationObjects->clear();
sourceStreams->clear();
DataStream::Settings settings {
"EphysSocketStream",
"Data acquired via network stream",
"ephyssocket.data",
sample_rate
};
sourceStreams->add (new DataStream (settings));
sourceBuffers[0]->resize (socket.num_channels, sample_rate * bufferSizeInSeconds);
for (int ch = 0; ch < socket.num_channels; ch++)
{
ContinuousChannel::Settings settings {
ContinuousChannel::Type::ELECTRODE,
"CH" + String (ch + 1),
"Channel acquired via network stream",
"ephyssocket.continuous",
data_scale,
sourceStreams->getFirst()
};
continuousChannels->add (new ContinuousChannel (settings));
}
EventChannel::Settings eventSettings {
EventChannel::Type::TTL,
"Events",
"Events acquired via network stream",
"ephyssocket.events",
sourceStreams->getFirst(),
1
};
eventChannels->add (new EventChannel (eventSettings));
}
bool EphysSocket::foundInputSource()
{
return socket.isConnected();
}
bool EphysSocket::isReady()
{
return socket.isConnected();
}
void EphysSocket::parameterValueChanged (Parameter* parameter)
{
if (parameter->getName() == "port")
{
port = (int) parameter->getValue();
}
else if (parameter->getName() == "sample_rate")
{
sample_rate = (float) parameter->getValue();
CoreServices::updateSignalChain (sn); // Update the signal chain to reflect the new sample rate
}
else if (parameter->getName() == "data_scale")
{
data_scale = (float) parameter->getValue();
CoreServices::updateSignalChain (sn); // Update the signal chain to reflect the new data scale
}
else if (parameter->getName() == "data_offset")
{
data_offset = (float) parameter->getValue();
}
}
bool EphysSocket::startAcquisition()
{
resizeBuffers();
total_samples = 0;
eventState = 0;
socket.startAcquisition();
socket.startThread();
startThread();
return true;
}
bool EphysSocket::stopAcquisition()
{
if (isThreadRunning())
{
signalThreadShouldExit();
}
socket.stopAcquisition();
sourceBuffers[0]->clear();
return true;
}
template <typename T>
void EphysSocket::convertData (std::vector<std::byte> buffer)
{
T* buf = (T*) (buffer.data() + HEADER_SIZE);
for (int i = 0; i < socket.num_samp * socket.num_channels; i++)
{
convbuf[i] = data_scale * ((float) (buf[i]) - data_offset);
}
}
bool EphysSocket::updateBuffer()
{
if (socket.isError())
{
return false;
}
if (socket.data.isEmpty())
{
return true;
}
std::vector<std::byte> data = socket.data.removeAndReturn (0);
if (socket.depth == U8)
{
convertData<uint8_t> (data);
}
else if (socket.depth == S8)
{
convertData<int8_t> (data);
}
else if (socket.depth == U16)
{
convertData<uint16_t> (data);
}
else if (socket.depth == S16)
{
convertData<int16_t> (data);
}
else if (socket.depth == S32)
{
convertData<int32_t> (data);
}
else if (socket.depth == F32)
{
convertData<float_t> (data);
}
else if (socket.depth == F64)
{
convertData<double_t> (data);
}
for (int i = 0; i < socket.num_samp; i++)
{
sampleNumbers.set (i, total_samples++);
ttlEventWords.set (i, eventState);
}
sourceBuffers[0]->addToBuffer (convbuf.data(),
sampleNumbers.getRawDataPointer(),
timestamps.getRawDataPointer(),
ttlEventWords.getRawDataPointer(),
socket.num_samp);
return true;
}
String EphysSocket::handleConfigMessage (const String& msg)
{
// Available commands:
// ES INFO - Returns info on current variables that can be modified over HTTP
// ES SCALE <data_scale> - Updates the data scale to data_scale
// ES OFFSET <data_offset> - Updates the offset to data_offset
// ES PORT <port> - Updates the port number that EphysSocket connects to
// ES FREQUENCY <sample_rate> - Updates the sampling rate
if (CoreServices::getAcquisitionStatus())
{
return "Ephys Socket plugin cannot update settings while acquisition is active.";
}
if (socket.isConnected())
{
return "Ephys Socket plugin cannot update settings while connected to an active socket.";
}
StringArray parts = StringArray::fromTokens (msg, " ", "");
if (parts.size() > 0)
{
if (parts[0].equalsIgnoreCase ("ES"))
{
if (parts.size() == 3)
{
if (parts[1].equalsIgnoreCase ("SCALE"))
{
float scale = parts[2].getFloatValue();
if (scale > MIN_DATA_SCALE && scale < MAX_DATA_SCALE)
{
getParameter ("data_scale")->setNextValue (scale);
LOGC ("Scale updated to: ", scale);
return "SUCCESS";
}
return "Invalid scale requested. Scale can be set between '" + String (MIN_DATA_SCALE) + "' and '" + String (MAX_DATA_SCALE) + "'";
}
else if (parts[1].equalsIgnoreCase ("OFFSET"))
{
float offset = parts[2].getFloatValue();
if (offset >= MIN_DATA_OFFSET && offset < MAX_DATA_OFFSET)
{
getParameter ("data_offset")->setNextValue (offset);
LOGC ("Offset updated to: ", offset);
return "SUCCESS";
}
return "Invalid offset requested. Offset can be set between '" + String (MIN_DATA_OFFSET) + "' and '" + String (MAX_DATA_OFFSET) + "'";
}
else if (parts[1].equalsIgnoreCase ("PORT"))
{
float _port = parts[2].getFloatValue();
if (_port > MIN_PORT && _port < MAX_PORT)
{
getParameter ("port")->setNextValue (_port);
LOGC ("Port updated to: ", _port);
return "SUCCESS";
}
return "Invalid port requested. Port can be set between '" + String (MIN_PORT) + "' and '" + String (MAX_PORT) + "'";
}
else if (parts[1].equalsIgnoreCase ("FREQUENCY"))
{
float frequency = parts[2].getFloatValue();
if (frequency > MIN_SAMPLE_RATE && frequency < MAX_SAMPLE_RATE)
{
getParameter ("sample_rate")->setNextValue (frequency);
LOGC ("Frequency updated to: ", sample_rate);
return "SUCCESS";
}
return "Invalid frequency requested. Frequency can be set between '" + String (MIN_SAMPLE_RATE) + "' and '" + String (MAX_SAMPLE_RATE) + "'";
}
else
{
return "ES command " + parts[1] + "not recognized.";
}
}
else if (parts.size() == 2)
{
if (parts[1].equalsIgnoreCase ("INFO"))
{
return "Port = " + String (port) + ". Sample rate = " + String (sample_rate) + "Scale = " + String (data_scale) + ". Offset = " + String (data_offset) + ".";
}
else
{
return "ES command " + parts[1] + "not recognized.";
}
}
return "Unknown number of inputs given.";
}
return "Command not recognized.";
}
return "Empty message";
}