Add explicit guards to all device mode tasks to ensure the device is connected and...
[pub/lufa.git] / Demos / Device / LowLevel / Joystick / Joystick.c
index 42ae62c..e117475 100644 (file)
@@ -181,28 +181,28 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
 /** Function to manage HID report generation and transmission to the host. */\r
 void HID_Task(void)\r
 {\r
-       /* Check if the USB System is connected to a Host */\r
-       if (USB_IsConnected)\r
+       /* Device must be connected and configured for the task to run */\r
+       if (!(USB_IsConnected) || !(USB_ConfigurationNumber))\r
+         return;\r
+  \r
+       /* Select the Joystick Report Endpoint */\r
+       Endpoint_SelectEndpoint(JOYSTICK_EPNUM);\r
+\r
+       /* Check to see if the host is ready for another packet */\r
+       if (Endpoint_IsINReady())\r
        {\r
-               /* Select the Joystick Report Endpoint */\r
-               Endpoint_SelectEndpoint(JOYSTICK_EPNUM);\r
-\r
-               /* Check to see if the host is ready for another packet */\r
-               if (Endpoint_IsINReady())\r
-               {\r
-                       USB_JoystickReport_Data_t JoystickReportData;\r
-                       \r
-                       /* Create the next HID report to send to the host */\r
-                       GetNextReport(&JoystickReportData);\r
+               USB_JoystickReport_Data_t JoystickReportData;\r
                \r
-                       /* Write Joystick Report Data */\r
-                       Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));\r
-\r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-                       \r
-                       /* Clear the report data afterwards */\r
-                       memset(&JoystickReportData, 0, sizeof(JoystickReportData));\r
-               }\r
+               /* Create the next HID report to send to the host */\r
+               GetNextReport(&JoystickReportData);\r
+       \r
+               /* Write Joystick Report Data */\r
+               Endpoint_Write_Stream_LE(&JoystickReportData, sizeof(JoystickReportData));\r
+\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
+               \r
+               /* Clear the report data afterwards */\r
+               memset(&JoystickReportData, 0, sizeof(JoystickReportData));\r
        }\r
 }\r