USB_IsConnected is now cleared before the USB_Disconnect() event is fired in response...
[pub/USBasp.git] / Demos / Host / MouseHostWithParser / MouseHostWithParser.c
index 79cecdf..f26e9e2 100644 (file)
  \r
 #include "MouseHostWithParser.h"\r
 \r
-/* Project Tags, for reading out using the ButtLoad project */\r
-BUTTLOADTAG(ProjName,    "LUFA Mouse Host App");\r
-BUTTLOADTAG(BuildTime,   __TIME__);\r
-BUTTLOADTAG(BuildDate,   __DATE__);\r
-BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);\r
-\r
 /* Scheduler Task List */\r
 TASK_LIST\r
 {\r
-       { Task: USB_USBTask          , TaskStatus: TASK_STOP },\r
-       { Task: USB_Mouse_Host       , TaskStatus: TASK_STOP },\r
+       { .Task = USB_USBTask          , .TaskStatus = TASK_STOP },\r
+       { .Task = USB_Mouse_Host       , .TaskStatus = TASK_STOP },\r
 };\r
 \r
 \r
@@ -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\r
  *  starts the library USB task to begin the enumeration and USB management process.\r
  */\r
-EVENT_HANDLER(USB_DeviceAttached)\r
+void EVENT_USB_DeviceAttached(void)\r
 {\r
        puts_P(PSTR("Device Attached.\r\n"));\r
        UpdateStatus(Status_USBEnumerating);\r
@@ -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\r
  *  stops the library USB task management process.\r
  */\r
-EVENT_HANDLER(USB_DeviceUnattached)\r
+void EVENT_USB_DeviceUnattached(void)\r
 {\r
        /* Stop mouse and USB management task */\r
        Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);\r
@@ -111,7 +105,7 @@ EVENT_HANDLER(USB_DeviceUnattached)
 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully\r
  *  enumerated by the host and is now ready to be used by the application.\r
  */\r
-EVENT_HANDLER(USB_DeviceEnumerationComplete)\r
+void EVENT_USB_DeviceEnumerationComplete(void)\r
 {\r
        /* Start Mouse Host task */\r
        Scheduler_SetTaskMode(USB_Mouse_Host, TASK_RUN);\r
@@ -121,7 +115,7 @@ EVENT_HANDLER(USB_DeviceEnumerationComplete)
 }\r
 \r
 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */\r
-EVENT_HANDLER(USB_HostError)\r
+void EVENT_USB_HostError(const uint8_t ErrorCode)\r
 {\r
        USB_ShutDown();\r
 \r
@@ -135,7 +129,7 @@ EVENT_HANDLER(USB_HostError)
 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while\r
  *  enumerating an attached USB device.\r
  */\r
-EVENT_HANDLER(USB_DeviceEnumerationFailed)\r
+void EVENT_USB_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)\r
 {\r
        puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));\r
        printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
