Make sure we do not try to send another report before the old one has finished sending

This commit is contained in:
Kristian Sloth Lauszus
2018-06-23 17:33:20 +02:00
parent 14eb9c6ec0
commit 724378cf6c
5 changed files with 60 additions and 25 deletions

View File

@@ -3,6 +3,7 @@
uint32_t UsbMouseActionCounter;
static usb_mouse_report_t usbMouseReports[2];
usb_mouse_report_t* ActiveUsbMouseReport = usbMouseReports;
volatile bool sendUsbMouseReportCompleted = true;
usb_mouse_report_t* GetInactiveUsbMouseReport(void)
{
@@ -21,32 +22,31 @@ void ResetActiveUsbMouseReport(void)
usb_status_t usbMouseAction(void)
{
if (!sendUsbMouseReportCompleted)
return kStatus_USB_Busy; // The previous report has not been sent yet
UsbMouseActionCounter++;
SwitchActiveUsbMouseReport(); // Switch the active report
usb_status_t usb_status = USB_DeviceHidSend(
UsbCompositeDevice.mouseHandle, USB_MOUSE_ENDPOINT_INDEX,
(uint8_t*)ActiveUsbMouseReport, USB_MOUSE_REPORT_LENGTH);
if (usb_status == kStatus_USB_Success)
SwitchActiveUsbMouseReport(); // Switch the active report if the data was sent successfully
(uint8_t*)GetInactiveUsbMouseReport(), USB_MOUSE_REPORT_LENGTH);
if (usb_status == kStatus_USB_Success) {
sendUsbMouseReportCompleted = false;
}
return usb_status;
}
usb_status_t UsbMouseCallback(class_handle_t handle, uint32_t event, void *param)
{
static bool usbMouseActionActive = false;
usb_status_t error = kStatus_USB_Error;
switch (event) {
// This report is received when the report has been sent
case kUSB_DeviceHidEventSendResponse:
if (UsbCompositeDevice.attach) {
// Send out the last report continuously if the report was not zeros
usb_mouse_report_t *report = GetInactiveUsbMouseReport();
uint8_t zeroBuf[sizeof(usb_mouse_report_t)] = { 0 };
bool reportChanged = memcmp(report, zeroBuf, sizeof(usb_mouse_report_t)) != 0;
if (usbMouseActionActive || reportChanged) {
usbMouseActionActive = reportChanged; // Used to send out all zeros once after a report has been sent
return usbMouseAction();
}
}
sendUsbMouseReportCompleted = true;
error = kStatus_USB_Success;
break;
case kUSB_DeviceHidEventRecvResponse:
case kUSB_DeviceHidEventGetReport:
case kUSB_DeviceHidEventSetReport:
case kUSB_DeviceHidEventRequestReportBuffer: