/** Function to manage HID report generation and transmission to the host. */ void HID_Task(void) { /* Device must be connected and configured for the task to run */ if (USB_DeviceState != DEVICE_STATE_Configured) return; /* Select the Joystick Report Endpoint */ Endpoint_SelectEndpoint(JOYSTICK_EPADDR); /* Check to see if the host is ready for another packet */ if (Endpoint_IsINReady()) { USB_JoystickReport_Data_t JoystickReportData; /* Create the next HID report to send to the host */ GetNextReport(&JoystickReportData); /* Write Joystick Report Data */ Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData), NULL); /* Finalize the stream transfer to send the last packet */ Endpoint_ClearIN(); /* Clear the report data afterwards */ memset(&JoystickReportData, 0, sizeof(JoystickReportData)); } }
/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific * control requests that are not handled internally by the USB library (including the HID commands, which are * all issued via the control endpoint), so that they can be handled appropriately for the application. */ void EVENT_USB_UnhandledControlPacket(void) { /* Handle HID Class specific requests */ switch (USB_ControlRequest.bRequest) { case REQ_GetReport: if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) { USB_JoystickReport_Data_t JoystickReportData; Endpoint_ClearSETUP(); /* Create the next HID report to send to the host */ GetNextReport(&JoystickReportData); /* Write the report data to the control endpoint */ Endpoint_Write_Control_Stream_LE(&JoystickReportData, sizeof(JoystickReportData)); /* Finalize the stream transfer to send the last packet or clear the host abort */ Endpoint_ClearOUT(); } break; } }