Make HID device class driver reselect the correct endpoint after the user callbacks...
[pub/USBasp.git] / Demos / Device / LowLevel / CDC / CDC.c
index 9ee744e..444dc01 100644 (file)
@@ -36,7 +36,6 @@
 \r
 #include "CDC.h"\r
 \r
 \r
 #include "CDC.h"\r
 \r
-/* Globals: */\r
 /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use\r
  *  the physical USART and thus does not use these settings, they must still be retained and returned to the host\r
  *  upon request or the host will assume the device is non-functional.\r
 /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use\r
  *  the physical USART and thus does not use these settings, they must still be retained and returned to the host\r
  *  upon request or the host will assume the device is non-functional.\r
@@ -50,6 +49,7 @@ CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0,
                                    .ParityType  = Parity_None,\r
                                    .DataBits    = 8            };\r
 \r
                                    .ParityType  = Parity_None,\r
                                    .DataBits    = 8            };\r
 \r
+\r
 #if 0\r
 /* NOTE: Here you can set up a standard stream using the created virtual serial port, so that the standard stream functions in\r
  *       <stdio.h> can be used on the virtual serial port (e.g. fprintf(&USBSerial, "Test"); to print a string).\r
 #if 0\r
 /* NOTE: Here you can set up a standard stream using the created virtual serial port, so that the standard stream functions in\r
  *       <stdio.h> can be used on the virtual serial port (e.g. fprintf(&USBSerial, "Test"); to print a string).\r
@@ -62,11 +62,8 @@ static int CDC_putchar(char c, FILE *stream)
        if (!(LineEncoding.BaudRateBPS))\r
          return -1;\r
        \r
        if (!(LineEncoding.BaudRateBPS))\r
          return -1;\r
        \r
-       while (!(Endpoint_IsReadWriteAllowed()))\r
-       {\r
-               if (USB_DeviceState != DEVICE_STATE_Configured)\r
-                 return -1;\r
-       }\r
+       if (Endpoint_WaitUntilReady())\r
+         return -1;\r
 \r
        Endpoint_Write_Byte(c);\r
        Endpoint_ClearIN();\r
 \r
        Endpoint_Write_Byte(c);\r
        Endpoint_ClearIN();\r
@@ -85,11 +82,8 @@ static int CDC_getchar(FILE *stream)
        \r
        for (;;)\r
        {\r
        \r
        for (;;)\r
        {\r
-               while (!(Endpoint_IsReadWriteAllowed()))\r
-               {\r
-                       if (USB_DeviceState != DEVICE_STATE_Configured)\r
-                         return -1;\r
-               }\r
+               if (Endpoint_WaitUntilReady())\r
+                 return -1;\r
        \r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
        \r
                if (!(Endpoint_BytesInEndpoint()))\r
                {\r
@@ -253,14 +247,6 @@ void CDC_Task(void)
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
        char*       ReportString    = NULL;\r
        uint8_t     JoyStatus_LCL   = Joystick_GetStatus();\r
        static bool ActionSent      = false;\r
-       char*       JoystickStrings[] =\r
-                                       {\r
-                                               "Joystick Up\r\n",\r
-                                               "Joystick Down\r\n",\r
-                                               "Joystick Left\r\n",\r
-                                               "Joystick Right\r\n",\r
-                                               "Joystick Pressed\r\n",\r
-                                       };\r
        \r
        /* Device must be connected and configured for the task to run */\r
        if (USB_DeviceState != DEVICE_STATE_Configured)\r
        \r
        /* Device must be connected and configured for the task to run */\r
        if (USB_DeviceState != DEVICE_STATE_Configured)\r
@@ -291,22 +277,20 @@ void CDC_Task(void)
 \r
        /* Determine if a joystick action has occurred */\r
        if (JoyStatus_LCL & JOY_UP)\r
 \r
        /* Determine if a joystick action has occurred */\r
        if (JoyStatus_LCL & JOY_UP)\r
-         ReportString = JoystickStrings[0];\r
+         ReportString = "Joystick Up\r\n";\r
        else if (JoyStatus_LCL & JOY_DOWN)\r
        else if (JoyStatus_LCL & JOY_DOWN)\r
-         ReportString = JoystickStrings[1];\r
+         ReportString = "Joystick Down\r\n";\r
        else if (JoyStatus_LCL & JOY_LEFT)\r
        else if (JoyStatus_LCL & JOY_LEFT)\r
-         ReportString = JoystickStrings[2];\r
+         ReportString = "Joystick Left\r\n";\r
        else if (JoyStatus_LCL & JOY_RIGHT)\r
        else if (JoyStatus_LCL & JOY_RIGHT)\r
-         ReportString = JoystickStrings[3];\r
+         ReportString = "Joystick Right\r\n";\r
        else if (JoyStatus_LCL & JOY_PRESS)\r
        else if (JoyStatus_LCL & JOY_PRESS)\r
-         ReportString = JoystickStrings[4];\r
+         ReportString = "Joystick Pressed\r\n";\r
+       else\r
+         ActionSent = false;\r
 \r
        /* Flag management - Only allow one string to be sent per action */\r
 \r
        /* Flag management - Only allow one string to be sent per action */\r
-       if (ReportString == NULL)\r
-       {\r
-               ActionSent = false;\r
-       }\r
-       else if ((ActionSent == false) && LineEncoding.BaudRateBPS)\r
+       if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)\r
        {\r
                ActionSent = true;\r
 \r
        {\r
                ActionSent = true;\r
 \r
@@ -327,11 +311,7 @@ void CDC_Task(void)
                if (IsFull)\r
                {\r
                        /* Wait until the endpoint is ready for another packet */\r
                if (IsFull)\r
                {\r
                        /* Wait until the endpoint is ready for another packet */\r
-                       while (!(Endpoint_IsINReady()))\r
-                       {\r
-                               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
-                                 return;\r
-                       }\r
+                       Endpoint_WaitUntilReady();\r
                        \r
                        /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                        Endpoint_ClearIN();\r
                        \r
                        /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
                        Endpoint_ClearIN();\r