X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ef06bfd1c0ef5272c32808e23d0fd60d2d1bca9c..8f6b4ddf764c3a54e42d00a7502c82c5c3e71b1c:/Demos/Host/KeyboardHostWithParser/KeyboardHostWithParser.c diff --git a/Demos/Host/KeyboardHostWithParser/KeyboardHostWithParser.c b/Demos/Host/KeyboardHostWithParser/KeyboardHostWithParser.c index d19929d4d..d7d5f9414 100644 --- a/Demos/Host/KeyboardHostWithParser/KeyboardHostWithParser.c +++ b/Demos/Host/KeyboardHostWithParser/KeyboardHostWithParser.c @@ -36,12 +36,6 @@ #include "KeyboardHostWithParser.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA KBD Host App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { @@ -270,70 +264,24 @@ TASK(USB_Keyboard_Host) Pipe_SelectPipe(KEYBOARD_DATAPIPE); Pipe_Unfreeze(); - /* Check if data has been received from the attached keyboard */ - if (Pipe_ReadWriteAllowed()) + /* Check to see if a packet has been received */ + if (Pipe_IsINReceived()) { - /* Create buffer big enough for the report */ - uint8_t KeyboardReport[Pipe_BytesInPipe()]; + /* Check if data has been received from the attached keyboard */ + if (Pipe_IsReadWriteAllowed()) + { + /* Create buffer big enough for the report */ + uint8_t KeyboardReport[Pipe_BytesInPipe()]; - /* Load in the keyboard report */ - Pipe_Read_Stream_LE(KeyboardReport, Pipe_BytesInPipe()); + /* Load in the keyboard report */ + Pipe_Read_Stream_LE(KeyboardReport, Pipe_BytesInPipe()); - /* Clear the IN endpoint, ready for next data packet */ - Pipe_ClearCurrentBank(); - - /* Check each HID report item in turn, looking for keyboard scan code reports */ - for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++) - { - /* Create a temporary item pointer to the next report item */ - HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber]; - - /* Check if the current report item is a keyboard scancode */ - if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_KEYBOARD) && - (ReportItem->Attributes.BitSize == 8) && - (ReportItem->Attributes.Logical.Maximum > 1) && - (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) - { - /* Retrieve the keyboard scancode from the report data retrieved from the device */ - bool FoundData = GetReportItemInfo(KeyboardReport, ReportItem); - - /* For multi-report devices - if the requested data was not in the issued report, continue */ - if (!(FoundData)) - continue; - - /* Key code is an unsigned char in length, cast to the appropriate type */ - uint8_t KeyCode = (uint8_t)ReportItem->Value; - - /* If scancode is non-zero, a key is being pressed */ - if (KeyCode) - { - /* Toggle status LED to indicate keypress */ - if (LEDs_GetLEDs() & LEDS_LED2) - LEDs_TurnOffLEDs(LEDS_LED2); - else - LEDs_TurnOnLEDs(LEDS_LED2); - - char PressedKey = 0; - - /* Convert scancode to printable character if alphanumeric */ - if ((KeyCode >= 0x04) && (KeyCode <= 0x1D)) - PressedKey = (KeyCode - 0x04) + 'A'; - else if ((KeyCode >= 0x1E) && (KeyCode <= 0x27)) - PressedKey = (KeyCode - 0x1E) + '0'; - else if (KeyCode == 0x2C) - PressedKey = ' '; - else if (KeyCode == 0x28) - PressedKey = '\n'; - - /* Print the pressed key character out through the serial port if valid */ - if (PressedKey) - putchar(PressedKey); - } - - /* Once a scancode is found, stop scanning through the report items */ - break; - } + /* Process the read in keyboard report from the device */ + ProcessKeyboardReport(KeyboardReport); } + + /* Clear the IN endpoint, ready for next data packet */ + Pipe_ClearIN(); } /* Freeze keyboard data pipe */ @@ -341,3 +289,64 @@ TASK(USB_Keyboard_Host) break; } } + +/** Processes a read HID report from an attached keyboard, extracting out elements via the HID parser results + * as required and prints pressed characters to the serial port. Each time a key is typed, a board LED is toggled. + * + * \param KeyboardReport Pointer to a HID report from an attached keyboard device + */ +void ProcessKeyboardReport(uint8_t* KeyboardReport) +{ + /* Check each HID report item in turn, looking for keyboard scan code reports */ + for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++) + { + /* Create a temporary item pointer to the next report item */ + HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber]; + + /* Check if the current report item is a keyboard scancode */ + if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_KEYBOARD) && + (ReportItem->Attributes.BitSize == 8) && + (ReportItem->Attributes.Logical.Maximum > 1) && + (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) + { + /* Retrieve the keyboard scancode from the report data retrieved from the device */ + bool FoundData = GetReportItemInfo(KeyboardReport, ReportItem); + + /* For multi-report devices - if the requested data was not in the issued report, continue */ + if (!(FoundData)) + continue; + + /* Key code is an unsigned char in length, cast to the appropriate type */ + uint8_t KeyCode = (uint8_t)ReportItem->Value; + + /* If scancode is non-zero, a key is being pressed */ + if (KeyCode) + { + /* Toggle status LED to indicate keypress */ + if (LEDs_GetLEDs() & LEDS_LED2) + LEDs_TurnOffLEDs(LEDS_LED2); + else + LEDs_TurnOnLEDs(LEDS_LED2); + + char PressedKey = 0; + + /* Convert scancode to printable character if alphanumeric */ + if ((KeyCode >= 0x04) && (KeyCode <= 0x1D)) + PressedKey = (KeyCode - 0x04) + 'A'; + else if ((KeyCode >= 0x1E) && (KeyCode <= 0x27)) + PressedKey = (KeyCode - 0x1E) + '0'; + else if (KeyCode == 0x2C) + PressedKey = ' '; + else if (KeyCode == 0x28) + PressedKey = '\n'; + + /* Print the pressed key character out through the serial port if valid */ + if (PressedKey) + putchar(PressedKey); + } + + /* Once a scancode is found, stop scanning through the report items */ + break; + } + } +}