From c1fe7c1374a343bdd2d7a7e2be91aa96fbcf2a77 Mon Sep 17 00:00:00 2001 From: tobozo Date: Wed, 26 Oct 2022 11:41:49 +0200 Subject: [PATCH 1/2] added printf header --- cores/arduino/Print.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index 0097cc11d..72bd65f6c 100644 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -57,6 +57,8 @@ class Print size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); } + + size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3))); // default to zero, meaning "a single write may block" // should be overridden by subclasses with buffering From 4ed591e5c00e310d086e94757848160fd53e66cc Mon Sep 17 00:00:00 2001 From: tobozo Date: Wed, 26 Oct 2022 11:45:31 +0200 Subject: [PATCH 2/2] added printf --- cores/arduino/Print.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 1e4c99a65..d8a168053 100644 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -41,6 +41,36 @@ size_t Print::write(const uint8_t *buffer, size_t size) return n; } +size_t Print::printf(const char *format, ...) +{ + 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); + if(len < 0) { + va_end(arg); + return 0; + }; + if((unsigned)len >= sizeof(loc_buf)){ + temp = (char*) malloc(len+1); + if(temp == NULL) { + va_end(arg); + return 0; + } + len = vsnprintf(temp, len+1, format, arg); + } + va_end(arg); + len = write((uint8_t*)temp, len); + if(temp != loc_buf){ + free(temp); + } + return len; +} + size_t Print::print(const __FlashStringHelper *ifsh) { PGM_P p = reinterpret_cast(ifsh);