From e35ebf137d8ee0d5c7949e9c888db0e8e25d0a1c Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Fri, 15 Jul 2016 10:15:44 +0300 Subject: [PATCH] Add support to Print::printf for printing from flash (#2266) * Add support to Print::printf for printing from flash --- cores/esp8266/Print.cpp | 23 +++++++++++++++++++++++ cores/esp8266/Print.h | 1 + 2 files changed, 24 insertions(+) diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp index 5b481fd40..0c3d362ab 100644 --- a/cores/esp8266/Print.cpp +++ b/cores/esp8266/Print.cpp @@ -63,6 +63,29 @@ size_t Print::printf(const char *format, ...) { return len; } +size_t Print::printf_P(PGM_P format, ...) { + va_list arg; + va_start(arg, format); + char temp[64]; + char* buffer = temp; + size_t len = vsnprintf_P(temp, sizeof(temp), format, arg); + va_end(arg); + if (len > sizeof(temp) - 1) { + buffer = new char[len + 1]; + if (!buffer) { + return 0; + } + va_start(arg, format); + vsnprintf_P(buffer, len + 1, format, arg); + va_end(arg); + } + len = write((const uint8_t*) buffer, len); + if (buffer != temp) { + delete[] buffer; + } + return len; +} + size_t Print::print(const __FlashStringHelper *ifsh) { PGM_P p = reinterpret_cast(ifsh); diff --git a/cores/esp8266/Print.h b/cores/esp8266/Print.h index a0df267c1..69f5ad621 100644 --- a/cores/esp8266/Print.h +++ b/cores/esp8266/Print.h @@ -64,6 +64,7 @@ class Print { } size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); + size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3))); size_t print(const __FlashStringHelper *); size_t print(const String &); size_t print(const char[]);