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