Skip to content

Added printf to Print class to have Serial.printf() functionality #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ size_t Print::println(const Printable& x)
return n;
}

void Print::printf(const char format[], ...)
{
char buf[PRINTF_BUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap);
write(buf);
va_end(ap);
}

// Private Methods /////////////////////////////////////////////////////////////

size_t Print::printNumber(unsigned long n, uint8_t base)
Expand Down
4 changes: 4 additions & 0 deletions cores/arduino/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <inttypes.h>
#include <stdio.h> // for size_t
#include <stdarg.h> // for printf
#define PRINTF_BUF 80

#include "WString.h"
#include "Printable.h"
Expand Down Expand Up @@ -86,6 +88,8 @@ class Print
size_t println(const Printable&);
size_t println(void);

void printf(const char[], ...);

virtual void flush() { /* Empty implementation for backward compatibility */ }
};

Expand Down