+ switch (USB_HostState)\r
+ {\r
+ case HOST_STATE_Addressed:\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
+ \r
+ uint16_t ConfigDescriptorSize;\r
+ uint8_t ConfigDescriptorData[512];\r
+\r
+ if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData,\r
+ sizeof(ConfigDescriptorData)) != HOST_GETCONFIG_Successful)\r
+ {\r
+ printf("Error Retrieving Configuration Descriptor.\r\n");\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+ USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+ break;\r
+ }\r
+\r
+ if (HID_Host_ConfigurePipes(&Keyboard_HID_Interface,\r
+ ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)\r
+ {\r
+ printf("Attached Device Not a Valid Keyboard.\r\n");\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+ USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+ break;\r
+ }\r
+ \r
+ if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)\r
+ {\r
+ printf("Error Setting Device Configuration.\r\n");\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+ USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+ break;\r
+ }\r
+\r
+ if (USB_HID_Host_SetReportProtocol(&Keyboard_HID_Interface) != 0)\r
+ {\r
+ printf("Error Setting Report Protocol Mode or Not a Valid Keyboard.\r\n");\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+ USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+ break;\r
+ }\r
+ \r
+ LEDs_SetAllLEDs(LEDS_NO_LEDS);\r
+\r
+ printf("Keyboard Enumerated.\r\n");\r
+ USB_HostState = HOST_STATE_Configured;\r
+ break;\r
+ case HOST_STATE_Configured:\r
+ if (HID_Host_IsReportReceived(&Keyboard_HID_Interface))\r
+ {\r
+ uint8_t KeyboardReport[Keyboard_HID_Interface.State.LargestReportSize];\r
+ HID_Host_ReceiveReport(&Keyboard_HID_Interface, &KeyboardReport);\r
+ \r
+ for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)\r
+ {\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 if it is\r
+ * contained within the current report, if not, skip to the next item in the parser list\r
+ */\r
+ if (!(USB_GetHIDReportItemInfo(KeyboardReport, ReportItem)))\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
+ LEDs_ToggleLEDs(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
+ \r
+ break;\r
+ }\r
+ \r
+ HID_Host_USBTask(&Keyboard_HID_Interface);\r