-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cpp
More file actions
603 lines (506 loc) · 20.1 KB
/
Device.cpp
File metadata and controls
603 lines (506 loc) · 20.1 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "mccoutlet/Device.hpp"
#include <lsl_cpp.h>
#include <uldaq.h>
#include <chrono>
#include <cstring>
#include <stdexcept>
#include <thread>
namespace mccoutlet {
namespace {
constexpr int MAX_DEV_COUNT = 100;
// Seconds of data to buffer in the circular scan buffer.
// Larger values tolerate more jitter but use more memory.
constexpr int SCAN_BUFFER_SECONDS = 10;
std::string ulErrorString(UlError err) {
char msg[ERR_MSG_LEN]{};
ulGetErrMsg(err, msg);
return std::string(msg);
}
std::string rangeToString(int range_id) {
switch (static_cast<Range>(range_id)) {
case BIP60VOLTS: return "+/-60V";
case BIP30VOLTS: return "+/-30V";
case BIP15VOLTS: return "+/-15V";
case BIP20VOLTS: return "+/-20V";
case BIP10VOLTS: return "+/-10V";
case BIP5VOLTS: return "+/-5V";
case BIP4VOLTS: return "+/-4V";
case BIP2PT5VOLTS: return "+/-2.5V";
case BIP2VOLTS: return "+/-2V";
case BIP1PT25VOLTS: return "+/-1.25V";
case BIP1VOLTS: return "+/-1V";
case BIPPT625VOLTS: return "+/-0.625V";
case BIPPT5VOLTS: return "+/-0.5V";
case BIPPT25VOLTS: return "+/-0.25V";
case BIPPT125VOLTS: return "+/-0.125V";
case BIPPT2VOLTS: return "+/-0.2V";
case BIPPT1VOLTS: return "+/-0.1V";
case BIPPT078VOLTS: return "+/-0.078V";
case BIPPT05VOLTS: return "+/-0.05V";
case BIPPT01VOLTS: return "+/-0.01V";
case BIPPT005VOLTS: return "+/-0.005V";
case BIP3VOLTS: return "+/-3V";
case BIPPT312VOLTS: return "+/-0.312V";
case BIPPT156VOLTS: return "+/-0.156V";
case UNI60VOLTS: return "0-60V";
case UNI30VOLTS: return "0-30V";
case UNI15VOLTS: return "0-15V";
case UNI20VOLTS: return "0-20V";
case UNI10VOLTS: return "0-10V";
case UNI5VOLTS: return "0-5V";
case UNI4VOLTS: return "0-4V";
case UNI2PT5VOLTS: return "0-2.5V";
case UNI2VOLTS: return "0-2V";
case UNI1PT25VOLTS: return "0-1.25V";
case UNI1VOLTS: return "0-1V";
case UNIPT625VOLTS: return "0-0.625V";
case UNIPT5VOLTS: return "0-0.5V";
case UNIPT25VOLTS: return "0-0.25V";
case UNIPT125VOLTS: return "0-0.125V";
case UNIPT2VOLTS: return "0-0.2V";
case UNIPT1VOLTS: return "0-0.1V";
case UNIPT078VOLTS: return "0-0.078V";
case UNIPT05VOLTS: return "0-0.05V";
case UNIPT01VOLTS: return "0-0.01V";
case UNIPT005VOLTS: return "0-0.005V";
case MA0TO20: return "0-20mA";
default: return "Range(" + std::to_string(range_id) + ")";
}
}
std::pair<double, double> rangeToVoltage(int range_id) {
switch (static_cast<Range>(range_id)) {
case BIP60VOLTS: return {-60.0, 60.0};
case BIP30VOLTS: return {-30.0, 30.0};
case BIP15VOLTS: return {-15.0, 15.0};
case BIP20VOLTS: return {-20.0, 20.0};
case BIP10VOLTS: return {-10.0, 10.0};
case BIP5VOLTS: return {-5.0, 5.0};
case BIP4VOLTS: return {-4.0, 4.0};
case BIP2PT5VOLTS: return {-2.5, 2.5};
case BIP2VOLTS: return {-2.0, 2.0};
case BIP1PT25VOLTS: return {-1.25, 1.25};
case BIP1VOLTS: return {-1.0, 1.0};
case BIPPT625VOLTS: return {-0.625, 0.625};
case BIPPT5VOLTS: return {-0.5, 0.5};
case BIPPT25VOLTS: return {-0.25, 0.25};
case BIPPT125VOLTS: return {-0.125, 0.125};
case BIPPT2VOLTS: return {-0.2, 0.2};
case BIPPT1VOLTS: return {-0.1, 0.1};
case BIPPT078VOLTS: return {-0.078, 0.078};
case BIPPT05VOLTS: return {-0.05, 0.05};
case BIPPT01VOLTS: return {-0.01, 0.01};
case BIPPT005VOLTS: return {-0.005, 0.005};
case BIP3VOLTS: return {-3.0, 3.0};
case BIPPT312VOLTS: return {-0.312, 0.312};
case BIPPT156VOLTS: return {-0.156, 0.156};
case UNI60VOLTS: return {0.0, 60.0};
case UNI30VOLTS: return {0.0, 30.0};
case UNI15VOLTS: return {0.0, 15.0};
case UNI20VOLTS: return {0.0, 20.0};
case UNI10VOLTS: return {0.0, 10.0};
case UNI5VOLTS: return {0.0, 5.0};
case UNI4VOLTS: return {0.0, 4.0};
case UNI2PT5VOLTS: return {0.0, 2.5};
case UNI2VOLTS: return {0.0, 2.0};
case UNI1PT25VOLTS: return {0.0, 1.25};
case UNI1VOLTS: return {0.0, 1.0};
case UNIPT625VOLTS: return {0.0, 0.625};
case UNIPT5VOLTS: return {0.0, 0.5};
case UNIPT25VOLTS: return {0.0, 0.25};
case UNIPT125VOLTS: return {0.0, 0.125};
case UNIPT2VOLTS: return {0.0, 0.2};
case UNIPT1VOLTS: return {0.0, 0.1};
case UNIPT078VOLTS: return {0.0, 0.078};
case UNIPT05VOLTS: return {0.0, 0.05};
case UNIPT01VOLTS: return {0.0, 0.01};
case UNIPT005VOLTS: return {0.0, 0.005};
case MA0TO20: return {0.0, 20.0};
default: return {0.0, 0.0};
}
}
} // anonymous namespace
// ============================================================================
// Discovery
// ============================================================================
std::vector<DiscoveredDevice> MCCDevice::discover() {
DaqDeviceDescriptor descriptors[MAX_DEV_COUNT];
unsigned int numDevs = MAX_DEV_COUNT;
UlError err = ulGetDaqDeviceInventory(ANY_IFC, descriptors, &numDevs);
if (err != ERR_NO_ERROR) {
return {};
}
std::vector<DiscoveredDevice> result;
result.reserve(numDevs);
for (unsigned int i = 0; i < numDevs; ++i) {
std::string ifc;
switch (descriptors[i].devInterface) {
case USB_IFC: ifc = "USB"; break;
case BLUETOOTH_IFC: ifc = "Bluetooth"; break;
case ETHERNET_IFC: ifc = "Ethernet"; break;
default: ifc = "Unknown"; break;
}
result.push_back({
.index = static_cast<int>(i),
.product_name = descriptors[i].productName,
.unique_id = descriptors[i].uniqueId,
.interface_name = ifc
});
}
return result;
}
// ============================================================================
// MCCDevice
// ============================================================================
MCCDevice::MCCDevice(const Config& config, StatusCallback callback)
: config_(config)
, statusCallback_(std::move(callback))
{
}
MCCDevice::~MCCDevice() {
disconnect();
}
bool MCCDevice::connect() {
if (connected_) return true;
disconnecting_ = false;
// --- Discover devices ---
DaqDeviceDescriptor descriptors[MAX_DEV_COUNT];
unsigned int numDevs = MAX_DEV_COUNT;
UlError err = ulGetDaqDeviceInventory(ANY_IFC, descriptors, &numDevs);
if (err != ERR_NO_ERROR) {
throw std::runtime_error("ulGetDaqDeviceInventory failed: " + ulErrorString(err));
}
if (numDevs == 0) {
throw std::runtime_error("No MCC DAQ devices found");
}
if (config_.device_index < 0 || config_.device_index >= static_cast<int>(numDevs)) {
throw std::runtime_error(
"Device index " + std::to_string(config_.device_index) +
" out of range (found " + std::to_string(numDevs) + " devices)");
}
auto& desc = descriptors[config_.device_index];
product_name_ = desc.productName;
unique_id_ = desc.uniqueId;
// --- Create and connect ---
handle_ = ulCreateDaqDevice(desc);
if (handle_ == 0) {
throw std::runtime_error("Failed to create DAQ device handle");
}
err = ulConnectDaqDevice(handle_);
if (err != ERR_NO_ERROR) {
ulReleaseDaqDevice(handle_);
handle_ = 0;
throw std::runtime_error("ulConnectDaqDevice failed: " + ulErrorString(err));
}
// --- Query device capabilities ---
long long numChans = 0;
long long hasPacer = 0;
// Try single-ended first, then differential
err = ulAIGetInfo(handle_, AI_INFO_NUM_CHANS_BY_MODE, AI_SINGLE_ENDED, &numChans);
if (err == ERR_NO_ERROR && numChans > 0) {
input_mode_ = AI_SINGLE_ENDED;
capabilities_.input_mode_name = "Single-Ended";
} else {
err = ulAIGetInfo(handle_, AI_INFO_NUM_CHANS_BY_MODE, AI_DIFFERENTIAL, &numChans);
if (err == ERR_NO_ERROR && numChans > 0) {
input_mode_ = AI_DIFFERENTIAL;
capabilities_.input_mode_name = "Differential";
} else {
disconnect();
throw std::runtime_error("Device has no supported analog input mode");
}
}
capabilities_.max_channels = static_cast<int>(numChans);
// Clamp high_channel to available channels
if (config_.high_channel >= static_cast<int>(numChans)) {
config_.high_channel = static_cast<int>(numChans) - 1;
}
// Verify hardware pacer
ulAIGetInfo(handle_, AI_INFO_HAS_PACER, 0, &hasPacer);
if (!hasPacer) {
disconnect();
throw std::runtime_error("Device does not support hardware-paced analog input");
}
// Query resolution
long long resolution = 0;
err = ulAIGetInfo(handle_, AI_INFO_RESOLUTION, 0, &resolution);
if (err == ERR_NO_ERROR) {
capabilities_.resolution_bits = static_cast<int>(resolution);
}
// Query scan rate limits
double minRate = 0.0, maxRate = 0.0;
err = ulAIGetInfoDbl(handle_, AI_INFO_MIN_SCAN_RATE, 0, &minRate);
if (err == ERR_NO_ERROR) {
capabilities_.min_scan_rate = minRate;
}
err = ulAIGetInfoDbl(handle_, AI_INFO_MAX_SCAN_RATE, 0, &maxRate);
if (err == ERR_NO_ERROR) {
capabilities_.max_scan_rate = maxRate;
}
// Query ALL supported ranges for the detected input mode
long long numRanges = 0;
AiInfoItem rangeCountItem = (input_mode_ == AI_SINGLE_ENDED)
? AI_INFO_NUM_SE_RANGES : AI_INFO_NUM_DIFF_RANGES;
AiInfoItem rangeItem = (input_mode_ == AI_SINGLE_ENDED)
? AI_INFO_SE_RANGE : AI_INFO_DIFF_RANGE;
ulAIGetInfo(handle_, rangeCountItem, 0, &numRanges);
if (numRanges <= 0) {
disconnect();
throw std::runtime_error("Device reports no supported voltage ranges");
}
capabilities_.available_ranges.clear();
for (long long i = 0; i < numRanges; ++i) {
long long rangeVal = 0;
err = ulAIGetInfo(handle_, rangeItem, static_cast<unsigned int>(i), &rangeVal);
if (err == ERR_NO_ERROR) {
capabilities_.available_ranges.push_back({
.id = static_cast<int>(rangeVal),
.label = rangeToString(static_cast<int>(rangeVal))
});
}
}
// Select range: user-specified or first available
if (config_.range >= 0) {
// Validate that the requested range is available
bool found = false;
for (const auto& r : capabilities_.available_ranges) {
if (r.id == config_.range) {
found = true;
break;
}
}
if (!found) {
disconnect();
throw std::runtime_error(
"Requested range " + rangeToString(config_.range) +
" is not supported by this device");
}
range_ = config_.range;
} else if (!capabilities_.available_ranges.empty()) {
range_ = capabilities_.available_ranges.front().id;
}
connected_ = true;
return true;
}
bool MCCDevice::startAcquisition() {
if (!connected_ || handle_ == 0) {
throw std::runtime_error("Device not connected");
}
if (scanning_) return true;
// Validate sample rate against device limits
if (capabilities_.min_scan_rate > 0 && config_.sample_rate < capabilities_.min_scan_rate) {
throw std::runtime_error(
"Sample rate " + std::to_string(config_.sample_rate) +
" Hz is below device minimum of " + std::to_string(capabilities_.min_scan_rate) + " Hz");
}
if (capabilities_.max_scan_rate > 0 && config_.sample_rate > capabilities_.max_scan_rate) {
throw std::runtime_error(
"Sample rate " + std::to_string(config_.sample_rate) +
" Hz exceeds device maximum of " + std::to_string(capabilities_.max_scan_rate) + " Hz");
}
// Allocate scan buffer and start acquisition
int channelCount = config_.high_channel - config_.low_channel + 1;
scan_buffer_samples_per_chan_ = static_cast<int>(config_.sample_rate * SCAN_BUFFER_SECONDS);
scan_buffer_.resize(
static_cast<size_t>(channelCount) * scan_buffer_samples_per_chan_, 0.0);
actual_rate_ = config_.sample_rate;
ScanOption options = static_cast<ScanOption>(SO_DEFAULTIO | SO_CONTINUOUS);
AInScanFlag scan_flags = AINSCAN_FF_DEFAULT;
if (!config_.scaled) {
scan_flags = static_cast<AInScanFlag>(
AINSCAN_FF_NOSCALEDATA | AINSCAN_FF_NOCALIBRATEDATA);
}
UlError err = ulAInScan(
handle_,
config_.low_channel,
config_.high_channel,
static_cast<AiInputMode>(input_mode_),
static_cast<Range>(range_),
scan_buffer_samples_per_chan_,
&actual_rate_,
options,
scan_flags,
scan_buffer_.data()
);
if (err != ERR_NO_ERROR) {
scan_buffer_.clear();
throw std::runtime_error("ulAInScan failed: " + ulErrorString(err));
}
scans_read_ = 0;
overrun_count_ = 0;
scanning_ = true;
return true;
}
bool MCCDevice::restartScan() {
// Stop the current (failed) scan
ulAInScanStop(handle_);
// Brief pause to let USB transfers settle
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// Restart the scan into the same buffer
actual_rate_ = config_.sample_rate;
ScanOption options = static_cast<ScanOption>(SO_DEFAULTIO | SO_CONTINUOUS);
AInScanFlag scan_flags = AINSCAN_FF_DEFAULT;
if (!config_.scaled) {
scan_flags = static_cast<AInScanFlag>(
AINSCAN_FF_NOSCALEDATA | AINSCAN_FF_NOCALIBRATEDATA);
}
UlError err = ulAInScan(
handle_,
config_.low_channel,
config_.high_channel,
static_cast<AiInputMode>(input_mode_),
static_cast<Range>(range_),
scan_buffer_samples_per_chan_,
&actual_rate_,
options,
scan_flags,
scan_buffer_.data()
);
if (err != ERR_NO_ERROR) {
return false;
}
scans_read_ = 0;
overrun_count_++;
if (statusCallback_) {
statusCallback_(
"Device FIFO overrun detected, scan restarted (overrun #" +
std::to_string(overrun_count_) + ")", true);
}
return true;
}
void MCCDevice::disconnect() {
disconnecting_ = true;
if (handle_ != 0) {
if (scanning_) {
ulAInScanStop(handle_);
scanning_ = false;
}
ulDisconnectDaqDevice(handle_);
ulReleaseDaqDevice(handle_);
handle_ = 0;
}
scan_buffer_.clear();
connected_ = false;
}
bool MCCDevice::isConnected() const {
return connected_;
}
DeviceInfo MCCDevice::getInfo() const {
int channelCount = config_.high_channel - config_.low_channel + 1;
auto [rmin, rmax] = rangeToVoltage(range_);
return {
.name = config_.stream_name,
.type = config_.stream_type,
.channel_count = channelCount,
.sample_rate = actual_rate_ > 0 ? actual_rate_ : config_.sample_rate,
.source_id = product_name_ + "_" + unique_id_,
.resolution_bits = capabilities_.resolution_bits,
.scaled = config_.scaled,
.range_min = rmin,
.range_max = rmax,
.range_label = rangeToString(range_)
};
}
DeviceCapabilities MCCDevice::getCapabilities() const {
return capabilities_;
}
bool MCCDevice::getData(std::vector<float>& buffer, double& timestamp) {
if (!connected_ || handle_ == 0) return false;
const int channelCount = config_.high_channel - config_.low_channel + 1;
const size_t total_buffer_elements = scan_buffer_.size();
while (!disconnecting_) {
ScanStatus status{};
TransferStatus xfer{};
UlError err = ulAInScanStatus(handle_, &status, &xfer);
if (err != ERR_NO_ERROR || status != SS_RUNNING) {
if (disconnecting_) return false;
if (!restartScan()) return false;
continue;
}
long long available = static_cast<long long>(xfer.currentScanCount) -
static_cast<long long>(scans_read_);
if (available > 0) {
timestamp = lsl::local_clock();
size_t num_elements = static_cast<size_t>(available) * channelCount;
buffer.resize(num_elements);
size_t read_offset =
(static_cast<size_t>(scans_read_) * channelCount) % total_buffer_elements;
for (size_t i = 0; i < num_elements; ++i) {
buffer[i] = static_cast<float>(
scan_buffer_[(read_offset + i) % total_buffer_elements]);
}
scans_read_ += available;
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return false;
}
bool MCCDevice::getDataInt32(std::vector<int32_t>& buffer, double& timestamp) {
if (!connected_ || handle_ == 0) return false;
const int channelCount = config_.high_channel - config_.low_channel + 1;
const size_t total_buffer_elements = scan_buffer_.size();
while (!disconnecting_) {
ScanStatus status{};
TransferStatus xfer{};
UlError err = ulAInScanStatus(handle_, &status, &xfer);
if (err != ERR_NO_ERROR || status != SS_RUNNING) {
if (disconnecting_) return false;
if (!restartScan()) return false;
continue;
}
long long available = static_cast<long long>(xfer.currentScanCount) -
static_cast<long long>(scans_read_);
if (available > 0) {
timestamp = lsl::local_clock();
size_t num_elements = static_cast<size_t>(available) * channelCount;
buffer.resize(num_elements);
size_t read_offset =
(static_cast<size_t>(scans_read_) * channelCount) % total_buffer_elements;
const int64_t mid = 1LL << (capabilities_.resolution_bits - 1);
for (size_t i = 0; i < num_elements; ++i) {
auto raw = static_cast<int64_t>(
scan_buffer_[(read_offset + i) % total_buffer_elements]);
buffer[i] = static_cast<int32_t>(raw - mid);
}
scans_read_ += available;
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return false;
}
bool MCCDevice::getDataInt16(std::vector<int16_t>& buffer, double& timestamp) {
if (!connected_ || handle_ == 0) return false;
const int channelCount = config_.high_channel - config_.low_channel + 1;
const size_t total_buffer_elements = scan_buffer_.size();
while (!disconnecting_) {
ScanStatus status{};
TransferStatus xfer{};
UlError err = ulAInScanStatus(handle_, &status, &xfer);
if (err != ERR_NO_ERROR || status != SS_RUNNING) {
if (disconnecting_) return false;
if (!restartScan()) return false;
continue;
}
long long available = static_cast<long long>(xfer.currentScanCount) -
static_cast<long long>(scans_read_);
if (available > 0) {
timestamp = lsl::local_clock();
size_t num_elements = static_cast<size_t>(available) * channelCount;
buffer.resize(num_elements);
size_t read_offset =
(static_cast<size_t>(scans_read_) * channelCount) % total_buffer_elements;
const int32_t mid = 1 << (capabilities_.resolution_bits - 1);
for (size_t i = 0; i < num_elements; ++i) {
auto raw = static_cast<int32_t>(
scan_buffer_[(read_offset + i) % total_buffer_elements]);
buffer[i] = static_cast<int16_t>(raw - mid);
}
scans_read_ += available;
return true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return false;
}
} // namespace mccoutlet