From 4cde6987904f91049d72493e84966977be4c4b84 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Mon, 11 Jul 2016 23:23:13 +0300 Subject: [PATCH 1/2] Add support to Print::printf for printing from flash --- cores/esp8266/Print.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ cores/esp8266/Print.h | 2 ++ 2 files changed, 49 insertions(+) diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp index 5b481fd403..dcb47d0d02 100644 --- a/cores/esp8266/Print.cpp +++ b/cores/esp8266/Print.cpp @@ -63,6 +63,53 @@ size_t Print::printf(const char *format, ...) { return len; } +size_t Print::printf(const __FlashStringHelper * format, ...) { + va_list arg; + va_start(arg, format); + PGM_P p = reinterpret_cast(format); + char temp[64]; + char* buffer = temp; + size_t len = vsnprintf_P(temp, sizeof(temp), p, 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, p, arg); + va_end(arg); + } + len = write((const uint8_t*) buffer, len); + if (buffer != temp) { + delete[] buffer; + } + 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 a0df267c1f..f178ac9fb8 100644 --- a/cores/esp8266/Print.h +++ b/cores/esp8266/Print.h @@ -64,6 +64,8 @@ class Print { } size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); + size_t printf(const __FlashStringHelper * format, ...); + 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[]); From 19b67a09eb034f48265f963154e80aff47b02805 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Tue, 12 Jul 2016 12:49:35 +0300 Subject: [PATCH 2/2] Remove flash string helper printf As discussed, such functionality should not be expected from printf --- cores/esp8266/Print.cpp | 24 ------------------------ cores/esp8266/Print.h | 1 - 2 files changed, 25 deletions(-) diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp index dcb47d0d02..0c3d362ab7 100644 --- a/cores/esp8266/Print.cpp +++ b/cores/esp8266/Print.cpp @@ -63,30 +63,6 @@ size_t Print::printf(const char *format, ...) { return len; } -size_t Print::printf(const __FlashStringHelper * format, ...) { - va_list arg; - va_start(arg, format); - PGM_P p = reinterpret_cast(format); - char temp[64]; - char* buffer = temp; - size_t len = vsnprintf_P(temp, sizeof(temp), p, 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, p, arg); - va_end(arg); - } - len = write((const uint8_t*) buffer, len); - if (buffer != temp) { - delete[] buffer; - } - return len; -} - size_t Print::printf_P(PGM_P format, ...) { va_list arg; va_start(arg, format); diff --git a/cores/esp8266/Print.h b/cores/esp8266/Print.h index f178ac9fb8..69f5ad6216 100644 --- a/cores/esp8266/Print.h +++ b/cores/esp8266/Print.h @@ -64,7 +64,6 @@ class Print { } size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); - size_t printf(const __FlashStringHelper * format, ...); size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3))); size_t print(const __FlashStringHelper *); size_t print(const String &);