Skip to content

Commit e35ebf1

Browse files
authored
Add support to Print::printf for printing from flash (esp8266#2266)
* Add support to Print::printf for printing from flash
1 parent 217ba9e commit e35ebf1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cores/esp8266/Print.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ size_t Print::printf(const char *format, ...) {
6363
return len;
6464
}
6565

66+
size_t Print::printf_P(PGM_P format, ...) {
67+
va_list arg;
68+
va_start(arg, format);
69+
char temp[64];
70+
char* buffer = temp;
71+
size_t len = vsnprintf_P(temp, sizeof(temp), format, arg);
72+
va_end(arg);
73+
if (len > sizeof(temp) - 1) {
74+
buffer = new char[len + 1];
75+
if (!buffer) {
76+
return 0;
77+
}
78+
va_start(arg, format);
79+
vsnprintf_P(buffer, len + 1, format, arg);
80+
va_end(arg);
81+
}
82+
len = write((const uint8_t*) buffer, len);
83+
if (buffer != temp) {
84+
delete[] buffer;
85+
}
86+
return len;
87+
}
88+
6689
size_t Print::print(const __FlashStringHelper *ifsh) {
6790
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
6891

cores/esp8266/Print.h

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class Print {
6464
}
6565

6666
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
67+
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
6768
size_t print(const __FlashStringHelper *);
6869
size_t print(const String &);
6970
size_t print(const char[]);

0 commit comments

Comments
 (0)