X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/6933f2e1a543b066ebe734bd126a7ff2f1c2777f..4a8ac5e474741f14492597880e751b3f806cb965:/Demos/Host/MouseHostWithParser/MouseHostWithParser.c diff --git a/Demos/Host/MouseHostWithParser/MouseHostWithParser.c b/Demos/Host/MouseHostWithParser/MouseHostWithParser.c index 79cecdf5b..f26e9e2a6 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 }, }; @@ -86,7 +80,7 @@ int main(void) /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and * starts the library USB task to begin the enumeration and USB management process. */ -EVENT_HANDLER(USB_DeviceAttached) +void EVENT_USB_DeviceAttached(void) { puts_P(PSTR("Device Attached.\r\n")); UpdateStatus(Status_USBEnumerating); @@ -98,7 +92,7 @@ EVENT_HANDLER(USB_DeviceAttached) /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and * stops the library USB task management process. */ -EVENT_HANDLER(USB_DeviceUnattached) +void EVENT_USB_DeviceUnattached(void) { /* Stop mouse and USB management task */ Scheduler_SetTaskMode(USB_USBTask, TASK_STOP); @@ -111,7 +105,7 @@ EVENT_HANDLER(USB_DeviceUnattached) /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully * enumerated by the host and is now ready to be used by the application. */ -EVENT_HANDLER(USB_DeviceEnumerationComplete) +void EVENT_USB_DeviceEnumerationComplete(void) { /* Start Mouse Host task */ Scheduler_SetTaskMode(USB_Mouse_Host, TASK_RUN); @@ -121,7 +115,7 @@ EVENT_HANDLER(USB_DeviceEnumerationComplete) } /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ -EVENT_HANDLER(USB_HostError) +void EVENT_USB_HostError(const uint8_t ErrorCode) { USB_ShutDown(); @@ -135,7 +129,7 @@ EVENT_HANDLER(USB_HostError) /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while * enumerating an attached USB device. */ -EVENT_HANDLER(USB_DeviceEnumerationFailed) +void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode) { puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n")); printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode); @@ -191,15 +185,18 @@ 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, }; + /* 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) { @@ -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 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 = 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