@@ -191,15 +185,18 @@ TASK(USB_Mouse_Host)
        {\r
                case HOST_STATE_Addressed:      \r
                        /* Standard request to set the device configuration to configuration 1 */\r
-                       USB_HostRequest = (USB_Host_Request_Header_t)\r
+                       USB_ControlRequest = (USB_Request_Header_t)\r
                                {\r
-                                       bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
-                                       bRequest:      REQ_SetConfiguration,\r
-                                       wValue:        1,\r
-                                       wIndex:        0,\r
-                                       wLength:       0,\r
+                                       .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
+                                       .bRequest      = REQ_SetConfiguration,\r
+                                       .wValue        = 1,\r
+                                       .wIndex        = 0,\r
+                                       .wLength       = 0,\r
                                };\r
 \r
+                       /* Select the control pipe for the request transfer */\r
+                       Pipe_SelectPipe(PIPE_CONTROLPIPE);\r
+\r
                        /* Send the request, display error and wait for device detach if request fails */\r
                        if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
                        {\r
@@ -268,79 +265,24 @@ TASK(USB_Mouse_Host)
                        Pipe_SelectPipe(MOUSE_DATAPIPE);        \r
                        Pipe_Unfreeze();\r
 \r
-                       /* Check if data has been received from the attached mouse */\r
-                       if (Pipe_ReadWriteAllowed())\r
+                       /* Check to see if a packet has been received */\r
+                       if (Pipe_IsINReceived())\r
                        {\r
-                               uint8_t LEDMask = LEDS_NO_LEDS;\r
-\r
-                               /* Create buffer big enough for the report */\r
-                               uint8_t MouseReport[Pipe_BytesInPipe()];\r
+                               /* Check if data has been received from the attached mouse */\r
+                               if (Pipe_IsReadWriteAllowed())\r
+                               {\r
+                                       /* Create buffer big enough for the report */\r
+                                       uint8_t MouseReport[Pipe_BytesInPipe()];\r
 \r
-                               /* Load in the mouse report */\r
-                               Pipe_Read_Stream_LE(MouseReport, Pipe_BytesInPipe());\r
+                                       /* Load in the mouse report */\r
+                                       Pipe_Read_Stream_LE(MouseReport, Pipe_BytesInPipe());\r
                                \r
-                               /* Clear the IN endpoint, ready for next data packet */\r
-                               Pipe_ClearCurrentBank();\r
-\r
-                               /* Check each HID report item in turn, looking for mouse X/Y/button reports */\r
-                               for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)\r
-                               {\r
-                                       /* Create a temporary item pointer to the next report item */\r
-                                       HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber];\r
-                                       \r
-                                       bool FoundData;\r
-\r
-                                       if ((ReportItem->Attributes.Usage.Page       == USAGE_PAGE_BUTTON) &&\r
-                                           (ReportItem->ItemType                    == REPORT_ITEM_TYPE_In))\r
-                                       {\r
-                                               /* Get the mouse button value */\r
-                                               FoundData = GetReportItemInfo(MouseReport, ReportItem);\r
-                                               \r
-                                               /* For multi-report devices - if the requested data was not in the issued report, continue */\r
-                                               if (!(FoundData))\r
-                                                 continue;\r
-\r
-                                               /* If button is pressed, all LEDs are turned on */\r
-                                               if (ReportItem->Value)\r
-                                                 LEDMask = LEDS_ALL_LEDS;\r
-                                       }\r
-                                       else if ((ReportItem->Attributes.Usage.Page   == USAGE_PAGE_GENERIC_DCTRL) &&\r
-                                                ((ReportItem->Attributes.Usage.Usage == USAGE_X)                  ||\r
-                                                 (ReportItem->Attributes.Usage.Usage == USAGE_Y))                 &&\r
-                                                (ReportItem->ItemType                == REPORT_ITEM_TYPE_In))\r
-                                       {\r
-                                               /* Get the mouse relative position value */\r
-                                               FoundData = GetReportItemInfo(MouseReport, ReportItem);\r
-                                               \r
-                                               /* For multi-report devices - if the requested data was not in the issued report, continue */\r
-                                               if (!(FoundData))\r
-                                                 continue;\r
-                                                 \r
-                                               int16_t DeltaMovement;\r
-                                               \r
-                                               if (ReportItem->Attributes.BitSize > 8)\r
-                                                 DeltaMovement = (int16_t)ReportItem->Value;\r
-                                               else\r
-                                                 DeltaMovement = (int8_t)ReportItem->Value;\r
-                                               \r
-                                               /* Determine if the report is for the X or Y delta movement */\r
-                                               if (ReportItem->Attributes.Usage.Usage == USAGE_X)\r
-                                               {\r
-                                                       /* Turn on the appropriate LED according to direction if the delta is non-zero */\r
-                                                       if (DeltaMovement)\r
-                                                         LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);\r
-                                               }\r
-                                               else\r
-                                               {\r
-                                                       /* Turn on the appropriate LED according to direction if the delta is non-zero */\r
-                                                       if (DeltaMovement)\r
-                                                         LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);\r
-                                               }\r
-                                       }\r
+                                       /* Process the read in mouse report from the device */\r
+                                       ProcessMouseReport(MouseReport);\r
                                }\r
                                \r
