Added new stream creation function to the CDC Class drivers, to easily make standard...
[pub/USBasp.git] / Demos / Device / ClassDriver / CDC / CDC.c
index 90cbaf0..5d74cb1 100644 (file)
@@ -60,27 +60,10 @@ USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
                        },\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
+/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be\r
+ *  used like any regular character stream in the C APIs\r
  */\r
-\r
-static int CDC_putchar(char c, FILE *stream)\r
-{\r
-       CDC_Device_SendByte(&VirtualSerial_CDC_Interface, c);\r
-       return 0;\r
-}\r
-\r
-static int CDC_getchar(FILE *stream)\r
-{\r
-       if (!(CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)))\r
-         return -1;\r
-\r
-       return CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);\r
-}\r
-\r
-static FILE USBSerial = FDEV_SETUP_STREAM(CDC_putchar, CDC_getchar, _FDEV_SETUP_RW);\r
-#endif\r
+static FILE USBSerialStream;\r
 \r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
  *  setup of all components and the main program loop.\r
@@ -89,6 +72,9 @@ int main(void)
 {\r
        SetupHardware();\r
        \r
+       /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */\r
+       CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);\r
+\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
        for (;;)\r
@@ -143,8 +129,12 @@ void CheckJoystickMovement(void)
        if ((ReportString != NULL) && (ActionSent == false))\r
        {\r
                ActionSent = true;\r
-               \r
-               CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));                \r
+\r
+               // Write the string to the virtual COM port via the created character stream\r
+               fputs(ReportString, &USBSerialStream);\r
+\r
+               // Alternatively, without the stream:\r
+               // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));\r
        }\r
 }\r
 \r