Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add printf method with FlashStringHelper argument #8677

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cores/esp32/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ size_t Print::write(const uint8_t *buffer, size_t size)
return n;
}

size_t Print::printf(const char *format, ...)
size_t Print::vprintf(const char *format, va_list arg)
{
char loc_buf[64];
char * temp = loc_buf;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
va_end(copy);
Expand All @@ -74,6 +72,25 @@ size_t Print::printf(const char *format, ...)
return len;
}

size_t Print::printf(const __FlashStringHelper *ifsh, ...)
{
va_list arg;
va_start(arg, ifsh);
const char * format = (reinterpret_cast<const char *>(ifsh));
size_t ret = vprintf(format, arg);
va_end(arg);
return ret;
}

size_t Print::printf(const char *format, ...)
{
va_list arg;
va_start(arg, format);
size_t ret = vprintf(format, arg);
va_end(arg);
return ret;
}

size_t Print::print(const String &s)
{
return write(s.c_str(), s.length());
Expand Down
4 changes: 4 additions & 0 deletions cores/esp32/Print.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define Print_h

#include <stdint.h>
#include <stdio.h>
#include <stddef.h>

#include "WString.h"
Expand Down Expand Up @@ -72,7 +73,10 @@ class Print
return write((const uint8_t *) buffer, size);
}

size_t vprintf(const char *format, va_list arg);

size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
size_t printf(const __FlashStringHelper *ifsh, ...);

// add availableForWrite to make compatible with Arduino Print.h
// default to zero, meaning "a single write may block"
Expand Down