From Pinguino
Jump to: navigation, search


CDC.printf - send string to CDC device


This command sends a string to the CDC device - usually USB connected to virtual text terminal.

The Communication Device Class (CDC) is a general-purpose way to enable all types of communications on the Universal Serial Bus (USB). This class makes it possible to connect communication devices such as digital telephones or analog modems, as well as networking devices like Asymmetric Digital Subscriber Line (ADSL, DSL) or Cable modems. While a CDC device enables the implementation of quite complex devices, it can also be used as a very simple method for communication on the USB. For example, a CDC device can appear as a virtual COM port (terminal window), which greatly simplifies application development on the host side.

Since Beta X


CDC.printf ( const uint8 *fmt, ... )

Example 1

// CDC.printf() example for Pinguino 26J50

double f = 1; 

void setup()
    pinMode(USERLED, OUTPUT);

void loop()
    f = f + 0.1;
    if ( f > 10 )
        CDC.printf("double=%.1f \r\n", f);  // 1 digit after decimal comma    
        f = 1;
        CDC.printf("double=%.1f \r\n", f);  // 1 digit after decimal comma    


Example 2

/* +---------------------------------------------------------------------+
   | cdc_printf_test.pde        by Jan Zumwalt        rev 2015.12.17     |
   | Copyright: COPYRIGHT by Jan Zumwalt                                 |
   |            Licensed under the GNU public license.                   |
   | Build    : Olimex Pic32 Micro dev brd w/ IDE (V11)       |
   | Purpose  : This demonstrates the CDC.printf command.                |
   +---------------------------------------------------------------------+  */
  // global variables  
  char *string = "Hello world!";
  char c       = 65;          // ascii "A"
  char keyin   = 0;
  int i        = 333;
  long l       = 186282;      // speed of light - miles/sec
  float f      = 3.14159265;  // not supported on 8bit processors
  void setup()  { // setup, run once
    // nothing to do
  void loop()  {  // main program, loop forever
    CDC.printf("Press Any Key to begin ...\r\n");
  	while(keyin == 0) {
      keyin = CDC.getKey();
    }  // end while
    keyin = 0;
  	CDC.printf("*** CDC printf() demo ***\r\n");
  	CDC.printf("string = %s\r\n", string);
  	CDC.printf("character = \"c\"\r\n", c);
  	CDC.printf("signed char = %d      |  unsigned char = %u\r\n", -c, -c);
  	CDC.printf("signed int  = %d     |  unsigned int  = %u\r\n", -i, -i);
  	CDC.printf("signed long = %d  |  unsigned long = %u\r\n", -l, -l);
  	CDC.printf("justify: \"%-10s\"  |  \"%10s\"\r\n", "left", "right");
  	CDC.printf("dec = [%d] is hex [0x%X], bin [0b%016b]\r\n", i, i, i);
  	CDC.printf("dec = [%d] is oct [%o],\r\n", i);
  	CDC.printf("float = %f\r\n", f);
  	CDC.printf(" 3: %04d zero padded\r\n", 3);
  	CDC.printf(" 3: %-4d left justif.\r\n", 3);
  	CDC.printf(" 3: %4d right justif.\r\n", 3);
  	CDC.printf("-3: %04d zero padded\r\n", -3);[ link title]
  	CDC.printf("-3: %-4d left justif.\r\n", -3);
  	CDC.printf("-3: %4d right justif.\r\n", -3);
  }  // end of loop

Bugs & Limitations

CDC.printf() uses a buffer array of a limited size and if the output if too long the output will appear corrupted and may also result in other data corruption.

For practical purposes the limit of the output is approximately 64 characters.

Printing a formatted float eg CDC.printf("float = %.1f", float); suffers from rounding errors (workaround: use a double instead of a float).

See also

Printf for all supported formats.

CDC.getString        CDC.print        CDC.printf
CDC.println         CDC.write

Serial.getString     Serial.print     Serial.printf
Serial.println      Serial.write



On Linux, check output with sudo cat /dev/ttyACM0
On OS X, check output with cat /dev/cu.usbmodemnnnnnn where n is a number unique to your system.
On Windows, check output with ???...