11#include " mccoutlet/Device.hpp"
22
3+ #include < lsl_cpp.h>
34#include < uldaq.h>
45
56#include < chrono>
@@ -463,11 +464,10 @@ DeviceCapabilities MCCDevice::getCapabilities() const {
463464 return capabilities_;
464465}
465466
466- bool MCCDevice::getData (std::vector<float >& buffer) {
467+ bool MCCDevice::getData (std::vector<float >& buffer, double & timestamp ) {
467468 if (!connected_ || handle_ == 0 ) return false ;
468469
469470 const int channelCount = config_.high_channel - config_.low_channel + 1 ;
470- const int samples_needed = static_cast <int >(buffer.size ()) / channelCount;
471471 const size_t total_buffer_elements = scan_buffer_.size ();
472472
473473 while (!disconnecting_) {
@@ -477,42 +477,41 @@ bool MCCDevice::getData(std::vector<float>& buffer) {
477477 UlError err = ulAInScanStatus (handle_, &status, &xfer);
478478 if (err != ERR_NO_ERROR || status != SS_RUNNING) {
479479 if (disconnecting_) return false ;
480- // Overrun or other scan failure — attempt restart
481480 if (!restartScan ()) return false ;
482481 continue ;
483482 }
484483
485- // currentScanCount = per-channel samples transferred since scan start
486484 long long available = static_cast <long long >(xfer.currentScanCount ) -
487485 static_cast <long long >(scans_read_);
488486
489- if (available >= samples_needed) {
490- // Calculate read position in the circular buffer
487+ if (available > 0 ) {
488+ timestamp = lsl::local_clock ();
489+
490+ size_t num_elements = static_cast <size_t >(available) * channelCount;
491+ buffer.resize (num_elements);
492+
491493 size_t read_offset =
492494 (static_cast <size_t >(scans_read_) * channelCount) % total_buffer_elements;
493495
494- // Copy data, converting double -> float, handling wrap-around
495- for (size_t i = 0 ; i < buffer.size (); ++i) {
496+ for (size_t i = 0 ; i < num_elements; ++i) {
496497 buffer[i] = static_cast <float >(
497498 scan_buffer_[(read_offset + i) % total_buffer_elements]);
498499 }
499500
500- scans_read_ += samples_needed ;
501+ scans_read_ += available ;
501502 return true ;
502503 }
503504
504- // Sleep briefly before polling again
505505 std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
506506 }
507507
508508 return false ;
509509}
510510
511- bool MCCDevice::getDataInt32 (std::vector<int32_t >& buffer) {
511+ bool MCCDevice::getDataInt32 (std::vector<int32_t >& buffer, double & timestamp ) {
512512 if (!connected_ || handle_ == 0 ) return false ;
513513
514514 const int channelCount = config_.high_channel - config_.low_channel + 1 ;
515- const int samples_needed = static_cast <int >(buffer.size ()) / channelCount;
516515 const size_t total_buffer_elements = scan_buffer_.size ();
517516
518517 while (!disconnecting_) {
@@ -529,16 +528,21 @@ bool MCCDevice::getDataInt32(std::vector<int32_t>& buffer) {
529528 long long available = static_cast <long long >(xfer.currentScanCount ) -
530529 static_cast <long long >(scans_read_);
531530
532- if (available >= samples_needed) {
531+ if (available > 0 ) {
532+ timestamp = lsl::local_clock ();
533+
534+ size_t num_elements = static_cast <size_t >(available) * channelCount;
535+ buffer.resize (num_elements);
536+
533537 size_t read_offset =
534538 (static_cast <size_t >(scans_read_) * channelCount) % total_buffer_elements;
535539
536- for (size_t i = 0 ; i < buffer. size () ; ++i) {
540+ for (size_t i = 0 ; i < num_elements ; ++i) {
537541 buffer[i] = static_cast <int32_t >(
538542 scan_buffer_[(read_offset + i) % total_buffer_elements]);
539543 }
540544
541- scans_read_ += samples_needed ;
545+ scans_read_ += available ;
542546 return true ;
543547 }
544548
@@ -548,11 +552,10 @@ bool MCCDevice::getDataInt32(std::vector<int32_t>& buffer) {
548552 return false ;
549553}
550554
551- bool MCCDevice::getDataInt16 (std::vector<int16_t >& buffer) {
555+ bool MCCDevice::getDataInt16 (std::vector<int16_t >& buffer, double & timestamp ) {
552556 if (!connected_ || handle_ == 0 ) return false ;
553557
554558 const int channelCount = config_.high_channel - config_.low_channel + 1 ;
555- const int samples_needed = static_cast <int >(buffer.size ()) / channelCount;
556559 const size_t total_buffer_elements = scan_buffer_.size ();
557560
558561 while (!disconnecting_) {
@@ -569,16 +572,21 @@ bool MCCDevice::getDataInt16(std::vector<int16_t>& buffer) {
569572 long long available = static_cast <long long >(xfer.currentScanCount ) -
570573 static_cast <long long >(scans_read_);
571574
572- if (available >= samples_needed) {
575+ if (available > 0 ) {
576+ timestamp = lsl::local_clock ();
577+
578+ size_t num_elements = static_cast <size_t >(available) * channelCount;
579+ buffer.resize (num_elements);
580+
573581 size_t read_offset =
574582 (static_cast <size_t >(scans_read_) * channelCount) % total_buffer_elements;
575583
576- for (size_t i = 0 ; i < buffer. size () ; ++i) {
584+ for (size_t i = 0 ; i < num_elements ; ++i) {
577585 buffer[i] = static_cast <int16_t >(
578586 scan_buffer_[(read_offset + i) % total_buffer_elements]);
579587 }
580588
581- scans_read_ += samples_needed ;
589+ scans_read_ += available ;
582590 return true ;
583591 }
584592
0 commit comments