MNE-CPP  beta 1.0
neuromagproducer.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "neuromagproducer.h"
42 #include "neuromag.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // USED NAMESPACES
48 //=============================================================================================================
49 
50 using namespace MneRtClientPlugin;
51 
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // QT INCLUDES
56 //=============================================================================================================
57 
59 : m_pNeuromag(p_pNeuromag)
60 , m_pRtDataClient(0)
61 , m_bDataClientIsConnected(false)
62 , m_iDataClientId(-1)
63 , m_bFlagInfoRequest(false)
64 , m_bFlagMeasuring(false)
65 , m_bIsRunning(false)
66 {
67 }
68 
69 
70 //*************************************************************************************************************
71 
73 {
74 
75 }
76 
77 
78 //*************************************************************************************************************
79 
80 void NeuromagProducer::connectDataClient(QString p_sRtSeverIP)
81 {
82  if(m_pRtDataClient.isNull())
83  m_pRtDataClient = QSharedPointer<RtDataClient>(new RtDataClient);
84  else if(m_bDataClientIsConnected)
85  return;
86 
87  m_pRtDataClient->connectToHost(p_sRtSeverIP);
88  m_pRtDataClient->waitForConnected(1000);
89 
90  if(m_pRtDataClient->state() == QTcpSocket::ConnectedState)
91  {
92  producerMutex.lock();
93  if(!m_bDataClientIsConnected)
94  {
95  //
96  // get client ID
97  //
98  m_iDataClientId = m_pRtDataClient->getClientId();
99 
100  //
101  // set data client alias -> for convinience (optional)
102  //
103  m_pRtDataClient->setClientAlias(m_pNeuromag->m_sNeuromagClientAlias); // used in option 2 later on
104 
105  //
106  // set new state
107  //
108  m_bDataClientIsConnected = true;
109  emit dataConnectionChanged(m_bDataClientIsConnected);
110  }
111  producerMutex.unlock();
112  }
113 }
114 
115 
116 //*************************************************************************************************************
117 
119 {
120  if(m_bDataClientIsConnected)
121  {
122  m_pRtDataClient->disconnectFromHost();
123  m_pRtDataClient->waitForDisconnected();
124  producerMutex.lock();
125  m_iDataClientId = -1;
126  m_bDataClientIsConnected = false;
127  producerMutex.unlock();
128  emit dataConnectionChanged(m_bDataClientIsConnected);
129  }
130 }
131 
132 
133 //*************************************************************************************************************
134 
136 {
137  //Wait until this thread (Producer) is stopped
138  m_bIsRunning = false;
139  m_bFlagMeasuring = false;
140 
141  if(m_pNeuromag->m_bCmdClientIsConnected) //ToDo replace this with is running
142  {
143  // Stop Measurement at rt_Server
144  (*m_pNeuromag->m_pRtCmdClient)["stop-all"].send();
145  }
146 
147  if(m_pNeuromag->m_pRawMatrixBuffer_In)
148  {
149  //In case the semaphore blocks the thread -> Release the QSemaphore and let it exit from the push function (acquire statement)
150  m_pNeuromag->m_pRawMatrixBuffer_In->releaseFromPush();
151  }
152 
153  this->disconnectDataClient();
154 
155  //Check if the thread is already or still running. This can happen if the start button is pressed immediately after the stop button was pressed. In this case the stopping process is not finished yet but the start process is initiated.
156  if(this->isRunning())
157  QThread::wait();
158 }
159 
160 
161 //*************************************************************************************************************
162 
164 {
165  m_bIsRunning = true;
166  //
167  // Connect data client
168  //
169  this->connectDataClient(m_pNeuromag->m_sNeuromagIP);
170 
171  qint32 count = 0;
172  while(m_pRtDataClient->state() != QTcpSocket::ConnectedState)
173  {
174  msleep(100);
175  this->connectDataClient(m_pNeuromag->m_sNeuromagIP);
176  ++count;
177  if(count > 10 || !m_bIsRunning)
178  return;
179  }
180 
181  msleep(1000);
182 
183  m_bFlagMeasuring = true;
184 
185  //
186  // Inits
187  //
188  MatrixXf t_matRawBuffer;
189 
190  fiff_int_t kind;
191 
192  qint32 from = 0;
193  qint32 to = -1;
194 
195  while(m_bIsRunning)
196  {
197  if(m_bFlagInfoRequest)
198  {
199  m_pNeuromag->rtServerMutex.lock();
200  m_pNeuromag->m_pFiffInfo = m_pRtDataClient->readInfo();
201  emit m_pNeuromag->fiffInfoAvailable();
202  m_pNeuromag->rtServerMutex.unlock();
203 
204  producerMutex.lock();
205  m_bFlagInfoRequest = false;
206  producerMutex.unlock();
207  }
208 
209  if(m_bFlagMeasuring)
210  {
211  m_pRtDataClient->readRawBuffer(m_pNeuromag->m_pFiffInfo->nchan, t_matRawBuffer, kind);
212 
213  if(kind == FIFF_DATA_BUFFER)
214  {
215  to += t_matRawBuffer.cols();
216  from += t_matRawBuffer.cols();
217 
218  m_pNeuromag->m_pRawMatrixBuffer_In->push(&t_matRawBuffer);
219  }
220  else if(FIFF_DATA_BUFFER == FIFF_BLOCK_END)
221  m_bFlagMeasuring = false;
222  }
223  }
224 }
Real-time data client.
Definition: rtdataclient.h:92
NeuromagProducer(Neuromag *p_pNeuromag)
void connectDataClient(QString p_sRtSeverIP)
The Neuromag class provides a RT server connection.
Definition: neuromag.h:115
Contains the declaration of the NeuromagProducer class.
void dataConnectionChanged(bool p_bStatus)
#define FIFF_DATA_BUFFER