-                               /* Display the button information on the board LEDs */\r
-                               LEDs_SetAllLEDs(LEDMask);\r
+                               /* Clear the IN endpoint, ready for next data packet */\r
+                               Pipe_ClearIN();\r
                        }\r
 \r
                        /* Freeze mouse data pipe */\r
@@ -349,3 +291,72 @@ TASK(USB_Mouse_Host)
        }\r
 }\r
 \r
+/** Processes a read HID report from an attached mouse, extracting out elements via the HID parser results\r
+ *  as required and displays movement and button presses on the board LEDs.\r
+ *\r
+ *  \param MouseReport  Pointer to a HID report from an attached mouse device\r
+ */\r
+void ProcessMouseReport(uint8_t* MouseReport)\r
+{\r
+       uint8_t LEDMask = LEDS_NO_LEDS;\r
+\r
+       /* Check each HID report item in turn, looking for mouse X/Y/button reports */\r
+       for (uint8_t ReportNumber = 0; ReportNumber < HIDReportInfo.TotalReportItems; ReportNumber++)\r
+       {\r
+               /* Create a temporary item pointer to the next report item */\r
+               HID_ReportItem_t* ReportItem = &HIDReportInfo.ReportItems[ReportNumber];\r
+               \r
+               bool FoundData;\r
+\r
+               if ((ReportItem->Attributes.Usage.Page       == USAGE_PAGE_BUTTON) &&\r
+                       (ReportItem->ItemType                    == REPORT_ITEM_TYPE_In))\r
+               {\r
+                       /* Get the mouse button value */\r
+                       FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem);\r
+                       \r
+                       /* For multi-report devices - if the requested data was not in the issued report, continue */\r
+                       if (!(FoundData))\r
+                         continue;\r
+\r
+                       /* If button is pressed, all LEDs are turned on */\r
+                       if (ReportItem->Value)\r
+                         LEDMask = LEDS_ALL_LEDS;\r
+               }\r
+               else if ((ReportItem->Attributes.Usage.Page   == USAGE_PAGE_GENERIC_DCTRL) &&\r
+                                ((ReportItem->Attributes.Usage.Usage == USAGE_X)                  ||\r
+                                 (ReportItem->Attributes.Usage.Usage == USAGE_Y))                 &&\r
+                                (ReportItem->ItemType                == REPORT_ITEM_TYPE_In))\r
+               {\r
+                       /* Get the mouse relative position value */\r
+                       FoundData = USB_GetHIDReportItemInfo(MouseReport, ReportItem);\r
+                       \r
+                       /* For multi-report devices - if the requested data was not in the issued report, continue */\r
+                       if (!(FoundData))\r
+                         continue;\r
+                         \r
+                       int16_t DeltaMovement;\r
+                       \r
+                       if (ReportItem->Attributes.BitSize > 8)\r
+                         DeltaMovement = (int16_t)ReportItem->Value;\r
+                       else\r
+                         DeltaMovement = (int8_t)ReportItem->Value;\r
+                       \r
+                       /* Determine if the report is for the X or Y delta movement */\r
+                       if (ReportItem->Attributes.Usage.Usage == USAGE_X)\r
+                       {\r
+                               /* Turn on the appropriate LED according to direction if the delta is non-zero */\r
+                               if (DeltaMovement)\r
+                                 LEDMask |= ((DeltaMovement > 0) ? LEDS_LED1 : LEDS_LED2);\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Turn on the appropriate LED according to direction if the delta is non-zero */\r
+                               if (DeltaMovement)\r
+                                 LEDMask |= ((DeltaMovement > 0) ? LEDS_LED3 : LEDS_LED4);\r
+                       }\r
+               }\r
+       }\r
+       \r
+       /* Display the button information on the board LEDs */\r
+       LEDs_SetAllLEDs(LEDMask);\r
+}
\ No newline at end of file