From 80ebc6718046befaf22568f355b5114f8662b581 Mon Sep 17 00:00:00 2001 From: MCUdude Date: Mon, 18 Nov 2019 15:34:30 +0100 Subject: [PATCH 1/6] Add printf to print class --- cores/arduino/Print.cpp | 10 ++++++++++ cores/arduino/Print.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index b8346c75b6..8e1ddb212d 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -196,6 +196,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) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 05b585ea88..f759814aaf 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -22,6 +22,8 @@ #include #include // for size_t +#include // for printf +#define PRINTF_BUF 80 #include "WString.h" #include "Printable.h" @@ -103,6 +105,8 @@ class Print { void println(uint64_t, uint8_t = DEC); void print(uint64_t, uint8_t = DEC); #endif + + void printf(const char[], ...); }; #endif From 7d48d701d4206a7ee324a84187e6f086f8aa1892 Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 20 Nov 2019 10:25:54 +0100 Subject: [PATCH 2/6] Add support for using the F() macro with printf --- cores/arduino/Print.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 8e1ddb212d..9a07b42682 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -196,9 +196,9 @@ size_t Print::println(const Printable &x) return n; } -void Print::printf(const char format[], ...) +void Print::printf(const char *format, ...) { - char buf[PRINTF_BUF]; + char buf[PRINTF_BUFFER]; va_list ap; va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); @@ -206,6 +206,16 @@ void Print::printf(const char format[], ...) va_end(ap); } +void Print::printf(const __FlashStringHelper *format, ...) +{ + char buf[PRINTF_BUFFER]; + va_list ap; + va_start(ap, format); + vsnprintf(buf, sizeof(buf), (const char *)format, ap); + write(buf); + va_end(ap); +} + // Private Methods ///////////////////////////////////////////////////////////// size_t Print::printNumber(unsigned long n, uint8_t base) From 05cdffefb9d0755175159bdbdcd81cef8563e0c1 Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 20 Nov 2019 10:28:07 +0100 Subject: [PATCH 3/6] Add support for using F() with printf + buffer change It's now possible to change the printf buffer size at compile time --- cores/arduino/Print.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index f759814aaf..79a507c59e 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -23,7 +23,6 @@ #include #include // for size_t #include // for printf -#define PRINTF_BUF 80 #include "WString.h" #include "Printable.h" @@ -33,6 +32,12 @@ #define OCT 8 #define BIN 2 +class __FlashStringHelper; + +#ifndef PRINTF_BUFFER +#define PRINTF_BUFFER 80 +#endif + // uncomment next line to support printing of 64 bit ints. #define SUPPORT_LONGLONG @@ -106,7 +111,8 @@ class Print { void print(uint64_t, uint8_t = DEC); #endif - void printf(const char[], ...); + void printf(const char *format, ...); + void printf(const __FlashStringHelper *format, ...); }; #endif From fd641da77b5a5ad0bc061068f11b2045fa2b8e44 Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 22 Nov 2019 18:19:04 +0100 Subject: [PATCH 4/6] Use vsnprintf_P instead of vsnprintf Co-Authored-By: Frederic Pillon --- cores/arduino/Print.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 9a07b42682..633f24eedd 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -211,7 +211,7 @@ void Print::printf(const __FlashStringHelper *format, ...) char buf[PRINTF_BUFFER]; va_list ap; va_start(ap, format); - vsnprintf(buf, sizeof(buf), (const char *)format, ap); + vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); write(buf); va_end(ap); } From b96bbcd05af9bc2e3033d5e293a06ab8c824f57d Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 22 Nov 2019 18:19:36 +0100 Subject: [PATCH 5/6] Remove __FlashStringHelper class Co-Authored-By: Frederic Pillon --- cores/arduino/Print.h | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 79a507c59e..19f8d7b4fb 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -32,7 +32,6 @@ #define OCT 8 #define BIN 2 -class __FlashStringHelper; #ifndef PRINTF_BUFFER #define PRINTF_BUFFER 80 From 5f13cac78bc0a969405c6c1badccf521f643fbc9 Mon Sep 17 00:00:00 2001 From: Hans Date: Fri, 22 Nov 2019 18:19:55 +0100 Subject: [PATCH 6/6] Remove blank line Co-Authored-By: Frederic Pillon --- cores/arduino/Print.h | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 19f8d7b4fb..19988adb07 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -32,7 +32,6 @@ #define OCT 8 #define BIN 2 - #ifndef PRINTF_BUFFER #define PRINTF_BUFFER 80 #endif