Create a new function pointer type in StreamCallbacks.h for endpoint/pipe stream...
[pub/USBasp.git] / Demos / Host / KeyboardHostWithParser / KeyboardHostWithParser.c
index f7a0087..9eb3d10 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
-       { Task: USB_USBTask          , TaskStatus: TASK_STOP },\r
-       { Task: USB_Keyboard_Host    , TaskStatus: TASK_STOP },\r
+       { .Task = USB_USBTask          , .TaskStatus = TASK_STOP },\r
+       { .Task = USB_Keyboard_Host    , .TaskStatus = TASK_STOP },\r
 };\r
 \r
 \r
@@ -75,7 +69,7 @@ int main(void)
        /* Initialize USB Subsystem */\r
        USB_Init();\r
 \r
-       /* Startup message */\r
+       /* Start-up message */\r
        puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY\r
               "Keyboard Host Demo running.\r\n" ESC_INVERSE_OFF));\r
                   \r
@@ -132,7 +126,7 @@ EVENT_HANDLER(USB_HostError)
        for(;;);\r
 }\r
 \r
-/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occured while\r
+/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while\r
  *  enumerating an attached USB device.\r
  */\r
 EVENT_HANDLER(USB_DeviceEnumerationFailed)\r
@@ -190,16 +184,19 @@ TASK(USB_Keyboard_Host)
        {\r
                case HOST_STATE_Addressed:\r
                        /* Standard request to set the device configuration to configuration 1 */\r
-                       USB_HostRequest = (USB_Host_Request_Header_t)\r
+                       USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
-                                       bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
-                                       bRequest:      REQ_SetConfiguration,\r
-                                       wValue:        1,\r
-                                       wIndex:        0,\r
-                                       wLength:       0,\r
+                                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
+                                       .bRequest      = REQ_SetConfiguration,\r
+                                       .wValue        = 1,\r
+                                       .wIndex        = 0,\r
+                                       .wLength       = 0,\r
                                };\r
 \r
-                       /* Send the request, display error and wait for device detatch if request fails */\r
+                       /* Select the control pipe for the request transfer */\r
+                       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
+\r
+                       /* Send the request, display error and wait for device detach if request fails */\r
                        if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
                        {\r
                                puts_P(PSTR("Control Error (Set Configuration).\r\n"));\r
@@ -255,7 +252,7 @@ TASK(USB_Keyboard_Host)
                                break;  \r
                        }\r
                        \r
-                       /* All LEDs off - ready to indicate keypresses */\r
+                       /* All LEDs off - ready to indicate key presses */\r
                        UpdateStatus(Status_USBReady);\r
 \r
                        puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
@@ -267,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 tempoary 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
@@ -338,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 = USB_GetHIDReportItemInfo(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