- /* One millisecond has elapsed, decrement the idle time remaining counter if it has not already elapsed */\r
- if (IdleMSRemaining)\r
- IdleMSRemaining--;\r
-}\r
-\r
-/** Fills the given HID report data structure with the next HID report to send to the host.\r
- *\r
- * \param ReportData Pointer to a HID report data structure to be filled\r
- */\r
-void CreateKeyboardReport(USB_KeyboardReport_Data_t* ReportData)\r
-{\r
- uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
-\r
- /* Clear the report contents */\r
- memset(ReportData, 0, sizeof(USB_KeyboardReport_Data_t));\r
-\r
- if (JoyStatus_LCL & JOY_UP)\r
- ReportData->KeyCode[0] = 0x04; // A\r
- else if (JoyStatus_LCL & JOY_DOWN)\r
- ReportData->KeyCode[0] = 0x05; // B\r
-\r
- if (JoyStatus_LCL & JOY_LEFT)\r
- ReportData->KeyCode[0] = 0x06; // C\r
- else if (JoyStatus_LCL & JOY_RIGHT)\r
- ReportData->KeyCode[0] = 0x07; // D\r
-\r
- if (JoyStatus_LCL & JOY_PRESS)\r
- ReportData->KeyCode[0] = 0x08; // E\r
-}\r
-\r
-/** Processes a received LED report, and updates the board LEDs states to match.\r
- *\r
- * \param LEDReport LED status report from the host\r
- */\r
-void ProcessLEDReport(uint8_t LEDReport)\r
-{\r
- uint8_t LEDMask = LEDS_LED2;\r
- \r
- if (LEDReport & 0x01) // NUM Lock\r
- LEDMask |= LEDS_LED1;\r
- \r
- if (LEDReport & 0x02) // CAPS Lock\r
- LEDMask |= LEDS_LED3;\r
-\r
- if (LEDReport & 0x04) // SCROLL Lock\r
- LEDMask |= LEDS_LED4;\r
-\r
- /* Set the status LEDs to the current Keyboard LED status */\r
- LEDs_SetAllLEDs(LEDMask);\r
-}\r
-\r
-/** Sends the next HID report to the host, via the keyboard data endpoint. */\r
-void SendNextReport(void)\r
-{\r
- static USB_KeyboardReport_Data_t PrevKeyboardReportData;\r
- USB_KeyboardReport_Data_t KeyboardReportData;\r
- bool SendReport = true;\r
- \r
- /* Create the next keyboard report for transmission to the host */\r
- CreateKeyboardReport(&KeyboardReportData);\r
- \r
- /* Check to see if the report data has changed - if so a report MUST be sent */\r
- SendReport = (memcmp(&PrevKeyboardReportData, &KeyboardReportData, sizeof(USB_KeyboardReport_Data_t)) != 0);\r
- \r
- /* Save the current report data for later comparison to check for changes */\r
- PrevKeyboardReportData = KeyboardReportData;\r
- \r
- /* Check if the idle period is set and has elapsed */\r
- if ((IdleCount != HID_IDLE_CHANGESONLY) && (!(IdleMSRemaining)))\r
- {\r
- /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */\r
- IdleMSRemaining = (IdleCount << 2);\r
- \r
- /* Idle period is set and has elapsed, must send a report to the host */\r
- SendReport = true;\r
- }\r
- \r
- /* Select the Keyboard Report Endpoint */\r
- Endpoint_SelectEndpoint(KEYBOARD_EPNUM);\r
-\r
- /* Check if Keyboard Endpoint Ready for Read/Write and if we should send a new report */\r
- if (Endpoint_IsReadWriteAllowed() && SendReport)\r
- {\r
- /* Write Keyboard Report Data */\r
- Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
- \r
- /* Finalize the stream transfer to send the last packet */\r
- Endpoint_ClearIN();\r
- }\r
-}\r
-\r
-/** Reads the next LED status report from the host from the LED data endpoint, if one has been sent. */\r
-void ReceiveNextReport(void)\r
-{\r
- /* Select the Keyboard LED Report Endpoint */\r
- Endpoint_SelectEndpoint(KEYBOARD_LEDS_EPNUM);\r
-\r
- /* Check if Keyboard LED Endpoint contains a packet */\r
- if (Endpoint_IsOUTReceived())\r
- {\r
- /* Check to see if the packet contains data */\r
- if (Endpoint_IsReadWriteAllowed())\r
- {\r
- /* Read in the LED report from the host */\r
- uint8_t LEDReport = Endpoint_Read_Byte();\r
-\r
- /* Process the read LED report from the host */\r
- ProcessLEDReport(LEDReport);\r
- }\r
-\r
- /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
- Endpoint_ClearOUT();\r
- }\r