X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/ed031c1df2f5b053b9cd9f48c63e66a42b7c049e..e0985b995009d71b80d214a66944e76f4e41aadb:/Demos/Host/MouseHostWithParser/MouseHostWithParser.c diff --git a/Demos/Host/MouseHostWithParser/MouseHostWithParser.c b/Demos/Host/MouseHostWithParser/MouseHostWithParser.c index 5ccda4912..58a189f6b 100644 --- a/Demos/Host/MouseHostWithParser/MouseHostWithParser.c +++ b/Demos/Host/MouseHostWithParser/MouseHostWithParser.c @@ -36,17 +36,11 @@ #include "MouseHostWithParser.h" -/* Project Tags, for reading out using the ButtLoad project */ -BUTTLOADTAG(ProjName, "LUFA Mouse Host App"); -BUTTLOADTAG(BuildTime, __TIME__); -BUTTLOADTAG(BuildDate, __DATE__); -BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING); - /* Scheduler Task List */ TASK_LIST { - { Task: USB_USBTask , TaskStatus: TASK_STOP }, - { Task: USB_Mouse_Host , TaskStatus: TASK_STOP }, + { .Task = USB_USBTask , .TaskStatus = TASK_STOP }, + { .Task = USB_Mouse_Host , .TaskStatus = TASK_STOP }, }; @@ -75,7 +69,7 @@ int main(void) /* Initialize USB Subsystem */ USB_Init(); - /* Startup message */ + /* Start-up message */ puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY "Mouse Host Demo running.\r\n" ESC_INVERSE_OFF)); @@ -132,7 +126,7 @@ EVENT_HANDLER(USB_HostError) for(;;); } -/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occured while +/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while * enumerating an attached USB device. */ EVENT_HANDLER(USB_DeviceEnumerationFailed) @@ -191,16 +185,19 @@ TASK(USB_Mouse_Host) { case HOST_STATE_Addressed: /* Standard request to set the device configuration to configuration 1 */ - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), - bRequest: REQ_SetConfiguration, - wValue: 1, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE), + .bRequest = REQ_SetConfiguration, + .wValue = 1, + .wIndex = 0, + .wLength = 0, }; - /* Send the request, display error and wait for device detatch if request fails */ + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + /* Send the request, display error and wait for device detach if request fails */ if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful) { puts_P(PSTR("Control Error (Set Configuration).\r\n")); @@ -256,7 +253,7 @@ TASK(USB_Mouse_Host) break; } - /* All LEDs off - ready to indicate keypresses */ + /* All LEDs off - ready to indicate key presses */ UpdateStatus(Status_USBReady); puts_P(PSTR("Mouse Enumerated.\r\n")); @@ -268,79 +265,24 @@ TASK(USB_Mouse_Host) Pipe_SelectPipe(MOUSE_DATAPIPE); Pipe_Unfreeze(); - /* Check if data has been received from the attached mouse */ - if (Pipe_ReadWriteAllowed()) + /* Check to see if a packet has been received */ + if (Pipe_IsINReceived()) { - uint8_t LEDMask = LEDS_NO_LEDS; - - /* Create buffer big enough for the report */ - uint8_t MouseReport[Pipe_BytesInPipe()]; + /* Check if data has been received from the attached mouse */ + if (Pipe_IsReadWriteAllowed()) + { + /* Create buffer big enough for the report */ + uint8_t MouseReport[Pipe_BytesInPipe()]; - /* Load in the mouse report */ - Pipe_Read_Stream_LE(MouseReport, Pipe_BytesInPipe()); + /* Load in the mouse report */ + Pipe_Read_Stream_LE(MouseReport, Pipe_BytesInPipe()); - /* Clear the IN endpoint, ready for next data packet */ - Pipe_ClearCurrentBank(); - - /* Check each HID report item in turn, looking for mouse X/Y/button reports */ - for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++) - { - /* Create a tempoary item pointer to the next report item */ - HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber]; - - bool FoundData; - - if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) && - (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) - { - /* Get the mouse button value */ - FoundData = GetReportItemInfo(MouseReport, ReportItem); - - /* For multi-report devices - if the requested data was not in the issued report, continue */ - if (!(FoundData)) - continue; - - /* If button is pressed, all LEDs are turned on */ - if (ReportItem->Value) - LEDMask = LEDS_ALL_LEDS; - } - else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) && - ((ReportItem->Attributes.Usage.Usage == USAGE_X) || - (ReportItem->Attributes.Usage.Usage == USAGE_Y)) && - (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) - { - /* Get the mouse relative position value */ - FoundData = GetReportItemInfo(MouseReport, ReportItem); - - /* For multi-report devices - if the requested data was not in the issued report, continue */ - if (!(FoundData)) - continue; - - int16_t DeltaMovement; - - if (ReportItem->Attributes.BitSize > 8) - DeltaMovement = (int16_t)ReportItem->Value; - else - DeltaMovement = (int8_t)ReportItem->Value; - - /* Determine if the report is for the X or Y delta movement */ - if (ReportItem->Attributes.Usage.Usage == USAGE_X) - { - /* Turn on the appropriate LED according to direction if the delta is non-zero */ - if (DeltaMovement) - LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2); - } - else - { - /* Turn on the appropriate LED according to direction if the delta is non-zero */ - if (DeltaMovement) - LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4); - } - } + /* Process the read in mouse report from the device */ + ProcessMouseReport(MouseReport); } - /* Display the button information on the board LEDs */ - LEDs_SetAllLEDs(LEDMask); + /* Clear the IN endpoint, ready for next data packet */ + Pipe_ClearIN(); } /* Freeze mouse data pipe */ @@ -349,3 +291,72 @@ TASK(USB_Mouse_Host) } } +/** Processes a read HID report from an attached mouse, extracting out elements via the HID parser results + * as required and displays movement and button presses on the board LEDs. + * + * \param MouseReport Pointer to a HID report from an attached mouse device + */ +void ProcessMouseReport(uint8_t* MouseReport) +{ + uint8_t LEDMask = LEDS_NO_LEDS; + + /* Check each HID report item in turn, looking for mouse X/Y/button reports */ + for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++) + { + /* Create a temporary item pointer to the next report item */ + HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber]; + + bool FoundData; + + if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_BUTTON) && + (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) + { + /* Get the mouse button value */ + FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem); + + /* For multi-report devices - if the requested data was not in the issued report, continue */ + if (!(FoundData)) + continue; + + /* If button is pressed, all LEDs are turned on */ + if (ReportItem->Value) + LEDMask = LEDS_ALL_LEDS; + } + else if ((ReportItem->Attributes.Usage.Page == USAGE_PAGE_GENERIC_DCTRL) && + ((ReportItem->Attributes.Usage.Usage == USAGE_X) || + (ReportItem->Attributes.Usage.Usage == USAGE_Y)) && + (ReportItem->ItemType == REPORT_ITEM_TYPE_In)) + { + /* Get the mouse relative position value */ + FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem); + + /* For multi-report devices - if the requested data was not in the issued report, continue */ + if (!(FoundData)) + continue; + + int16_t DeltaMovement; + + if (ReportItem->Attributes.BitSize > 8) + DeltaMovement = (int16_t)ReportItem->Value; + else + DeltaMovement = (int8_t)ReportItem->Value; + + /* Determine if the report is for the X or Y delta movement */ + if (ReportItem->Attributes.Usage.Usage == USAGE_X) + { + /* Turn on the appropriate LED according to direction if the delta is non-zero */ + if (DeltaMovement) + LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2); + } + else + { + /* Turn on the appropriate LED according to direction if the delta is non-zero */ + if (DeltaMovement) + LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4); + } + } + } + + /* Display the button information on the board LEDs */ + LEDs_SetAllLEDs(LEDMask); +} \ No newline at end of file