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