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