MNE-CPP  beta 1.0
tmsidriver.h
Go to the documentation of this file.
1 //=============================================================================================================
37 #ifndef TMSIDRIVER_H
38 #define TMSIDRIVER_H
39 
40 
41 //*************************************************************************************************************
42 //=============================================================================================================
43 // INCLUDES
44 //=============================================================================================================
45 
46 #include <iostream>
47 #include <fstream>
48 #include <cmath>
49 #include <cstring>
50 #include <vector>
51 #include <map>
52 #include <tchar.h>
53 #include <string.h>
54 #include <windows.h>
55 #include <Eigen/Core>
56 
57 
58 //*************************************************************************************************************
59 //=============================================================================================================
60 // QT STL INCLUDES
61 //=============================================================================================================
62 
63 #include <QSharedPointer>
64 #include <qapplication.h>
65 #include <QVector>
66 #include <QSysInfo>
67 
68 
69 //*************************************************************************************************************
70 //=============================================================================================================
71 // DEFINE NAMESPACE TMSIPlugin
72 //=============================================================================================================
73 
74 namespace TMSIPlugin
75 {
76 
77 
78 //*************************************************************************************************************
79 //=============================================================================================================
80 // Structure Typedefs - structure define as used in the TMSiSDK.dll
81 //=============================================================================================================
82 
83 typedef struct _SP_DEVICE_PATH
84 {
85  DWORD dwCbSize;
86  TCHAR devicePath[1];
88 
89 typedef struct _FeatureData
90 {
91  ULONG FeatureId;
92  ULONG Info;
94 
95 typedef struct _SYSTEM_TIME
96 {
97  WORD wYear;
98  WORD wMonth;
99  WORD wDayOfWeek;
100  WORD wDay;
101  WORD wHour;
102  WORD wMinute;
103  WORD wSecond;
104  WORD wMilliseconds;
105 } SYSTEM_TIME;
106 
107 typedef struct _SIGNAL_FORMAT
108 {
109  ULONG Size; // Size of this structure
110  ULONG Elements; // Number of elements in list
111 
112  ULONG Type; // One of the signal types above
113  ULONG SubType; // One of the signal sub-types above
114  ULONG Format; // Float / Integer / Asci / Ect..
115  ULONG Bytes; // Number of bytes per sample including subsignals
116 
117  FLOAT UnitGain;
118  FLOAT UnitOffSet;
119  ULONG UnitId;
120  LONG UnitExponent;
121 
122  WCHAR Name[40];
123 
124  ULONG Port;
125  WCHAR PortName[40];
126  ULONG SerialNumber;
128 
129 typedef enum _TMSiConnectionEnum {
130  TMSiConnectionUndefined = 0, //Undefined connection, indicates programming error
131  TMSiConnectionFiber, //Obsolete, do not use
132  TMSiConnectionBluetooth, //Bluetooth connection with Microsoft driver
133  TMSiConnectionUSB, //USB 2.0 connection direct
134  TMSiConnectionWifi, //Network connection, Ip-adress and port needed, wireless
135  TMSiConnectionNetwork //Network connection, Ip-adress and port needed, wired
136 } TMSiConnectionType;
137 
138 typedef struct _FRONTENDINFO
139 { unsigned short NrOfChannels; //Current number of channels used
140  unsigned short SampleRateSetting; //Current sample rate setting (a.k.a. base sample rate divider )
141  unsigned short Mode; //operating mode
142  unsigned short maxRS232;
143  unsigned long Serial; //Serial number
144  unsigned short NrExg; //Number of Exg channels in this device
145  unsigned short NrAux; //Number of Aux channels in this device
146  unsigned short HwVersion; //Version number for the hardware
147  unsigned short SwVersion; //Version number of the embedded software
148  unsigned short RecBufSize; //Used for debugging only
149  unsigned short SendBufSize; //Used for debugging only
150  unsigned short NrOfSwChannels; //Max. number of channels supported by this device
151  unsigned short BaseSf; //Max. sample frequency
152  unsigned short Power;
153  unsigned short Check;
155 
156 //*************************************************************************************************************
157 //=============================================================================================================
158 // Method Typedefs - method defines as used in the RTINST.DLL
159 //=============================================================================================================
160 
161 // define a pointer POPEN to a function which is taking a void pointe and a char and returns a bool value
162 typedef BOOLEAN ( __stdcall * POPEN) (void *Handle, const char *DeviceLocator);
163 typedef BOOL ( __stdcall * PCLOSE) (HANDLE hHandle);
164 typedef BOOLEAN ( __stdcall * PSTART) (IN HANDLE Handle);
165 typedef BOOLEAN ( __stdcall * PSTOP) (IN HANDLE Handle);
166 typedef PSIGNAL_FORMAT ( __stdcall * PGETSIGNALFORMAT) (IN HANDLE Handle, IN OUT char* FrontEndName);
167 typedef BOOLEAN ( __stdcall * PSETSIGNALBUFFER) (IN HANDLE Handle, IN OUT PULONG SampleRate, IN OUT PULONG BufferSize);
168 typedef LONG ( __stdcall * PGETSAMPLES) (IN HANDLE Handle, OUT PULONG SampleBuffer, IN ULONG Size);
169 typedef BOOLEAN ( __stdcall * PGETBUFFERINFO) (IN HANDLE Handle, OUT PULONG Overflow, OUT PULONG PercentFull);
170 typedef BOOL ( __stdcall * PFREE) (IN VOID *Memory);
171 typedef HANDLE ( __stdcall * PLIBRARYINIT) (IN TMSiConnectionType GivenConnectionType, IN OUT int *ErrorCode );
172 typedef int ( __stdcall * PLIBRARYEXIT) (IN HANDLE Handle);
173 typedef char** ( __stdcall * PGETDEVICELIST) (IN HANDLE Handle, IN OUT int *NrOfFrontEnds);
174 typedef BOOLEAN ( __stdcall * PGETFRONTENDINFO) (IN HANDLE Handle, IN OUT FRONTENDINFO *FrontEndInfo );
175 typedef BOOLEAN ( __stdcall * PSETREFCALCULATION) (IN HANDLE Handle, IN int OnOrOff );
176 typedef BOOLEAN ( __stdcall * PSETMEASURINGMODE) (IN HANDLE Handle, IN ULONG Mode, IN int Value );
177 typedef BOOLEAN ( __stdcall * PGETERRORCODE) (IN HANDLE Handle);
178 
179 
180 //*************************************************************************************************************
181 //=============================================================================================================
182 // USED NAMESPACES
183 //=============================================================================================================
184 
185 using namespace std;
186 using namespace Eigen;
187 
188 
189 //*************************************************************************************************************
190 //=============================================================================================================
191 // FORWARD DECLARATIONS
192 //=============================================================================================================
193 
194 class TMSIProducer;
195 
196 
197 //*************************************************************************************************************
198 //=============================================================================================================
199 // DEFINES
200 //=============================================================================================================
201 
202 #define MAX_BUFFER_SIZE 0xFFFFFFFF
203 
204 //DLL Function loader define
205 #define __load_dll_func__(var, type, name) \
206  var = (type)::GetProcAddress(m_oLibHandle, name); \
207  if(!var) \
208  cout<< "Plugin TMSI - ERROR - Error loading method " << name << "\n"; \
209 
210 // Measurement modes:
211 #define MEASURE_MODE_NORMAL ((ULONG)0x0)
212 #define MEASURE_MODE_IMPEDANCE ((ULONG)0x1)
213 #define MEASURE_MODE_CALIBRATION ((ULONG)0x2)
214 
215 #define MEASURE_MODE_IMPEDANCE_EX ((ULONG)0x3)
216 #define MEASURE_MODE_CALIBRATION_EX ((ULONG)0x4)
217 
218 // for MEASURE_MODE_IMPEDANCE:
219 #define IC_OHM_002 0
220 #define IC_OHM_005 1
221 #define IC_OHM_010 2
222 #define IC_OHM_020 3
223 #define IC_OHM_050 4
224 #define IC_OHM_100 5
225 #define IC_OHM_200 6
227 // for MEASURE_MODE_CALIBRATION:
228 #define IC_VOLT_050 0
229 #define IC_VOLT_100 1
230 #define IC_VOLT_200 2
231 #define IC_VOLT_500 3
233  // for Signat Format
234 #define SF_UNSIGNED 0x0 // Unsigned integer
235 #define SF_INTEGER 0x1 // signed integer
236 
237 // integer overflow value for analog channels
238 #define OVERFLOW_32BITS ((long) 0x80000000)
239 // Get Signal info
240 
241 #define SIGNAL_NAME 40
242 
243 // Unit defines
244 #define UNIT_UNKNOWN 0 // Used for digital inputs or if the driver cannot determine the units of a channel
245 #define UNIT_VOLT 1 // Channel measures voltage
246 #define UNIT_PERCENT 2 // Channel measures a percentage
247 #define UNIT_BPM 3 // Beats per minute
248 #define UNIT_BAR 4 // Pressure in bar
249 #define UNIT_PSI 5 // Pressure in psi
250 #define UNIT_MH20 6 // Pressure calibrated to meters water
251 #define UNIT_MHG 7 // Pressure calibrated to meters mercury
252 #define UNIT_BIT 8 // Used for digital inputs
253 
254 // Check windows
255 //#if defined (TAKE_TMSISDK_DLL)
256 //#define TMSISDK true
257 //#define TMSISDK32 false
258 //#endif
259 
260 //#if defined (TAKE_TMSISDK_32_DLL)
261 //#define TMSISDK32 true
262 //#define TMSISDK false
263 //#endif
264 
265 
266 //=============================================================================================================
273 {
274 public:
275  //=========================================================================================================
281  TMSIDriver(TMSIProducer* pTMSIProducer);
282 
283  //=========================================================================================================
287  ~TMSIDriver();
288 
289  //=========================================================================================================
295  bool getSampleMatrixValue(MatrixXf& sampleMatrix);
296 
297  //=========================================================================================================
312  bool initDevice(int iNumberOfChannels,
313  int iSamplingFrequency,
314  int iSamplesPerBlock,
315  bool bUseChExponent,
316  bool bUseUnitGain,
317  bool bUseUnitOffset,
318  bool bWriteDriverDebugToFile,
319  QString sOutpuFilePath,
320  bool bUseCommonAverage,
321  bool bMeasureImpedance);
322 
323  //=========================================================================================================
328  bool uninitDevice();
329 
330 private:
331  TMSIProducer* m_pTMSIProducer;
333  //Flags
334  bool m_bInitDeviceSuccess;
335  bool m_bDllLoaded;
337  //User definitons
338  uint m_uiNumberOfChannels;
339  uint m_uiSamplingFrequency;
340  uint m_uiSamplesPerBlock;
341  bool m_bUseChExponent;
342  bool m_bUseUnitGain;
343  bool m_bUseUnitOffset;
344  bool m_bWriteDriverDebugToFile;
345  bool m_bUsePreprocessing;
346  QString m_sOutputFilePath;
347  bool m_bUseCommonAverage;
348  bool m_bMeasureImpedances;
350  //Handler
351  HANDLE m_HandleMaster;
352  HINSTANCE m_oLibHandle;
354  //Device info
355  WCHAR m_wcDeviceName[40];
356  ULONG m_ulSerialNumber;
357  PSP_DEVICE_PATH m_PSPDPMasterDevicePath;
358  ULONG m_uiNumberOfAvailableChannels;
360  //Signal info
361  QVector <LONG> m_vExponentChannel;
362  QVector <FLOAT> m_vUnitGain;
363  QVector <FLOAT> m_vUnitOffSet;
364  LONG* m_lSignalBuffer;
365  LONG m_lSignalBufferSize;
366  ofstream m_outputFileStream;
367  QVector <double> m_vSampleBlockBuffer;
369  //Variables used for loading the TMSiSDK.dll methods. Note: Not all functions are used by this class at the moment.
370  POPEN m_oFpOpen;
371  PCLOSE m_oFpClose;
372  PSTART m_oFpStart;
373  PSTOP m_oFpStop;
374  PGETSIGNALFORMAT m_oFpGetSignalFormat;
375  PSETSIGNALBUFFER m_oFpSetSignalBuffer;
376  PGETSAMPLES m_oFpGetSamples;
377  PGETBUFFERINFO m_oFpGetBufferInfo;
378  PFREE m_oFpFree;
379  PLIBRARYINIT m_oFpLibraryInit;
380  PLIBRARYEXIT m_oFpLibraryExit;
381  PGETDEVICELIST m_oFpGetDeviceList;
382  PGETFRONTENDINFO m_oFpGetFrontEndInfo;
383  PSETREFCALCULATION m_oFpSetRefCalculation;
384  PSETMEASURINGMODE m_oFpSetMeasuringMode;
385  PGETERRORCODE m_oFpGetErrorCode;
386 };
387 
388 } // NAMESPACE
389 
390 #endif // TMSIDRIVER_H
The EEGProducer class provides a EEG data producer for a given sampling rate.
Definition: tmsiproducer.h:89
The TMSIDriver class provides real time data acquisition of EEG data with a TMSi Refa device...
Definition: tmsidriver.h:272