Skip to content

Commit

Permalink
Add support to Print::printf for printing from flash (esp8266#2266)
Browse files Browse the repository at this point in the history
* Add support to Print::printf for printing from flash
  • Loading branch information
me-no-dev authored Jul 15, 2016
1 parent 217ba9e commit e35ebf1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cores/esp8266/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PGM_P>(ifsh);

Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand Down

0 comments on commit e35ebf1

Please sign in to comment.