MNE-CPP  beta 1.0
neuromag.cpp
1 //=============================================================================================================
37 //*************************************************************************************************************
38 //=============================================================================================================
39 // INCLUDES
40 //=============================================================================================================
41 
42 #include "neuromag.h"
43 #include "dacqserver.h"
44 
45 
46 //*************************************************************************************************************
47 //=============================================================================================================
48 // FIFF INCLUDES
49 //=============================================================================================================
50 
51 #include <fiff/fiff.h>
52 #include <fiff/fiff_types.h>
53 
54 
55 //*************************************************************************************************************
56 //=============================================================================================================
57 // MNE INCLUDES
58 //=============================================================================================================
59 
60 #include <mne/mne.h>
62 
63 
64 //*************************************************************************************************************
65 //=============================================================================================================
66 // QT INCLUDES
67 //=============================================================================================================
68 
69 #include <QtCore/QtPlugin>
70 #include <QFile>
71 #include <QDebug>
72 
73 
74 //*************************************************************************************************************
75 //=============================================================================================================
76 // USED NAMESPACES
77 //=============================================================================================================
78 
79 using namespace NeuromagPlugin;
80 using namespace FIFFLIB;
81 using namespace MNELIB;
82 
83 
84 //*************************************************************************************************************
85 //=============================================================================================================
86 // DEFINE MEMBER METHODS
87 //=============================================================================================================
88 
90 : m_pDacqServer(new DacqServer(this))
91 , m_iID(-1)
92 , m_uiBufferSampleSize(100)
93 , m_pRawMatrixBuffer(NULL)
94 , m_bIsRunning(false)
95 {
96  this->init();
97 }
98 
99 
100 //*************************************************************************************************************
101 
103 {
104  qDebug() << "Destroy Neuromag::~Neuromag()";
105 
106  delete m_pDacqServer;
107 
108  m_bIsRunning = false;
109  QThread::wait();
110 }
111 
112 
113 //*************************************************************************************************************
114 
115 void Neuromag::comBufsize(Command p_command)
116 {
117  quint32 t_uiBuffSize = p_command.pValues()[0].toUInt();
118 
119  if(t_uiBuffSize > 0)
120  {
121  qDebug() << "void Neuromag::setBufferSize: " << t_uiBuffSize;
122 
123  bool t_bWasRunning = m_bIsRunning;
124 
125  if(m_bIsRunning)
126  this->stop();
127 
128  m_uiBufferSampleSize = t_uiBuffSize;
129 
130  if(t_bWasRunning)
131  this->start();
132 
133  QString str = QString("\tSet %1 buffer sample size to %2 samples\r\n\n").arg(getName()).arg(t_uiBuffSize);
134 
135  m_commandManager["bufsize"].reply(str);
136  }
137  else
138  m_commandManager["bufsize"].reply("Buffer size not set\r\n");
139 }
140 
141 
142 //*************************************************************************************************************
143 
144 void Neuromag::comGetBufsize(Command p_command)
145 {
146  bool t_bCommandIsJson = p_command.isJson();
147  if(t_bCommandIsJson)
148  {
149  //
150  //create JSON help object
151  //
152  QJsonObject t_qJsonObjectRoot;
153  t_qJsonObjectRoot.insert("bufsize", QJsonValue((double)m_uiBufferSampleSize));
154  QJsonDocument p_qJsonDocument(t_qJsonObjectRoot);
155 
156  m_commandManager["getbufsize"].reply(p_qJsonDocument.toJson());
157  }
158  else
159  {
160  QString str = QString("\t%1\r\n\n").arg(m_uiBufferSampleSize);
161  m_commandManager["getbufsize"].reply(str);
162  }
163 }
164 
165 
166 //*************************************************************************************************************
167 
169 {
170  //Connect slots
171  QObject::connect(&m_commandManager["bufsize"], &Command::executed, this, &Neuromag::comBufsize);
172  QObject::connect(&m_commandManager["getbufsize"], &Command::executed, this, &Neuromag::comGetBufsize);
173 
174 }
175 
176 
177 //*************************************************************************************************************
178 
179 ConnectorID Neuromag::getConnectorID() const
180 {
181  return _NEUROMAG;
182 }
183 
184 
185 //*************************************************************************************************************
186 
187 const char* Neuromag::getName() const
188 {
189  return "Neuromag Connector";
190 }
191 
192 
193 //*************************************************************************************************************
194 
195 void Neuromag::init()
196 {
197 
198 }
199 
200 
201 //*************************************************************************************************************
202 
203 void Neuromag::info(qint32 ID)
204 {
205  m_iID = ID;
206 
207  if(!m_info.isEmpty())
208  releaseMeasInfo();
209  else
210  {
211  m_pDacqServer->m_bMeasInfoRequest = true;
212 
213  //This should never happen
214  if(m_pDacqServer->isRunning())
215  {
216  m_pDacqServer->m_bIsRunning = false;
217  m_pDacqServer->wait();
218  m_pDacqServer->start();
219  }
220  //
221  else
222  {
223  m_pDacqServer->start();
224 // m_pDacqServer->wait();// until header reading finished
225  }
226  }
227 }
228 
229 
230 //*************************************************************************************************************
231 
232 void Neuromag::releaseMeasInfo()
233 {
234  if(!m_info.isEmpty())
235  emit remitMeasInfo(m_iID, m_info);
236 }
237 
238 //*************************************************************************************************************
239 
241 {
242  qDebug() << "bool Neuromag::start()";
243 
244  m_pDacqServer->m_bMeasRequest = true;
245 
246  // Start thread
247  m_pDacqServer->start();
248 
249  QThread::start();
250 
251  return true;
252 }
253 
254 
255 //*************************************************************************************************************
256 
258 {
259  m_bIsRunning = false;
260  QThread::wait();//ToDo: This thread will never be terminated when circular buffer is blocking the thread (happens when circularbuffer is empty)
261 
262  m_pDacqServer->m_bIsRunning = false;
263  m_pDacqServer->wait();
264 
265  qDebug() << "bool Neuromag::stop()";
266 
267  return true;
268 }
269 
270 
271 //*************************************************************************************************************
272 
274 {
275  m_bIsRunning = true;
276 
277  //qint32 count = 0;
278 
279  while(m_bIsRunning)
280  {
281  if(m_pRawMatrixBuffer)
282  {
283  // Pop available Buffers
284  QSharedPointer<Eigen::MatrixXf> t_pRawBuffer(new Eigen::MatrixXf(m_pRawMatrixBuffer->pop()));
285 // ++count;
286 // printf("%d raw buffer (%d x %d) generated\r\n", count, t_pRawBuffer->rows(), t_pRawBuffer->cols());
287 
288  emit remitRawBuffer(t_pRawBuffer);
289  }
290  }
291 }
virtual void info(qint32 ID)
Definition: neuromag.cpp:203
Old fiff_type declarations - replace them.
virtual void connectCommandManager()
Definition: neuromag.cpp:168
virtual const char * getName() const
Definition: neuromag.cpp:187
MNEEpochDataList class declaration.
declaration of the DacqServer Class.
virtual void run()
Definition: neuromag.cpp:273
CommandManager m_commandManager
Definition: IConnector.h:228
virtual bool start()
Definition: neuromag.cpp:240
virtual bool stop()
Definition: neuromag.cpp:257
QList< QVariant > & pValues()
Definition: command.h:370
Definition: fiff.h:98
void executed(Command p_command)
The DacqServer class provides a Neuromag MEG connector.
Definition: dacqserver.h:96
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...
virtual ConnectorID getConnectorID() const
Definition: neuromag.cpp:179