2013-10-25 3 views
1
I was looking for Keyboard firmware codes on FTDI website. I came across few codes such as USBHostHIDKdb, USBHostHID and USBHostHid2. In all the firmware codes I have noticed number function. But I am not getting the logic of it. Below I have written its code. Please check it and help me understand its logic. 

/* 
** number 
** 
** Print a character in the terminal application. 
** 
** Parameters: val - Byte to be printed 
** Returns: void 
** Comments: 
*/ 
void number(unsigned char val) 
{ 
    unsigned char nibble; 

    nibble = (val >> 4) + '0'; 
    if (nibble > '9') nibble += ('A' - '9' - 1); 

    vos_dev_write(hUART, &nibble, (uint16) 1, NULL); 

    nibble = (val & 15) + '0'; 
    if (nibble > '9') nibble += ('A' - '9' - 1); 

    vos_dev_write(hUART, &nibble, (uint16) 1, NULL); 
} 

여기서 function()은 USBHostHIDkdb.c에서 사용됩니다. 코드는 드라이버를 호스트에 연결합니다. 키 스트로크 이벤트가 발생하면 키보드 사용 ID를 UART에 기록합니다.USBHOSTHidKdb 펌웨어 코드에서 사용되는 'number()'함수의 논리 코드

void firmware() 
{ 

    do 
    { 
     open_drivers(); 

     if (status == PORT_STATE_ENUMERATED) 
     { 
      message("Enumeration complete\r\n"); 

      attach_drivers(); 

      // get report descriptor code, set idle code, get idle code 

경우 (상태 == USBHOSTHID_OK) { 동안 (1) { 경우 (vos_dev_read (hUSBHOST_HID, BUF, 8 & num_read) == USBHOSTHID_OK) {

     for (byteCount = 0; byteCount < num_read; byteCount++) 
         { 
          number(buf[byteCount]); 
         } 
         message(eol); 
        } 

       } 
       } 

} while (1); 

}

답변

0

number()은 1 바이트를 취하여 그 값을 16 진수 (2 자)로 씁니다.

코드의 일부를 살펴 보겠습니다.

다음 추출물 val 높은 nibble :

(val >> 4) 

다음 추출물 val 낮은 nibble :

(val & 15) 

다음은 16 진수 0과 15 사이의 값을 (띤다 0 .. 9, A .. F) :