File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,16 @@ class RawSerial: public SerialBase {
71
71
* @returns The char read from the serial port
72
72
*/
73
73
int getc ();
74
+
75
+ /* * Write a string to the serial port
76
+ *
77
+ * @param str The string to write
78
+ *
79
+ * @returns 0 if the write succeeds, EOF for error
80
+ */
81
+ int puts (const char *str);
82
+
83
+ int printf (const char *format, ...);
74
84
};
75
85
76
86
} // namespace mbed
Original file line number Diff line number Diff line change 15
15
*/
16
16
#include " RawSerial.h"
17
17
#include " wait_api.h"
18
+ #include < cstdarg>
18
19
19
20
#if DEVICE_SERIAL
20
21
22
+ #define STRING_STACK_LIMIT 120
23
+
21
24
namespace mbed {
22
25
23
26
RawSerial::RawSerial (PinName tx, PinName rx) : SerialBase(tx, rx) {
@@ -31,6 +34,34 @@ int RawSerial::putc(int c) {
31
34
return _base_putc (c);
32
35
}
33
36
37
+ int RawSerial::puts (const char *str) {
38
+ while (*str)
39
+ putc (*str ++);
40
+ return 0 ;
41
+ }
42
+
43
+ // Experimental support for printf in RawSerial. No Stream inheritance
44
+ // means we can't call printf() directly, so we use sprintf() instead.
45
+ // We only call malloc() for the sprintf() buffer if the buffer
46
+ // length is above a certain threshold, otherwise we use just the stack.
47
+ int RawSerial::printf (const char *format, ...) {
48
+ std::va_list arg;
49
+ va_start (arg, format);
50
+ int len = vsnprintf (NULL , 0 , format, arg);
51
+ if (len < STRING_STACK_LIMIT) {
52
+ char temp[STRING_STACK_LIMIT];
53
+ vsprintf (temp, format, arg);
54
+ puts (temp);
55
+ } else {
56
+ char *temp = new char [len + 1 ];
57
+ vsprintf (temp, format, arg);
58
+ puts (temp);
59
+ delete[] temp;
60
+ }
61
+ va_end (arg);
62
+ return len;
63
+ }
64
+
34
65
} // namespace mbed
35
66
36
67
#endif
You can’t perform that action at this time.
0 commit comments