diff options
Diffstat (limited to 'common/mbed/xprintf.cpp')
| -rw-r--r-- | common/mbed/xprintf.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/common/mbed/xprintf.cpp b/common/mbed/xprintf.cpp new file mode 100644 index 000000000..4342b79f8 --- /dev/null +++ b/common/mbed/xprintf.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #include <cstdarg> | ||
| 2 | //#include <stdarg.h> | ||
| 3 | #include "mbed.h" | ||
| 4 | #include "mbed/xprintf.h" | ||
| 5 | |||
| 6 | |||
| 7 | #define STRING_STACK_LIMIT 120 | ||
| 8 | |||
| 9 | /* mbed Serial */ | ||
| 10 | Serial ser(UART_TX, UART_RX); | ||
| 11 | |||
| 12 | /* TODO: Need small implementation for embedded */ | ||
| 13 | int xprintf(const char* format, ...) | ||
| 14 | { | ||
| 15 | /* copy from mbed/common/RawSerial.cpp */ | ||
| 16 | std::va_list arg; | ||
| 17 | va_start(arg, format); | ||
| 18 | int len = vsnprintf(NULL, 0, format, arg); | ||
| 19 | if (len < STRING_STACK_LIMIT) { | ||
| 20 | char temp[STRING_STACK_LIMIT]; | ||
| 21 | vsprintf(temp, format, arg); | ||
| 22 | ser.puts(temp); | ||
| 23 | } else { | ||
| 24 | char *temp = new char[len + 1]; | ||
| 25 | vsprintf(temp, format, arg); | ||
| 26 | ser.puts(temp); | ||
| 27 | delete[] temp; | ||
| 28 | } | ||
| 29 | va_end(arg); | ||
| 30 | return len; | ||
| 31 | |||
| 32 | /* Fail: __builtin_va_arg_pack? | ||
| 33 | * https://gcc.gnu.org/onlinedocs/gcc-4.3.5/gcc/Constructing-Calls.html#Constructing-Calls | ||
| 34 | void *arg = __builtin_apply_args(); | ||
| 35 | void *ret = __builtin_apply((void*)(&(ser.printf)), arg, 100); | ||
| 36 | __builtin_return(ret) | ||
| 37 | */ | ||
| 38 | /* Fail: varargs can not be passed to printf | ||
| 39 | //int r = ser.printf("test %i\r\n", 123); | ||
| 40 | va_list arg; | ||
| 41 | va_start(arg, format); | ||
| 42 | int r = ser.printf(format, arg); | ||
| 43 | va_end(arg); | ||
| 44 | return r; | ||
| 45 | */ | ||
| 46 | } | ||
