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);
}

USBHostHIDkdb.c 中使用 number() 的函数。该代码将驱动程序附加到主机。并在击键事件发生时将键盘使用 ID 写入 UART。

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

if (status == USBHOSTHID_OK) { while (1) { if (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);

}

4

1 回答 1

0

number() takes a byte, and writes its value in hex (as two characters).

Let's examine some parts of the code.

The following extract the high nibble of val:

(val >> 4)

The following extract the low nibble of val:

(val & 15)

The following coverts a value between 0 and 15 to the hex digit (0..9, A..F):

nibble = (...) + '0';
if (nibble > '9') nibble += ('A' - '9' - 1);
于 2013-10-25T05:53:10.013 回答