Skip to content

Commit f35e28f

Browse files
authored
Update Print.cpp
Signed-off-by: tobozo <[email protected]>
1 parent fec71c1 commit f35e28f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

cores/arduino/Print.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdlib.h>
2020
#include <stdio.h>
2121
#include <string.h>
22+
#include <cstdarg>
2223
#include <math.h>
2324
#include <limits.h>
2425
#include "Arduino.h"
@@ -39,6 +40,36 @@ size_t Print::write(const uint8_t *buffer, size_t size)
3940
return n;
4041
}
4142

43+
size_t Print::printf(const char *format, ...)
44+
{
45+
char loc_buf[64];
46+
char * temp = loc_buf;
47+
va_list arg;
48+
va_list copy;
49+
va_start(arg, format);
50+
va_copy(copy, arg);
51+
int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
52+
va_end(copy);
53+
if(len < 0) {
54+
va_end(arg);
55+
return 0;
56+
};
57+
if(len >= sizeof(loc_buf)){
58+
temp = (char*) malloc(len+1);
59+
if(temp == NULL) {
60+
va_end(arg);
61+
return 0;
62+
}
63+
len = vsnprintf(temp, len+1, format, arg);
64+
}
65+
va_end(arg);
66+
len = write((uint8_t*)temp, len);
67+
if(temp != loc_buf){
68+
free(temp);
69+
}
70+
return len;
71+
}
72+
4273
size_t Print::print(const __FlashStringHelper *ifsh)
4374
{
4475
return print(reinterpret_cast<const char *>(ifsh));

0 commit comments

Comments
 (0)