Skip to content

Commit b19f682

Browse files
committed
MAINT: convert rest of sensor plugins
1 parent 18f0d33 commit b19f682

File tree

13 files changed

+112
-40
lines changed

13 files changed

+112
-40
lines changed

src/applications/mne_scan/plugins/brainamp/brainamp.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ BrainAMP::~BrainAMP()
100100
//std::cout << "BrainAMP::~BrainAMP() " << std::endl;
101101

102102
//If the program is closed while the sampling is in process
103-
if(this->isRunning()) {
103+
if(m_bProcessOutput) {
104104
this->stop();
105105
}
106106
}
@@ -279,7 +279,8 @@ bool BrainAMP::start()
279279
m_iSamplingFreq);
280280

281281
if(m_pBrainAMPProducer->isRunning()) {
282-
QThread::start();
282+
m_bProcessOutput = true;
283+
m_OutputProcessingThread = std::thread(&BrainAMP::run, this);
283284
return true;
284285
} else {
285286
qWarning() << "BrainAMP::start() - BrainAMPProducer thread could not be started." << endl;
@@ -291,8 +292,11 @@ bool BrainAMP::start()
291292

292293
bool BrainAMP::stop()
293294
{
294-
requestInterruption();
295-
wait(500);
295+
m_bProcessOutput = false;
296+
297+
if(m_OutputProcessingThread.joinable()){
298+
m_OutputProcessingThread.join();
299+
}
296300

297301
//Stop the producer thread first
298302
m_pBrainAMPProducer->stop();
@@ -373,7 +377,7 @@ void BrainAMP::run()
373377
{
374378
MatrixXd matData;
375379

376-
while(!isInterruptionRequested()) {
380+
while(m_bProcessOutput) {
377381
if(m_pBrainAMPProducer->isRunning()) {
378382
//pop matrix
379383
if(m_pCircularBuffer->pop(matData)) {

src/applications/mne_scan/plugins/brainflowboard/brainflowboard.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ BrainFlowBoard::BrainFlowBoard()
6969
, m_sStreamerParams("")
7070
, m_uiSamplesPerBlock(100)
7171
, m_pFiffInfo(QSharedPointer<FiffInfo>::create())
72+
, m_bProcessOutput(false)
7273
{
7374
m_pShowSettingsAction = new QAction(QIcon(":/images/options.png"), tr("Streaming Settings"),this);
7475
m_pShowSettingsAction->setStatusTip(tr("Streaming Settings"));
@@ -244,7 +245,8 @@ bool BrainFlowBoard::start()
244245
msgBox.exec();
245246
return false;
246247
}
247-
QThread::start();
248+
m_bProcessOutput = true;
249+
m_OutputProcessingThread = std::thread(&BrainFlowBoard::run, this);
248250
return true;
249251
}
250252

@@ -256,8 +258,11 @@ bool BrainFlowBoard::stop()
256258
if(m_pBoardShim) {
257259
m_pBoardShim->stop_stream();
258260
}
259-
requestInterruption();
260-
wait(500);
261+
m_bProcessOutput = false;
262+
263+
if(m_OutputProcessingThread.joinable()){
264+
m_OutputProcessingThread.join();
265+
}
261266
m_pOutput->measurementData()->clear();
262267
} catch (const BrainFlowException &err) {
263268
BoardShim::log_message((int)LogLevels::LEVEL_ERROR, err.what());
@@ -388,12 +393,12 @@ void BrainFlowBoard::run()
388393
BrainFlowArray<double, 2> data;
389394
QList<Eigen::VectorXd> lSampleBlockBuffer;
390395

391-
while(!isInterruptionRequested()) {
392-
usleep(lSamplingPeriod);
396+
while(m_bProcessOutput) {
397+
std::this_thread::sleep_for(std::chrono::microseconds(lSamplingPeriod));
393398
iSampleIterator = 0;
394399

395400
//get samples from device until the complete matrix is filled, i.e. the samples per block size is met
396-
while(iSampleIterator < m_uiSamplesPerBlock && !isInterruptionRequested()) {
401+
while(iSampleIterator < m_uiSamplesPerBlock && m_bProcessOutput) {
397402
//Get sample block from device
398403
data = m_pBoardShim->get_board_data ();
399404
if (data.get_size(1) == 0) {

src/applications/mne_scan/plugins/brainflowboard/brainflowboard.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747

4848
#include <scShared/Plugins/abstractsensor.h>
4949

50+
#include <thread>
51+
#include <atomic>
52+
53+
5054
//=============================================================================================================
5155
// QT INCLUDES
5256
//=============================================================================================================
@@ -128,6 +132,9 @@ class BRAINFLOWBOARD_EXPORT BrainFlowBoard : public SCSHAREDLIB::AbstractSensor
128132

129133
QSharedPointer<SCSHAREDLIB::PluginOutputData<SCMEASLIB::RealTimeMultiSampleArray> > m_pOutput;
130134
QSharedPointer<FIFFLIB::FiffInfo> m_pFiffInfo; /**< Fiff measurement info.*/
135+
136+
std::thread m_OutputProcessingThread;
137+
std::atomic_bool m_bProcessOutput;
131138
};
132139
}
133140

src/applications/mne_scan/plugins/eegosports/eegosports.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ EEGoSports::EEGoSports()
9898
, m_sRPA("2RD")
9999
, m_sNasion("0Z")
100100
, m_pCircularBuffer(QSharedPointer<CircularBuffer_Matrix_double>(new CircularBuffer_Matrix_double(10)))
101+
, m_bProcessOutput(false)
101102
{
102103
}
103104

@@ -106,7 +107,7 @@ EEGoSports::EEGoSports()
106107
EEGoSports::~EEGoSports()
107108
{
108109
//If the program is closed while the sampling is in process
109-
if(this->isRunning()) {
110+
if(m_bProcessOutput) {
110111
this->stop();
111112
}
112113
}
@@ -564,7 +565,8 @@ bool EEGoSports::start()
564565
m_bCheckImpedances);
565566

566567
if(m_pEEGoSportsProducer->isRunning()) {
567-
QThread::start();
568+
m_bProcessOutput = true;
569+
m_OutputProcessingThread = std::thread(&EEGoSports::run, this);;
568570
return true;
569571
} else {
570572
qWarning() << "[EEGoSports::start] EEGoSports thread could not be started - Either the device is turned off (check your OS device manager) or the driver DLL (EEGO-SDK.dll) is not installed in one of the monitored dll path." << endl;
@@ -577,8 +579,11 @@ bool EEGoSports::start()
577579
bool EEGoSports::stop()
578580
{
579581
// Stop this (consumer) thread first
580-
requestInterruption();
581-
wait(500);
582+
m_bProcessOutput = false;
583+
584+
if(m_OutputProcessingThread.joinable()){
585+
m_OutputProcessingThread.join();
586+
}
582587

583588
//Stop the producer thread
584589
m_pEEGoSportsProducer->stop();
@@ -660,7 +665,7 @@ void EEGoSports::onUpdateCardinalPoints(const QString& sLPA, double dLPA, const
660665
void EEGoSports::showImpedanceDialog()
661666
{
662667
// Open Impedance dialog only if no sampling process is active
663-
if(!this->isRunning()) {
668+
if(!m_bProcessOutput) {
664669
if(m_pEEGoSportsImpedanceWidget == NULL) {
665670
m_pEEGoSportsImpedanceWidget = QSharedPointer<EEGoSportsImpedanceWidget>(new EEGoSportsImpedanceWidget(this));
666671
}
@@ -700,7 +705,7 @@ void EEGoSports::run()
700705
{
701706
MatrixXd matData;
702707

703-
while(!isInterruptionRequested()) {
708+
while(m_bProcessOutput) {
704709
if(m_pEEGoSportsProducer->isRunning()) {
705710
// Check impedances - send new impedance values to graphic scene
706711
if(m_bCheckImpedances) {

src/applications/mne_scan/plugins/eegosports/eegosports.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
#include <utils/generics/circularbuffer.h>
5454
#include <fstream>
5555

56+
#include <thread>
57+
#include <atomic>
58+
5659
//=============================================================================================================
5760
// EIGEN INCLUDES
5861
//=============================================================================================================
@@ -260,6 +263,9 @@ protected slots:
260263
QSharedPointer<EEGoSportsProducer> m_pEEGoSportsProducer; /**< The EEGoSportsProducer.*/
261264

262265
QMutex m_mutex; /**< Holds the threads mutex.*/
266+
267+
std::thread m_OutputProcessingThread;
268+
std::atomic_bool m_bProcessOutput;
263269
};
264270
} // NAMESPACE
265271

src/applications/mne_scan/plugins/ftbuffer/ftbuffer.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ FtBuffer::FtBuffer()
7070
, m_pCircularBuffer(QSharedPointer<CircularBuffer_Matrix_double>(new CircularBuffer_Matrix_double(10)))
7171
, m_sBufferAddress("127.0.0.1")
7272
, m_iBufferPort(1972)
73+
, m_bProcessOutput(false)
7374
{
7475
}
7576

@@ -133,7 +134,8 @@ bool FtBuffer::start()
133134
qInfo() << "[FtBuffer::start] Producer thread created, sending work command...";
134135
emit workCommand();
135136

136-
QThread::start();
137+
m_bProcessOutput = true;
138+
m_OutputProcessingThread = std::thread(&FtBuffer::run, this);
137139

138140
return true;
139141
}
@@ -146,14 +148,17 @@ bool FtBuffer::stop()
146148

147149
m_bIsConfigured = false;
148150

149-
requestInterruption();
150-
wait(500);
151+
m_bProcessOutput = false;
152+
153+
if(m_OutputProcessingThread.joinable()){
154+
m_OutputProcessingThread.join();
155+
}
151156

152157
//stops separate producer/client thread first
153158
m_pProducerThread.requestInterruption();
154159
int itries = 0;
155160
while(m_pProducerThread.isRunning()) {
156-
msleep(10);
161+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
157162
if(itries > 10){
158163
break;
159164
}
@@ -200,12 +205,12 @@ void FtBuffer::run()
200205
{
201206
MatrixXd matData;
202207

203-
while(!isInterruptionRequested()) {
208+
while(m_bProcessOutput) {
204209
//qDebug() << "[FtBuffer::run] m_pFiffInfo->dig.size()" << m_pFiffInfo->dig.size();
205210
//pop matrix
206211
if(m_pCircularBuffer->pop(matData)) {
207212
//emit values
208-
if(!isInterruptionRequested()) {
213+
if(m_bProcessOutput) {
209214
m_pRTMSA_BufferOutput->measurementData()->setValue(matData);
210215
}
211216
}

src/applications/mne_scan/plugins/ftbuffer/ftbuffer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555

5656
#include <utils/generics/circularbuffer.h>
5757

58+
#include <thread>
59+
#include <atomic>
60+
5861
//=============================================================================================================
5962
// QT INCLUDES
6063
//=============================================================================================================
@@ -253,6 +256,9 @@ class FTBUFFER_EXPORT FtBuffer : public SCSHAREDLIB::AbstractSensor
253256

254257
QString m_sBufferAddress; /**< The address used to connect to the buffer if starting without being connected */
255258
int m_iBufferPort; /**< The port used to connect to the buffer if starting without being connected */
259+
260+
std::thread m_OutputProcessingThread;
261+
std::atomic_bool m_bProcessOutput;
256262
};
257263
}//namespace end brace
258264

src/applications/mne_scan/plugins/gusbamp/gusbamp.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ GUSBAmp::GUSBAmp()
7272
, m_iSamplesPerBlock(0)
7373
, m_iSampleRate(128)
7474
, m_pCircularBuffer(QSharedPointer<CircularBuffer_Matrix_float>(new CircularBuffer_Matrix_float(8)))
75+
, m_bProcessOutput(false)
7576
{
7677
m_viChannelsToAcquire = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
7778

@@ -86,7 +87,7 @@ GUSBAmp::GUSBAmp()
8687
GUSBAmp::~GUSBAmp()
8788
{
8889
//If the program is closed while the sampling is in process
89-
if(this->isRunning()){
90+
if(m_bProcessOutput){
9091
this->stop();
9192
}
9293
}
@@ -212,7 +213,8 @@ bool GUSBAmp::start()
212213

213214
//start the thread for ring buffer
214215
if(m_pGUSBAmpProducer->isRunning()) {
215-
QThread::start();
216+
m_bProcessOutput = true;
217+
m_OutputProcessingThread = std::thread(&GUSBAmp::run, this);
216218
return true;
217219
} else {
218220
qWarning() << "Plugin GUSBAmp - ERROR - GUSBAmpProducer thread could not be started - Either the device is turned off (check your OS device manager) or the driver DLL (GUSBAmpSDK.dll / GUSBAmpSDK32bit.dll) is not installed in the system32 / SysWOW64 directory" << endl;
@@ -225,8 +227,11 @@ bool GUSBAmp::start()
225227
bool GUSBAmp::stop()
226228
{
227229
// Stop this (consumer) thread first
228-
requestInterruption();
229-
wait(500);
230+
m_bProcessOutput = false;
231+
232+
if(m_OutputProcessingThread.joinable()){
233+
m_OutputProcessingThread.join();
234+
}
230235

231236
//Stop the producer thread first
232237
m_pGUSBAmpProducer->stop();
@@ -271,7 +276,7 @@ void GUSBAmp::run()
271276
qint32 size = 0;
272277
MatrixXf matValue;
273278

274-
while(!isInterruptionRequested()) {
279+
while(m_bProcessOutput) {
275280
//pop matrix only if the producer thread is running
276281
if(m_pGUSBAmpProducer->isRunning()) {
277282
//pop matrix

src/applications/mne_scan/plugins/gusbamp/gusbamp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#include "FormFiles/gusbampsetupwidget.h"
5151
#include "FormFiles/gusbampsetupprojectwidget.h"
5252

53+
#include <thread>
54+
#include <atomic>
55+
5356
//=============================================================================================================
5457
// QT INCLUDES
5558
//=============================================================================================================
@@ -211,6 +214,9 @@ class GUSBAMPSHARED_EXPORT GUSBAmp : public SCSHAREDLIB::AbstractSensor
211214
std::vector<int> m_viSizeOfSampleMatrix; /**< vector including the size of the two dimensional sample Matrix. */
212215
std::vector<int> m_viChannelsToAcquire; /**< vector of the calling numbers of the channels to be acquired. */
213216
Eigen::RowVectorXd m_cals;
217+
218+
std::thread m_OutputProcessingThread;
219+
std::atomic_bool m_bProcessOutput;
214220
};
215221
} // NAMESPACE
216222

src/applications/mne_scan/plugins/natus/natus.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Natus::Natus()
7575
, m_qStringResourcePath(qApp->applicationDirPath()+"/../resources/mne_scan/plugins/natus/")
7676
, m_pRMTSA_Natus(PluginOutputData<RealTimeMultiSampleArray>::create(this, "Natus", "EEG output data"))
7777
, m_pFiffInfo(QSharedPointer<FiffInfo>::create())
78+
, m_bProcessOutput(false)
7879
{
7980
m_pRMTSA_Natus->measurementData()->setName(this->getName());//Provide name to auto store widget settings
8081
}
@@ -84,7 +85,7 @@ Natus::Natus()
8485
Natus::~Natus()
8586
{
8687
//If the program is closed while the sampling is in process
87-
if(this->isRunning()) {
88+
if(m_bProcessOutput) {
8889
this->stop();
8990
}
9091
}
@@ -229,7 +230,8 @@ bool Natus::start()
229230
m_pRMTSA_Natus->measurementData()->initFromFiffInfo(m_pFiffInfo);
230231
m_pRMTSA_Natus->measurementData()->setMultiArraySize(1);
231232

232-
QThread::start();
233+
m_bProcessOutput = true;
234+
m_OutputProcessingThread = std::thread(&Natus::run, this);
233235

234236
// Start the producer
235237
m_pNatusProducer = QSharedPointer<NatusProducer>::create(m_iSamplesPerBlock, m_iNumberChannels);
@@ -245,8 +247,11 @@ bool Natus::start()
245247

246248
bool Natus::stop()
247249
{
248-
requestInterruption();
249-
wait(500);
250+
m_bProcessOutput = false;
251+
252+
if(m_OutputProcessingThread.joinable()){
253+
m_OutputProcessingThread.join();
254+
}
250255

251256
// Clear all data in the buffer connected to displays and other plugins
252257
m_pRMTSA_Natus->measurementData()->clear();
@@ -299,10 +304,10 @@ void Natus::run()
299304
{
300305
MatrixXd matData;
301306

302-
while(!isInterruptionRequested()) {
307+
while(m_bProcessOutput) {
303308
if(m_pCircularBuffer->pop(matData)) {
304309
//emit values
305-
if(!isInterruptionRequested()) {
310+
if(m_bProcessOutput) {
306311
m_pRMTSA_Natus->measurementData()->setValue(matData);
307312
}
308313
}

0 commit comments

Comments
 (0)