diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index b8346c75b6..633f24eedd 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -196,6 +196,26 @@ size_t Print::println(const Printable &x) return n; } +void Print::printf(const char *format, ...) +{ + char buf[PRINTF_BUFFER]; + va_list ap; + va_start(ap, format); + vsnprintf(buf, sizeof(buf), format, ap); + write(buf); + va_end(ap); +} + +void Print::printf(const __FlashStringHelper *format, ...) +{ + char buf[PRINTF_BUFFER]; + va_list ap; + va_start(ap, format); + vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); + write(buf); + va_end(ap); +} + // Private Methods ///////////////////////////////////////////////////////////// size_t Print::printNumber(unsigned long n, uint8_t base) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 05b585ea88..19988adb07 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -22,6 +22,7 @@ #include #include // for size_t +#include // for printf #include "WString.h" #include "Printable.h" @@ -31,6 +32,10 @@ #define OCT 8 #define BIN 2 +#ifndef PRINTF_BUFFER +#define PRINTF_BUFFER 80 +#endif + // uncomment next line to support printing of 64 bit ints. #define SUPPORT_LONGLONG @@ -103,6 +108,9 @@ class Print { void println(uint64_t, uint8_t = DEC); void print(uint64_t, uint8_t = DEC); #endif + + void printf(const char *format, ...); + void printf(const __FlashStringHelper *format, ...); }; #endif