1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "RawSerial.h"
22 #define STRING_STACK_LIMIT 120
26 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
29 int RawSerial::getc() {
33 int RawSerial::putc(int c) {
37 int RawSerial::puts(const char *str) {
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, ...) {
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);
56 char *temp = new char[len + 1];
57 vsprintf(temp, format, arg);