Skip to content

Commit 0b9a591

Browse files
MCUdudefpistm
authored andcommitted
Add printf to print class (stm32duino#780)
Based on Adafruits printf implementation in https://github.com/adafruit/ArduinoCore-samd
1 parent 4be877d commit 0b9a591

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

cores/arduino/Print.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ size_t Print::println(const Printable &x)
196196
return n;
197197
}
198198

199+
void Print::printf(const char *format, ...)
200+
{
201+
char buf[PRINTF_BUFFER];
202+
va_list ap;
203+
va_start(ap, format);
204+
vsnprintf(buf, sizeof(buf), format, ap);
205+
write(buf);
206+
va_end(ap);
207+
}
208+
209+
void Print::printf(const __FlashStringHelper *format, ...)
210+
{
211+
char buf[PRINTF_BUFFER];
212+
va_list ap;
213+
va_start(ap, format);
214+
vsnprintf_P(buf, sizeof(buf), (const char *)format, ap);
215+
write(buf);
216+
va_end(ap);
217+
}
218+
199219
// Private Methods /////////////////////////////////////////////////////////////
200220

201221
size_t Print::printNumber(unsigned long n, uint8_t base)

cores/arduino/Print.h

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <inttypes.h>
2424
#include <stdio.h> // for size_t
25+
#include <stdarg.h> // for printf
2526

2627
#include "WString.h"
2728
#include "Printable.h"
@@ -31,6 +32,10 @@
3132
#define OCT 8
3233
#define BIN 2
3334

35+
#ifndef PRINTF_BUFFER
36+
#define PRINTF_BUFFER 80
37+
#endif
38+
3439
// uncomment next line to support printing of 64 bit ints.
3540
#define SUPPORT_LONGLONG
3641

@@ -103,6 +108,9 @@ class Print {
103108
void println(uint64_t, uint8_t = DEC);
104109
void print(uint64_t, uint8_t = DEC);
105110
#endif
111+
112+
void printf(const char *format, ...);
113+
void printf(const __FlashStringHelper *format, ...);
106114
};
107115

108116
#endif

0 commit comments

Comments
 (0)