Jump to content
43oh

Recommended Posts

Try this. I have limited the string to 64 char so that you won't run into running out of RAM to quickly. If you start to see strange behavior or nothing is printing then reduce the string size further. The snippet came from here: http://playground.arduino.cc/Main/Printf

 


#include <stdarg.h>
void _printf(const char *fmt, ... ){
        char tmp[64]; // resulting string limited to 64 chars
        va_list args;
        va_start (args, fmt );
        vsnprintf(tmp, 64, fmt, args);
        va_end (args);
        Serial.print(tmp);
}

void setup() {
  Serial.begin(9600);
  _printf("%s", "Hello world");
  _printf("%s\n", "Hello world"); // with line break
  unsigned long a=0xFFFFFFFF;
  _printf("Decimal a: %l\nDecimal unsigned a: %lu\n", a, a); 
  _printf("Hex a: %x\n", a);
}

void loop() {

}


Link to post
Share on other sites

Or you could just override the putchar() c function and then you don't have to use a buffer.

 

-rick

extern "C" {
  int putchar(int c) {
    Serial.write((uint8_t)c);
    return c;
  }
}

void setup() {
  Serial.begin(9600);
  
  printf("%s", "This is a really long string that would blow out an\n"
               "an arbitrarily sized buffer on the stack, but this\n"
               "should work just fine\n"
               "Lorem Ipsum is simply dummy text of the printing and\n"
               "typesetting industry. Lorem Ipsum has been the industry's\n"
               "standard dummy text ever since the 1500s, when an unknown\n"
               "printer took a galley of type and scrambled it to make a\n"
               "type specimen book. It has survived not only five centuries,\n"
               "but also the leap into electronic typesetting, remaining\n"
               "essentially unchanged. It was popularised in the 1960s with\n"
               "the release of Letraset sheets containing Lorem Ipsum passages,\n"
               "and more recently with desktop publishing software like Aldus\n"
               "PageMaker including versions of Lorem Ipsum.\n\n"
               );
}

static int cnt=0;

void loop() {
  printf("cnt=%-5d hex=0xx\n",cnt,cnt);
  cnt++;
  
  delay(500);
}
Link to post
Share on other sites

The good thing about the putchar method is that you can use it for any output method, for example by calling lcd.write instead of serial.write you can output to an LCD.

 

The drawback of printf is that it comes with a substantial amount of overhead. In most cases where your app might just need to convert a few integers to strings for output to serial, it will be cheaper in terms of code size to provide your own formatting with calls to .write or .print.

For example when I was doing my real-time clock examples, I found that instead of printf("%02d:%02d:%02d",hour,minute,second) it took far less code to do:

 

 
void PrintWithLeading0(unsigned char value)
{
  if (value<10) lcd.write('0');
  lcd.print(value);
};
 
and then...
PrintWithLeading0(hour);
lcd.write(':');
PrintWithLeading0(minute);
lcd.write(':');
PrintWithLeading0(second);
 
Link to post
Share on other sites
  • 2 years later...

Just tried out Rick's solution, but got some strange error messages (I am using a Tiva-C launchpad...).

 

The solution was to replace the putchar-definition with

extern "C" {
  int _write(int fd, char * str, int len)
  {
    for (int i = 0; i < len; i++) {
      Serial.write((uint8_t)str[i]);
    }
    return len;
  }
}

Maybe this is useful for s.o. ...

Link to post
Share on other sites

Just tried out Rick's solution, but got some strange error messages (I am using a Tiva-C launchpad...).

 

The solution was to replace the putchar-definition with

 

extern "C" {
  int _write(int fd, char * str, int len)
  {
    for (int i = 0; i < len; i++) {
      Serial.write((uint8_t)str[i]);
    }
    return len;
  }
}
Maybe this is useful for s.o. ...
Yep the newlib C library implementation used on most ARM GCC toolchains has a whole "stdio" skeleton subsystem in place where you plug in your own virtual filesystem so to speak.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...