- /* Set the LED mask to the appropriate LED mask based on the given status code */\r
- switch (CurrentStatus)\r
- {\r
- case Status_USBNotReady:\r
- LEDMask = (LEDS_LED1);\r
- break;\r
- case Status_USBEnumerating:\r
- LEDMask = (LEDS_LED1 | LEDS_LED2);\r
- break;\r
- case Status_USBReady:\r
- LEDMask = (LEDS_LED2);\r
- break;\r
- case Status_EnumerationError:\r
- case Status_HardwareError:\r
- LEDMask = (LEDS_LED1 | LEDS_LED3);\r
- break;\r
- }\r
- \r
- /* Set the board LEDs to the new LED mask */\r
- LEDs_SetAllLEDs(LEDMask);\r
-}\r
-\r
-/** Reads in and processes the next report from the attached device, displaying the report\r
- * contents on the board LEDs and via the serial port.\r
- */\r
-void ReadNextReport(void)\r
-{\r
- USB_KeyboardReport_Data_t KeyboardReport;\r
- \r
- /* Select keyboard data pipe */\r
- Pipe_SelectPipe(KEYBOARD_DATAPIPE); \r
-\r
- /* Unfreeze keyboard data pipe */\r
- Pipe_Unfreeze();\r
-\r
- /* Check to see if a packet has been received */\r
- if (!(Pipe_IsINReceived()))\r
- {\r
- /* Refreeze HID data IN pipe */\r
- Pipe_Freeze();\r
- \r
- return;\r
- }\r
- \r
- /* Ensure pipe contains data before trying to read from it */\r
- if (Pipe_IsReadWriteAllowed())\r
- {\r
- /* Read in keyboard report data */\r
- Pipe_Read_Stream_LE(&KeyboardReport, sizeof(KeyboardReport));\r
-\r
- /* Indicate if the modifier byte is non-zero (special key such as shift is being pressed) */\r
- LEDs_ChangeLEDs(LEDS_LED1, (KeyboardReport.Modifier) ? LEDS_LED1 : 0);\r
- \r
- /* Check if a key has been pressed */\r
- if (KeyboardReport.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
- /* Retrieve pressed key character if alphanumeric */\r
- if ((KeyboardReport.KeyCode >= 0x04) && (KeyboardReport.KeyCode <= 0x1D))\r
- PressedKey = (KeyboardReport.KeyCode - 0x04) + 'A';\r
- else if ((KeyboardReport.KeyCode >= 0x1E) && (KeyboardReport.KeyCode <= 0x27))\r
- PressedKey = (KeyboardReport.KeyCode - 0x1E) + '0';\r
- else if (KeyboardReport.KeyCode == 0x2C)\r
- PressedKey = ' '; \r
- else if (KeyboardReport.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
- \r
- /* Clear the IN endpoint, ready for next data packet */\r
- Pipe_ClearIN();\r
-\r
- /* Refreeze keyboard data pipe */\r
- Pipe_Freeze();\r
-}\r
-\r
-/** Task to set the configuration of the attached device after it has been enumerated, and to read and process\r
- * HID reports from the device and display the results onto the board LEDs.\r
- */\r
-TASK(USB_Keyboard_Host)\r
-{\r
- uint8_t ErrorCode;\r
-\r
- switch (USB_HostState)\r
- {\r
- case HOST_STATE_Addressed:\r
- /* Standard request to set the device configuration to configuration 1 */\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
- };\r
-\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
- printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
-\r
- /* Indicate error status */\r
- UpdateStatus(Status_EnumerationError);\r
-\r
- /* Wait until USB device disconnected */\r
- while (USB_IsConnected);\r
- break;\r
- }\r
- \r
- USB_HostState = HOST_STATE_Configured;\r
- break;\r
- case HOST_STATE_Configured:\r
- puts_P(PSTR("Getting Config Data.\r\n"));\r
- \r
- /* Get and process the configuration descriptor data */\r
- if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)\r
- {\r
- if (ErrorCode == ControlError)\r
- puts_P(PSTR("Control Error (Get Configuration).\r\n"));\r
- else\r
- puts_P(PSTR("Invalid Device.\r\n"));\r
-\r
- printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
- \r
- /* Indicate error status */\r
- UpdateStatus(Status_EnumerationError);\r
-\r
- /* Wait until USB device disconnected */\r
- while (USB_IsConnected);\r
- break;\r
- }\r
- \r
- /* HID class request to set the keyboard protocol to the Boot Protocol */\r
- USB_ControlRequest = (USB_Request_Header_t)\r
- {\r
- .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),\r
- .bRequest = REQ_SetProtocol,\r
- .wValue = 0,\r
- .wIndex = 0,\r
- .wLength = 0,\r
- };\r
-\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 Protocol).\r\n"));\r
- printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
-\r
- /* Indicate error status */\r
- UpdateStatus(Status_EnumerationError);\r
- \r
- /* Wait until USB device disconnected */\r
- while (USB_IsConnected);\r
- break;\r
- }\r
-\r
- puts_P(PSTR("Keyboard Enumerated.\r\n"));\r
-\r
- USB_HostState = HOST_STATE_Ready;\r
- break;\r
- case HOST_STATE_Ready:\r
- /* If a report has been received, read and process it */\r
- ReadNextReport();\r
-\r
- break;\r
- }\r