60 : m_pTMSIProducer(pTMSIProducer)
62 , m_bInitDeviceSuccess(false)
63 , m_uiNumberOfChannels(138)
64 , m_uiSamplingFrequency(1024)
65 , m_uiSamplesPerBlock(1)
66 , m_bUseChExponent(false)
67 , m_bUseUnitGain(false)
68 , m_bUseUnitOffset(false)
69 , m_bWriteDriverDebugToFile(false)
70 , m_sOutputFilePath(
"/mne_x_plugins/resources/tmsi")
71 , m_bUseCommonAverage(false)
72 , m_bMeasureImpedances(false)
76 m_HandleMaster = NULL;
77 m_PSPDPMasterDevicePath = NULL;
78 m_lSignalBuffer = NULL;
88 #ifdef TAKE_TMSISDK_DLL //32 bit system & 64 bit (with 64 bit compiler)
89 m_oLibHandle = ::LoadLibrary(L
"C:\\Windows\\System32\\TMSiSDK.dll");
90 #elif TAKE_TMSISDK_32_DLL //64 bit (with 32 bit compiler)
92 m_oLibHandle = ::LoadLibrary(L
"C:\\Windows\\SysWOW64\\TMSiSDK32bit.dll");
96 if( m_oLibHandle == NULL)
98 cout <<
"Plugin TMSI - ERROR - Couldn't load DLL - Check if the driver for the TMSi USB Fiber Connector installed in the system dir" << endl;
104 __load_dll_func__(m_oFpOpen, POPEN,
"Open");
105 __load_dll_func__(m_oFpClose, PCLOSE,
"Close");
106 __load_dll_func__(m_oFpStart, PSTART,
"Start");
107 __load_dll_func__(m_oFpStop, PSTOP,
"Stop");
108 __load_dll_func__(m_oFpGetSignalFormat, PGETSIGNALFORMAT,
"GetSignalFormat");
109 __load_dll_func__(m_oFpSetSignalBuffer, PSETSIGNALBUFFER,
"SetSignalBuffer");
110 __load_dll_func__(m_oFpGetSamples, PGETSAMPLES,
"GetSamples");
111 __load_dll_func__(m_oFpGetBufferInfo, PGETBUFFERINFO,
"GetBufferInfo");
112 __load_dll_func__(m_oFpFree, PFREE,
"Free");
113 __load_dll_func__(m_oFpLibraryInit, PLIBRARYINIT,
"LibraryInit");
114 __load_dll_func__(m_oFpLibraryExit, PLIBRARYEXIT,
"LibraryExit");
115 __load_dll_func__(m_oFpGetDeviceList, PGETDEVICELIST,
"GetDeviceList");
116 __load_dll_func__(m_oFpGetFrontEndInfo, PGETFRONTENDINFO,
"GetFrontEndInfo");
117 __load_dll_func__(m_oFpSetRefCalculation, PSETREFCALCULATION,
"SetRefCalculation");
118 __load_dll_func__(m_oFpSetMeasuringMode, PSETMEASURINGMODE,
"SetMeasuringMode");
119 __load_dll_func__(m_oFpGetErrorCode, PGETERRORCODE,
"GetErrorCode");
121 cout <<
"Plugin TMSI - INFO - TMSIDriver() - Successfully loaded all DLL functions" << endl;
136 int iSamplingFrequency,
137 int iSamplesPerBlock,
141 bool bWriteDriverDebugToFile,
142 QString sOutpuFilePath,
143 bool bUseCommonAverage,
144 bool bMeasureImpedance)
151 m_uiNumberOfChannels = iNumberOfChannels;
152 m_uiSamplingFrequency = iSamplingFrequency;
153 m_uiSamplesPerBlock = iSamplesPerBlock;
154 m_bUseChExponent = bUseChExponent;
155 m_bUseUnitGain = bUseUnitGain;
156 m_bUseUnitOffset = bUseUnitOffset;
157 m_bWriteDriverDebugToFile = bWriteDriverDebugToFile;
158 m_sOutputFilePath = sOutpuFilePath;
159 m_bUseCommonAverage = bUseCommonAverage;
160 m_bMeasureImpedances = bMeasureImpedance;
163 if(m_bWriteDriverDebugToFile)
164 m_outputFileStream.open(
"mne_x_plugins/resources/tmsi/TMSi_Driver_Debug.txt", ios::trunc);
173 m_HandleMaster = m_oFpLibraryInit(TMSiConnectionUSB, &ErrorCode);
177 cout <<
"Plugin TMSI - ERROR - initDevice() - Can not initialize library" << endl;
182 char **DeviceList = NULL;
185 DeviceList = m_oFpGetDeviceList(m_HandleMaster, &NrOfDevices);
187 if( NrOfDevices == 0 )
189 cout <<
"Plugin TMSI - ERROR - initDevice() - Frontend list NOT available - Maybe no devices are connected" << endl;
190 m_oFpLibraryExit(m_HandleMaster);
196 char *DeviceLocator = DeviceList[0] ;
197 Status = m_oFpOpen(m_HandleMaster, DeviceLocator);
200 m_oFpStop(m_HandleMaster);
204 cout <<
"Plugin TMSI - ERROR - initDevice() - Failed to open connected device" << endl;
205 m_oFpLibraryExit(m_HandleMaster);
210 ULONG impedanceMode = 3;
211 ULONG normalMode = 0;
213 if(m_bMeasureImpedances)
215 if(m_oFpSetMeasuringMode(m_HandleMaster, impedanceMode, 1))
216 cout <<
"Plugin TMSI - INFO - Now measuring impedances" << endl;
219 int ErrorCode = m_oFpGetErrorCode(m_HandleMaster);
220 cout <<
"Unable to set Measuremode impedance, errorcode = " << ErrorCode << endl;
224 m_oFpSetMeasuringMode(m_HandleMaster, normalMode, 0);
228 Status = m_oFpGetFrontEndInfo(m_HandleMaster, &FrontEndInfo);
229 unsigned short serial, hwVersion, swVersion, baseSf, maxRS232, nrOfChannels;
232 cout <<
"Plugin TMSI - ERROR - initDevice() - FrontendInfo NOT available" << endl;
235 serial = FrontEndInfo.Serial;
236 hwVersion = FrontEndInfo.HwVersion;
237 swVersion = FrontEndInfo.SwVersion;
238 baseSf = FrontEndInfo.BaseSf;
239 maxRS232 = FrontEndInfo.maxRS232;
240 nrOfChannels = FrontEndInfo.NrOfChannels;
244 if(m_bUseCommonAverage)
246 BOOLEAN setRefCalculation = m_oFpSetRefCalculation(m_HandleMaster, 1);
247 if(setRefCalculation)
248 cout <<
"Plugin TMSI - INFO - initDevice() - Common average now active" << endl;
250 cout <<
"Plugin TMSI - INFO - initDevice() - Common average is inactive (Could not be initiated)" << endl;
254 PSIGNAL_FORMAT pSignalFormat = m_oFpGetSignalFormat(m_HandleMaster, NULL);
256 if(pSignalFormat != NULL)
258 wcscpy_s(m_wcDeviceName, pSignalFormat->PortName);
259 m_ulSerialNumber = pSignalFormat->SerialNumber;
260 m_uiNumberOfAvailableChannels = pSignalFormat[0].Elements;
262 if(m_bWriteDriverDebugToFile)
263 m_outputFileStream <<
"Found "<< m_wcDeviceName <<
" device (" << m_ulSerialNumber <<
") with " << m_uiNumberOfAvailableChannels <<
" available channels" << endl << endl;
265 for(uint i = 0 ; i < m_uiNumberOfAvailableChannels; i++ )
267 m_vExponentChannel.push_back(pSignalFormat[i].UnitExponent);
268 m_vUnitGain.push_back(pSignalFormat[i].UnitGain);
269 m_vUnitOffSet.push_back(pSignalFormat[i].UnitOffSet);
271 if(m_bWriteDriverDebugToFile)
272 m_outputFileStream <<
"Channel number: " << i <<
" has type " << pSignalFormat[i].Type <<
" , format " << pSignalFormat[i].Format <<
" exponent " << pSignalFormat[i].UnitExponent <<
" gain " << pSignalFormat[i].UnitGain <<
" offset " << pSignalFormat[i].UnitOffSet << endl;
275 if(m_bWriteDriverDebugToFile)
276 m_outputFileStream << endl;
280 ULONG iSamplingFrequencyMilliHertz = m_uiSamplingFrequency*1000;
281 ULONG iBufferSize = MAX_BUFFER_SIZE;
283 if(!m_oFpSetSignalBuffer(m_HandleMaster, &iSamplingFrequencyMilliHertz, &iBufferSize))
285 cout <<
"Plugin TMSI - ERROR - initDevice() - Failed to allocate signal buffer" << endl;
286 m_oFpLibraryExit(m_HandleMaster);
291 bool start = m_oFpStart(m_HandleMaster);
294 cout <<
"Plugin TMSI - ERROR - initDevice() - Failed to start the sampling procedure" << endl;
295 m_oFpLibraryExit(m_HandleMaster);
301 m_lSignalBufferSize = m_uiSamplesPerBlock*m_uiNumberOfAvailableChannels*4;
302 m_lSignalBuffer =
new LONG[m_lSignalBufferSize];
304 cout <<
"Plugin TMSI - INFO - initDevice() - The device has been connected and initialised successfully" << endl;
305 m_bInitDeviceSuccess =
true;
315 m_vSampleBlockBuffer.clear();
318 if(!m_bInitDeviceSuccess)
320 cout <<
"Plugin TMSI - ERROR - uninitDevice() - Device was not initialised - therefore can not be uninitialised" << endl;
327 cout <<
"Plugin TMSI - ERROR - uninitDevice() - Driver DLL was not loaded" << endl;
332 if(m_outputFileStream.is_open() && m_bWriteDriverDebugToFile)
334 m_outputFileStream.close();
335 m_outputFileStream.clear();
338 if(!m_oFpStop(m_HandleMaster))
340 cout <<
"Plugin TMSI - ERROR - uninitDevice() - Failed to stop the device" << endl;
344 if(!m_oFpClose(m_HandleMaster))
346 cout <<
"Plugin TMSI - ERROR - uninitDevice() - Failed to close the device" << endl;
350 m_oFpLibraryExit(m_HandleMaster);
353 m_oLibHandle = NULL ;
354 m_HandleMaster = NULL;
355 m_PSPDPMasterDevicePath = NULL;
356 m_lSignalBuffer = NULL;
358 cout <<
"Plugin TMSI - INFO - uninitDevice() - Successfully uninitialised the device" << endl;
372 if(!m_bInitDeviceSuccess)
374 cout <<
"Plugin TMSI - ERROR - getSampleMatrixValue() - Cannot start to get samples from device because device was not initialised correctly" << endl;
378 sampleMatrix.setZero();
379 uint iSamplesWrittenToMatrix = 0;
382 int sampleIterator = 0;
385 while(iSamplesWrittenToMatrix < m_uiSamplesPerBlock)
388 LONG ulSizeSamples = m_oFpGetSamples(m_HandleMaster, (PULONG)m_lSignalBuffer, m_lSignalBufferSize);
389 LONG ulNumSamplesReceived = ulSizeSamples/(m_uiNumberOfAvailableChannels*4);
392 if(ulNumSamplesReceived > 0)
394 int actualSamplesWritten = 0;
397 for(
int i=0; i<ulNumSamplesReceived; i++)
399 for(uint j=i*m_uiNumberOfAvailableChannels; j<(i*m_uiNumberOfAvailableChannels)+m_uiNumberOfChannels; j++)
400 m_vSampleBlockBuffer.push_back((
double)m_lSignalBuffer[j]);
404 if(m_uiNumberOfAvailableChannels < m_uiNumberOfChannels)
405 channelMax = m_uiNumberOfAvailableChannels;
407 channelMax = m_uiNumberOfChannels;
411 if(iSamplesWrittenToMatrix + ulNumSamplesReceived > m_uiSamplesPerBlock)
412 sampleMax = m_uiSamplesPerBlock - iSamplesWrittenToMatrix + sampleIterator;
414 sampleMax = ulNumSamplesReceived + sampleIterator;
417 for(; sampleIterator < sampleMax; sampleIterator++)
419 for(
int channelIterator = 0; channelIterator < channelMax; channelIterator++)
421 sampleMatrix(channelIterator, sampleIterator) = ((m_vSampleBlockBuffer.first() * (m_bUseUnitGain ? m_vUnitGain[channelIterator] : 1)) + (m_bUseUnitOffset ? m_vUnitOffSet[channelIterator] : 0)) * (m_bUseChExponent ? pow(10., (
double)m_vExponentChannel[channelIterator]) : 1);
422 m_vSampleBlockBuffer.pop_front();
425 actualSamplesWritten ++;
428 iSamplesWrittenToMatrix = iSamplesWrittenToMatrix + actualSamplesWritten;
431 if(m_outputFileStream.is_open() && m_bWriteDriverDebugToFile)
433 m_outputFileStream <<
"samples in buffer: " << m_vSampleBlockBuffer.size()/m_uiNumberOfChannels << endl;
434 m_outputFileStream <<
"ulSizeSamples: " << ulSizeSamples << endl;
435 m_outputFileStream <<
"ulNumSamplesReceived: " << ulNumSamplesReceived << endl;
436 m_outputFileStream <<
"sampleMax: " << sampleMax << endl;
437 m_outputFileStream <<
"sampleIterator: " << sampleIterator << endl;
438 m_outputFileStream <<
"iSamplesWrittenToMatrix: " << iSamplesWrittenToMatrix << endl << endl;
442 if( m_bWriteDriverDebugToFile)
447 m_oFpGetBufferInfo(m_HandleMaster, &ulOverflow, &ulPercentFull);
449 m_outputFileStream <<
"Unit offset: " << endl;
450 for(
int w = 0; w<<m_vUnitOffSet.size(); w++)
451 cout <<
float(m_vUnitOffSet[w]) <<
" ";
452 m_outputFileStream << endl << endl;
454 m_outputFileStream <<
"Unit gain: " << endl;
455 for(
int w = 0; w<<m_vUnitGain.size(); w++)
456 m_outputFileStream <<
float(m_vUnitGain[w]) <<
" ";
457 m_outputFileStream << endl << endl;
459 m_outputFileStream <<
"----------<See output file for sample matrix>----------" <<endl<<endl;
460 m_outputFileStream <<
"----------<Internal driver buffer is "<<ulPercentFull<<
" full>----------"<<endl;
461 m_outputFileStream <<
"----------<Internal driver overflow is "<<ulOverflow<<
">----------"<<endl;
The EEGProducer class provides a EEG data producer for a given sampling rate.
bool initDevice(int iNumberOfChannels, int iSamplingFrequency, int iSamplesPerBlock, bool bUseChExponent, bool bUseUnitGain, bool bUseUnitOffset, bool bWriteDriverDebugToFile, QString sOutpuFilePath, bool bUseCommonAverage, bool bMeasureImpedance)
bool getSampleMatrixValue(MatrixXf &sampleMatrix)
Contains the declaration of the tmsidriver class. This class implements the basic communication betwe...
TMSIDriver(TMSIProducer *pTMSIProducer)
Contains the declaration of the TMSIProducer class.