Oops: Really disable building of Projects/Host/ClassDriver directory in the Projects...
[pub/USBasp.git] / Demos / Device / LowLevel / KeyboardMouse / KeyboardMouse.c
index ddfe05a..bfd337a 100644 (file)
@@ -80,7 +80,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
@@ -89,7 +89,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 task.\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
@@ -98,7 +98,7 @@ void EVENT_USB_Disconnect(void)
 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration\r
  *  of the USB device after enumeration, and configures the keyboard and mouse device endpoints.\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
@@ -132,7 +132,7 @@ void EVENT_USB_ConfigurationChanged(void)
  *  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
        uint8_t* ReportData;\r
        uint8_t  ReportSize;\r
@@ -174,7 +174,11 @@ void EVENT_USB_UnhandledControlPacket(void)
                                Endpoint_ClearSETUP();\r
                                \r
                                /* Wait until the LED report has been sent by the host */\r
-                               while (!(Endpoint_IsOUTReceived()));\r
+                               while (!(Endpoint_IsOUTReceived()))\r
+                               {\r
+                                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                                         return;\r
+                               }\r
 \r
                                /* Read in the LED report from the host */\r
                                uint8_t LEDStatus = Endpoint_Read_Byte();\r
@@ -195,9 +199,7 @@ void EVENT_USB_UnhandledControlPacket(void)
                                /* Clear the endpoint data */\r
                                Endpoint_ClearOUT();\r
 \r
-                               /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsINReady()));\r
-                               Endpoint_ClearIN();\r
+                               Endpoint_ClearStatusStage();\r
                        }\r
                        \r
                        break;\r
@@ -212,6 +214,10 @@ void Keyboard_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+\r
        /* Check if board button is not pressed, if so mouse mode enabled */\r
        if (!(Buttons_GetStatus() & BUTTONS_BUTTON1))\r
        {\r
@@ -228,51 +234,47 @@ void Keyboard_HID_Task(void)
                if (JoyStatus_LCL & JOY_PRESS)\r
                  KeyboardReportData.KeyCode[0] = 0x08; // E\r
        }\r
-       \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
+\r
+       /* Select the Keyboard Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
+\r
+       /* Check if Keyboard Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
        {\r
-               /* Select the Keyboard Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_IN_EPNUM);\r
-\r
-               /* Check if Keyboard Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Write Keyboard Report Data */\r
-                       Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
-\r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
-\r
-                       /* Clear the report data afterwards */\r
-                       memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
-               }\r
-\r
-               /* Select the Keyboard LED Report Endpoint */\r
-               Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
-\r
-               /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {               \r
-                       /* Read in the LED report from the host */\r
-                       uint8_t LEDStatus = Endpoint_Read_Byte();\r
-                       uint8_t LEDMask   = LEDS_LED2;\r
-                       \r
-                       if (LEDStatus & 0x01) // NUM Lock\r
-                         LEDMask |= LEDS_LED1;\r
-                       \r
-                       if (LEDStatus & 0x02) // CAPS Lock\r
-                         LEDMask |= LEDS_LED3;\r
+               /* Write Keyboard Report Data */\r
+               Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
+\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
 \r
-                       if (LEDStatus & 0x04) // SCROLL Lock\r
-                         LEDMask |= LEDS_LED4;\r
+               /* Clear the report data afterwards */\r
+               memset(&KeyboardReportData, 0, sizeof(KeyboardReportData));\r
+       }\r
+\r
+       /* Select the Keyboard LED Report Endpoint */\r
+       Endpoint_SelectEndpoint(KEYBOARD_OUT_EPNUM);\r
+\r
+       /* Check if Keyboard LED Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {               \r
+               /* Read in the LED report from the host */\r
+               uint8_t LEDStatus = Endpoint_Read_Byte();\r
+               uint8_t LEDMask   = LEDS_LED2;\r
+               \r
+               if (LEDStatus & 0x01) // NUM Lock\r
+                 LEDMask |= LEDS_LED1;\r
+               \r
+               if (LEDStatus & 0x02) // CAPS Lock\r
+                 LEDMask |= LEDS_LED3;\r
 \r
-                       /* Set the status LEDs to the current Keyboard LED status */\r
-                       LEDs_SetAllLEDs(LEDMask);\r
+               if (LEDStatus & 0x04) // SCROLL Lock\r
+                 LEDMask |= LEDS_LED4;\r
 \r
-                       /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
-                       Endpoint_ClearOUT();\r
-               }\r
+               /* Set the status LEDs to the current Keyboard LED status */\r
+               LEDs_SetAllLEDs(LEDMask);\r
+\r
+               /* Handshake the OUT Endpoint - clear endpoint and ready for next report */\r
+               Endpoint_ClearOUT();\r
        }\r
 }\r
 \r
@@ -283,6 +285,10 @@ void Mouse_HID_Task(void)
 {\r
        uint8_t JoyStatus_LCL = Joystick_GetStatus();\r
 \r
+       /* Device must be connected and configured for the task to run */\r
+       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+         return;\r
+\r
        /* Check if board button is pressed, if so mouse mode enabled */\r
        if (Buttons_GetStatus() & BUTTONS_BUTTON1)\r
        {\r
@@ -300,23 +306,19 @@ void Mouse_HID_Task(void)
                  MouseReportData.Button  = (1 << 0);\r
        }\r
 \r
-       /* Check if the USB system is connected to a host and report protocol mode is enabled */\r
-       if (USB_IsConnected)\r
-       {\r
-               /* Select the Mouse Report Endpoint */\r
-               Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
+       /* Select the Mouse Report Endpoint */\r
+       Endpoint_SelectEndpoint(MOUSE_IN_EPNUM);\r
 \r
-               /* Check if Mouse Endpoint Ready for Read/Write */\r
-               if (Endpoint_IsReadWriteAllowed())\r
-               {\r
-                       /* Write Mouse Report Data */\r
-                       Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
+       /* Check if Mouse Endpoint Ready for Read/Write */\r
+       if (Endpoint_IsReadWriteAllowed())\r
+       {\r
+               /* Write Mouse Report Data */\r
+               Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));\r
 \r
-                       /* Finalize the stream transfer to send the last packet */\r
-                       Endpoint_ClearIN();\r
+               /* Finalize the stream transfer to send the last packet */\r
+               Endpoint_ClearIN();\r
 \r
-                       /* Clear the report data afterwards */\r
-                       memset(&MouseReportData, 0, sizeof(MouseReportData));\r
-               }\r
+               /* Clear the report data afterwards */\r
+               memset(&MouseReportData, 0, sizeof(MouseReportData));\r
        }\r
 }\r