diff --git a/Gameleap/code/mw4/Code/MW4/CRIOMAIN.CPP b/Gameleap/code/mw4/Code/MW4/CRIOMAIN.CPP index 8d0fe02d..d2d2bc96 100644 --- a/Gameleap/code/mw4/Code/MW4/CRIOMAIN.CPP +++ b/Gameleap/code/mw4/Code/MW4/CRIOMAIN.CPP @@ -43,7 +43,7 @@ LONG g_LeftPedalLast = 0; LONG g_RightPedalLast = 0; BOOL g_StartGame = FALSE; -static int g_nOpenComState = 0; // úè +static int g_nOpenComState = 0; // hyun DWORD g_dwLastAnalogUpdate = 0; @@ -54,7 +54,7 @@ DWORD g_dwLastAnalogUpdate = 0; #define RESTART_CHAR 0xFE #define IDLE_CHAR 0xFF -//member º¯¼öó·³ Ȱ¿ëÇÒ°Í. +//Use it like a member variable. static HANDLE g_hCom = INVALID_HANDLE_VALUE; static OVERLAPPED wos; static OVERLAPPED ros; @@ -108,6 +108,7 @@ int g_nRIOPacketCountB = (sizeof(g_baRIOLengthsB) / sizeof(g_baRIOLengthsB[0])); int g_nRIOType = 0; // 0: old(original) type, 1: new type DWORD g_dwRIOBaud = 0; // [tbaud] 0: default by RIO type; else COM1 baud forced by -tbaud (high-speed replica of the original RIO board, protocol unchanged) +DWORD g_dwRIOPollTimeout = 50; // [tbaud] WaitForMultipleObjects timeout (ms); computed from baud rate before the receive loop starts int g_nEjectButton = 61; // 61: original, 31: new type BYTE* g_pbRIOLengths = NULL; int g_nRIOPacketCount = 0; @@ -318,7 +319,7 @@ void RequestVersion(); void RequestAnalogUpdate(BYTE bFreq = 3); // start - for new RIO Board only... -//received packetÀº °¢°¢ ´Ù¸¥ ½º·¹µå¿¡¼­ ó¸®ÇϹǷÎ.. ciritical sectionÀÌ ÇÊ¿äÇÏ´Ù.. +//Received packets are handled in different threads.. a critical section is needed.. BYTE received_buffer[128][16]; int received_packet=0;//packets count in the received_buffer. DWORD received_serial=0; @@ -569,7 +570,7 @@ BOOL SetupConnection(HANDLE hCom) return TRUE; /* - ¿ø·¡ ó¸® ·çƾ.. + Original processing routine.. NPTTYINFO npTTYInfo=0 ; BYTE bset1 = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_DTRDSR) != 0) ; BYTE bset2 = (BYTE) ((FLOWCTRL( npTTYInfo ) & FC_RTSCTS) != 0) ; @@ -603,7 +604,7 @@ BOOL SetupConnection(HANDLE hCom) -//󸮵ÇÁö ¾Ê´Â Ç׸ñµé.. GetCommStateÀÇ ±âº»°ªÀ¸·Î ó¸®ÇÑ´Ù. +//Unhandled items.. handled using GetCommState default values. DWORD fDsrSensitivity:1; // DSR sensitivity DWORD fTXContinueOnXoff:1; // XOFF continues Tx DWORD fErrorChar: 1; // enable error replacement @@ -647,7 +648,7 @@ HANDLE OpenConnection(int port) g_hLampEvent = CreateEvent(0,FALSE,0,0); g_hCommWatchThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL, 0, (LPTHREAD_START_ROUTINE)CommWatchProc, NULL, CREATE_SUSPENDED, &g_dwThreadID); if (g_hCommWatchThread) { - //Thread±îÁö Á¤»óÀûÀ¸·Î »ý¼ºµÇ¾ú´Ù. + //Thread was created successfully. ////////////////////////////////////////////// //All OK //Exit Point <====== @@ -744,11 +745,11 @@ retry: for(int i=0;i<(int)dwLength;i++){ int ch=(BYTE)lpszBlock[i]; - //¹ÞÀº ¹®ÀÚ¸¸Å­ ·çÇÁ¸¦ µ·´Ù. - //ÇöÀç.. ¹Þ°í ÀÖ´Â packetÀÌ ¾ø°í.. ¾î¶² control¹®ÀÚ¶óµµ ¿Ã ¼ö ÀÖ´Â »óÅÂÀÌ´Ù. + //Loop for the number of received characters. + //Currently.. no packet is being received.. can receive any control character. if(packetbyteremain!=0){ - //ÆÐŶ¿¡ ÇØ´çÇÏ´Â ¹®ÀÚ°¡ µµÂøÇß´Ù.. ³ª¸ÓÁö ¹®ÀÚµéÀ» ¹Þ´Â´Ù. + //Character corresponding to a packet has arrived.. receive the remaining characters. if (ch & 0x80) { packetbyteremain=0; chinpacket=0; @@ -758,7 +759,7 @@ retry: packetbytes[chinpacket] = ch; chinpacket++; if (packetbyteremain == 0) { - //ÇϳªÀÇ ÆÐŶÀÌ ¿Ï¼ºµÇ¾ú´Ù. µµÂøÇß´Ù.. Áï½Ã ¹ÝÀÀÇÑ´Ù. + //A packet has been completed and arrived.. respond immediately. chinpacket--; // exclude check byte int packettype=packetbytes[0]; BYTE bCheckByte = 0; @@ -837,21 +838,21 @@ retry: packettype++; packettype--; #endif // _DEBUG - case rio_Ack2://¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß + case rio_Ack2: if (g_nRIOType != 0) { BYTE id=packetbytes[1]; int popped_index=PopPacket(id); ack_timeout=GetTickCount()+30; if(popped_index==0){ - //¸ÇóÀ½°Í... Á¤»óÀûÀÎ °æ¿ìÀÌ´Ù. + //This is the very first one... Normal case. }else if(popped_index>0){ - //¸ÇóÀ½°ÍÀÌ ¾Æ´Ñ°Í. - //ÀÌÀü°ÍµéÀÌ Á¤»óÀûÀ¸·Î º¸³»ÁöÁö ¾Ê¾ÒÀ¸¹Ç·Î ´Ù½Ã º¸³½´Ù... - ReSendPackets(popped_index);//À߸øµÈ °³¼ö ¸¸Å­.. + //Not the very first one. + //Previous ones were not sent successfully, resend... + ReSendPackets(popped_index);//By the number of incorrect ones.. }else{ - //½ÇÆÐ Çߴٴ°ÍÀº.. ¾ø´Â id°¡ ¿Ô´Ù´Â °ÍÀε¥.. - //¸í¹éÇÑ ¿¡·¯.. ±×·¯³ª Ưº°È÷ ´ëóÇÒ ¹æ¹ýÀº ¾ø´Ù. - //±×³É ¹«½ÃÇÑ´Ù. + //Failure means an id that does not exist arrived.. + //An obvious error.. but there is no particular way to handle it. + //Simply ignore it. } } break; @@ -870,9 +871,9 @@ retry: // WriteTTYBlock(hWnd,"|",1); chinpacket=0; - //packetbytes¹öÆÛ¸¦ Áö¿î´Ù. + //Clear the packetbytes buffer. } else { - //¾ÆÁ÷ ÆÐŶ ¹®ÀÚµéÀÌ ´Ù µµÂøÇÏÁö ¾Ê¾Ò´Ù.. ¾Æ¹«°Íµµ ÇÏÁö ¾Ê´Â´Ù. + //Packet characters have not all arrived yet.. do nothing. ; } }else{ @@ -889,7 +890,7 @@ retry: }//for(int i=0;i<(int)dwLength;i++) - //»óÈÆ µÚ + //sanghoon end if (!fReadStat){ if (GetLastError() == ERROR_IO_PENDING){ while(!GetOverlappedResult( g_hCom,&ros, &dwLength, TRUE )){ @@ -1000,12 +1001,22 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lpData ) } g_dwLastAnalogUpdate = GetTickCount() + 2800; + // [tbaud] Scale the poll timeout proportionally to baud rate so faster links + // poll more frequently. Formula: clamp(ceil(480000/baud), 5, 50) + // preserves the existing 50ms at 9600 baud, floors at 5ms for high speeds. + // effectiveBaud: use -tbaud override if set, else the RIO type's default. + { + DWORD effectiveBaud = (g_dwRIOBaud != 0) ? g_dwRIOBaud + : ((g_nRIOType == 0) ? 9600UL : 115200UL); + g_dwRIOPollTimeout = min(50UL, max(5UL, + (480000UL + effectiveBaud - 1) / effectiveBaud)); + } while(1) { DWORD dwEvtMask = 0; WaitCommEvent( g_hCom, &dwEvtMask,&wcos ); - DWORD ret=WaitForMultipleObjects(3,events,FALSE,50/*INFINITE*/); + DWORD ret=WaitForMultipleObjects(3,events,FALSE,g_dwRIOPollTimeout); if(ret==WAIT_OBJECT_0){ //An Comm Event has arrived. DWORD bytesread; @@ -1022,10 +1033,10 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lpData ) } } else {*/ if (g_nRIOType != 0) { - ///¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ßÀ§ÀÇ ReadCommBlock¿¡¼­ ack¸¦ ¹Þ¾ÒÀ» °æ¿ì duetimeÀ» update½ÃŲ´Ù. - ///¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ßCheckSentBuffer... + ///When ack is received from ReadCommBlock above, update duetime. + ///CheckSentBuffer... if(GetTickCount()>ack_timeout) - ReSendPackets(sent_packet);//sent_buffer¿¡ ÀÖ´Â ¸ðµç°ÍµéÀ» ´Ù½Ã º¸³»º»´Ù. + ReSendPackets(sent_packet);//Resend everything in sent_buffer. } /*}*/ }else if(ret==WAIT_OBJECT_0+1){ @@ -1039,9 +1050,9 @@ DWORD FAR PASCAL CommWatchProc( LPSTR lpData ) //break; }else if (ret==WAIT_TIMEOUT) { if (g_nRIOType != 0) { - ///¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ß¡ßCheckSentBuffer... + ///CheckSentBuffer... if(GetTickCount()>ack_timeout) - ReSendPackets(sent_packet);//sent_buffer¿¡ ÀÖ´Â ¸ðµç°ÍµéÀ» ´Ù½Ã º¸³»º»´Ù. + ReSendPackets(sent_packet);//Resend everything in sent_buffer. } if (packetbyteremain == 0) { RequestAnalogUpdate(); @@ -1236,8 +1247,8 @@ bool SaveReceivedPacket(const BYTE*ba,int length) bool GetSerialFromReceivedBuffer(char * ba) { -//ButtonPressed/Released¸¸ ÀúÀåÇØ ³õ´Â´Ù.. - //¸ðµç serial packetÀ» ¿Å±ä´Ù. +//Store only ButtonPressed/Released.. + //Transfer all serial packets. if(received_packet>0){ if(received_buffer[0][2]==received_serial){ CopyMemory(ba,received_buffer[0],16); @@ -1264,7 +1275,7 @@ bool QueuePacket(const BYTE *ba,int length) int PopPacket(BYTE id) { -//lamp´Â.. id°¡.. +//lamp.. check by id.. //search the packet.. for(int i=0;i ====================== - ¢Ö + /^ g_LeftPedalStart: INT_MAX (+) lP = (LONG)g_LeftPedalStart - g_LeftPedal; - lP °á°ú°ªÀº ¹«Á¶°Ç À½¼ö + lP result value is always negative */ LONG lP = 0; @@ -2362,9 +2373,9 @@ void CRIOMAIN::UpdateThrottle (DIJOYSTATE &js) Up Down ====================== - ¡ç (-) (+) ¡æ + <-- (-) (+) --> ====================== - ¢Ö + /^ g_ThrottleStart: INT_MIN (-) */ @@ -2447,11 +2458,11 @@ void CRIOMAIN::UpdateJoystick (DIJOYSTATE &js) */ ////////////////////////////////////////////////////////////////////////////////// /* - 80 ÀÇ ÀÇ¹Ì + Meaning of 80 - RIO (LEFT :120 ~ RIGHT :-80) ±îÁöÀÇ ¼ö¸¦ µ¹·ÁÁØ´Ù°í °¡Á¤Çϰí - (LEFTÀǰæ¿ì) RIGHT ÀÇ ÃÖ´ë°ª 80 ÀÌ»óÀÎ °ªÀ» Àß¶ó¹ö¸®°í ³ª¸ÓÁö´Â - ¹ö¸±°æ¿ì LEFT ¿Í RIGHT°¡ µ¿ÀÏÇÑ ¼Óµµ·Î ¿òÁ÷Àϼö ÀÖ´Ù + Assuming RIO returns values in range (LEFT:120 ~ RIGHT:-80) + (In the LEFT case) clip values above the RIGHT maximum of 80, and the rest + if discarded, LEFT and RIGHT can move at the same speed */ ////////////////////////////////////////////////////////////////////////////////// LONG lJ;