DWORD WINAPI ReaderProc(LPVOID lpv) { DWORD dwRead; // bytes actually read DWORD dwBytesTransferred; // result from WaitForSingleObject struct _COMSTAT status; unsigned long etat; // while (!fThreadDone) { // Wait for an event to occur for the port. // WaitCommEvent (TTY.COMDEV, &dwCommModemStatus, 0); // Re-specify the set of events to be monitored for the port. // SetCommMask (TTY.COMDEV, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING); if ((TTY.CONNECTED) && (!GLOBAL.loading)) { dwBytesTransferred = 0; if (ClearCommError(TTY.COMDEV, &etat, &status)) dwBytesTransferred = status.cbInQue; if (dwBytesTransferred) { dwRead=0; // Read the data from the serial port. ReadFile (TTY.COMDEV, TTY.readBuf, dwBytesTransferred, &dwRead, 0); // Display the data read. if ((dwRead)&&(!TTY.read_pause)) ParseLocalInput((int)dwRead); } else Sleep (1); } } write_logfile("COMPORT closed"); return 1; }
void CALLBACK TimerProc(UINT uID,UINT uMsg,DWORD dwUser,DWORD dw1,DWORD dw2) { LONGLONG pc; MSG msg; QueryPerformanceCounter((_LARGE_INTEGER *)&pc); //TIMING.acttime=pc; check_keys(); if (GLOBAL.neurobit_available) NdProtocolEngine(); if (GLOBAL.emotiv_available) process_emotiv(); if ((!TIMING.pause_timer) && (!GLOBAL.loading)) { // one second passed ? -> update PPS-info if (pc-TIMING.timestamp >= TIMING.pcfreq) { TIMING.timestamp+=TIMING.pcfreq; TIMING.actpps=(int) TIMING.ppscounter; TIMING.ppscounter=0; } // Reading from Archive & next packet demanded? -> read from File and Process Packets while ((pc-TIMING.readtimestamp >= TTY.packettime) || (GLOBAL.fly)) { TIMING.readtimestamp+=TTY.packettime; TIMING.acttime=TIMING.readtimestamp; if(CAPTFILE.do_read&&(CAPTFILE.offset<=TIMING.packetcounter)&&(CAPTFILE.offset+CAPTFILE.length>TIMING.packetcounter)) { long tmp; tmp=TIMING.packetcounter; read_captfile(TTY.amount_to_read); ParseLocalInput(TTY.amount_to_read); if ((TIMING.packetcounter-tmp-1)>0) TIMING.readtimestamp+=TTY.packettime*(TIMING.packetcounter-tmp-1); //return; } // process packets in case of no File-Read and no Com-Read else if ((TTY.read_pause) && (!GLOBAL.neurobit_available) && (!GLOBAL.emotiv_available)) process_packets(); if (GLOBAL.fly) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } } }
int ReadNIA( UINT wParam, LONG lParam ) { int nNIA; int i; UINT dwSize=0; RAWINPUT* raw; //first get size GetRawInputData((HRAWINPUT)(lParam), RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); LPBYTE lpb = new BYTE[dwSize+sizeof(RAWINPUTHEADER)]; if (lpb == NULL) { write_logfile("Create NIA Buffer failed!"); report_error("Create NIA Buffer failed!"); return 0; } //then get data if (GetRawInputData((HRAWINPUT)(lParam), RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize ) write_logfile ("GetRawInputData doesn't return correct size !\n"); raw = (RAWINPUT*)lpb; if(m_hNIA[0]!=NULL && raw->header.hDevice==m_hNIA[0]) nNIA=1; else if(m_hNIA[1]!=NULL && raw->header.hDevice==m_hNIA[1]) nNIA=2; else nNIA=0; if(nNIA>0 && raw->header.dwType == RIM_TYPEHID && raw->data.hid.dwSizeHid==56) { UINT ui; RID_DEVICE_INFO dev_info; dev_info.cbSize=sizeof(RID_DEVICE_INFO); ui=sizeof(RID_DEVICE_INFO); GetRawInputDeviceInfo(raw->header.hDevice,RIDI_DEVICEINFO,&dev_info,&ui); #define MAX_SAMPLE_COUNT 16 int nFixed; // Bytes 49+50 int nTiming; // Timing-Info int nIndex; // sequential Index(corresponding to 4kHz Sampling-Rate) int nSamples; // number of Samples int nByteTrans; // number of Bytes to be transferred (2 Channels at one time!) nFixed=raw->data.hid.bRawData[49]+raw->data.hid.bRawData[50]*0x100; nTiming=raw->data.hid.bRawData[51]+raw->data.hid.bRawData[52]*0x100; nIndex=raw->data.hid.bRawData[53]+raw->data.hid.bRawData[54]*0x100; nSamples=raw->data.hid.bRawData[55]; /* copy channel data to TTY.readBuf (for archive functions) */ nByteTrans = NIABYTECOUNT*2 ; // take 1 Sample (3Bytes) /Channel if ((!TTY.read_pause) && nNIA ==1) { if (oldIndex_NIA1 >0){ // check for lost samples // if (nIndex > (oldIndex+4)){ if (nIndex != (oldIndex_NIA1+nSamples)%0xffff){ GLOBAL.syncloss++; } } oldIndex_NIA1 = nIndex; for (i=0; i<NIABYTECOUNT; i++) TTY.readBuf[i] = (unsigned char) raw->data.hid.bRawData[1+i] ; ParseLocalInput(nByteTrans); // evaluate always all channels(=2 or 6Bytes) with 1st sample; // ParseLocalInput(NIABYTECOUNT); } else if ((!TTY.read_pause) && nNIA ==2) { if (oldIndex_NIA2 >0){ // check for lost samples // if (nIndex > (oldIndex+4)){ if (nIndex != (oldIndex_NIA2+nSamples)%0xffff){ GLOBAL.syncloss++; } } oldIndex_NIA2 = nIndex; for (i=0; i<NIABYTECOUNT; i++) TTY.readBuf[i+3] = (unsigned char) raw->data.hid.bRawData[1+i] ; } } else { write_logfile("NIA connection issue!"); report_error("NIA connection issue!"); } if(lpb!=NULL) delete[] lpb; return 0L; }