CDC.printf

From Pinguino-Wiki
Jump to: navigation, search

Contents

Name

CDC.printf - send string to CDC device

Description

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

Syntax

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;
        }
    else
        CDC.printf("double=%.1f \r\n", f);  // 1 digit after decimal comma    
 
    toggle(USERLED);
    delay(250);
}

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/Pinguino.cc 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("\r\n");
  	CDC.printf("*************************\r\n");
  	CDC.printf("*** CDC printf() demo ***\r\n");
  	CDC.printf("*************************\r\n");
  	CDC.printf("\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);[http://www.example.com link title]
  	CDC.printf("-3: %-4d left justif.\r\n", -3);
  	CDC.printf("-3: %4d right justif.\r\n", -3);
  	CDC.printf("\r\n");
  }  // 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.
Sprintf

CDC.getString        CDC.print        CDC.printf
CDC.println          CDC.read         CDC.write
CDC.getKey

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

lcd.print

Comments

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 ???...
Personal tools