]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/common/RawSerial.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / common / RawSerial.cpp
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "RawSerial.h"
17 #include "wait_api.h"
18 #include <cstdarg>
19
20 #if DEVICE_SERIAL
21
22 #define STRING_STACK_LIMIT    120
23
24 namespace mbed {
25
26 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
27 }
28
29 int RawSerial::getc() {
30     return _base_getc();
31 }
32
33 int RawSerial::putc(int c) {
34     return _base_putc(c);
35 }
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
65 } // namespace mbed
66
67 #endif