Added new Relay Controller Board project (thanks to OBinou).
[pub/USBasp.git] / Demos / Device / LowLevel / Joystick / Joystick.c
index 0c018aa..0edead5 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -37,7 +37,7 @@
 #include "Joystick.h"\r
 \r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
- *  starts the scheduler to run the application tasks.\r
+ *  enters a loop to run the application tasks in sequence.\r
  */\r
 int main(void)\r
 {\r
@@ -72,7 +72,7 @@ void SetupHardware(void)
 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and\r
  *  starts the library USB task to begin the enumeration and USB management process.\r
  */\r
-void EVENT_USB_Connect(void)\r
+void EVENT_USB_Device_Connect(void)\r
 {\r
        /* Indicate USB enumerating */\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
@@ -81,7 +81,7 @@ void EVENT_USB_Connect(void)
 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via\r
  *  the status LEDs and stops the USB management and joystick reporting tasks.\r
  */\r
-void EVENT_USB_Disconnect(void)\r
+void EVENT_USB_Device_Disconnect(void)\r
 {\r
        /* Indicate USB not ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
@@ -90,7 +90,7 @@ void EVENT_USB_Disconnect(void)
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration\r
  *  of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.\r
  */ \r
-void EVENT_USB_ConfigurationChanged(void)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
        /* Indicate USB connected and ready */\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
@@ -104,11 +104,11 @@ void EVENT_USB_ConfigurationChanged(void)
        }\r
 }\r
 \r
-/** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific\r
+/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific\r
  *  control requests that are not handled internally by the USB library (including the HID commands, which are\r
  *  all issued via the control endpoint), so that they can be handled appropriately for the application.\r
  */\r
-void EVENT_USB_UnhandledControlPacket(void)\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
        /* Handle HID Class specific requests */\r
        switch (USB_ControlRequest.bRequest)\r
@@ -136,7 +136,7 @@ void EVENT_USB_UnhandledControlPacket(void)
 \r
 /** Fills the given HID report data structure with the next HID report to send to the host.\r
  *\r
- *  \param ReportData  Pointer to a HID report data structure to be filled\r
+ *  \param[out] ReportData  Pointer to a HID report data structure to be filled\r
  *\r
  *  \return Boolean true if the new report differs from the last report, false otherwise\r
  */\r
@@ -156,10 +156,10 @@ bool GetNextReport(USB_JoystickReport_Data_t* ReportData)
        else if (JoyStatus_LCL & JOY_DOWN)\r
          ReportData->Y =  100;\r
 \r
-       if (JoyStatus_LCL & JOY_RIGHT)\r
-         ReportData->X =  100;\r
-       else if (JoyStatus_LCL & JOY_LEFT)\r
+       if (JoyStatus_LCL & JOY_LEFT)\r
          ReportData->X = -100;\r
+       else if (JoyStatus_LCL & JOY_RIGHT)\r
+         ReportData->X =  100;\r
 \r
        if (JoyStatus_LCL & JOY_PRESS)\r
          ReportData->Button  = (1 << 1);\r
@@ -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_DeviceState != DEVICE_STATE_Configured)\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
+               /* 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
-                       /* 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
+               /* Clear the report data afterwards */\r
+               memset(&JoystickReportData, 0, sizeof(JoystickReportData));\r
        }\r
 }\r