mbed library sources

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RawSerial.cpp Source File

RawSerial.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "RawSerial.h"
00017 #include "wait_api.h"
00018 #include <cstdarg>
00019 
00020 #if DEVICE_SERIAL
00021 
00022 #define STRING_STACK_LIMIT    120
00023 
00024 namespace mbed {
00025 
00026 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
00027 }
00028 
00029 int RawSerial::getc() {
00030     return _base_getc();
00031 }
00032 
00033 int RawSerial::putc(int c) {
00034     return _base_putc(c);
00035 }
00036 
00037 int RawSerial::puts(const char *str) {
00038     while (*str)
00039         putc(*str ++);
00040     return 0;
00041 }
00042 
00043 // Experimental support for printf in RawSerial. No Stream inheritance
00044 // means we can't call printf() directly, so we use sprintf() instead.
00045 // We only call malloc() for the sprintf() buffer if the buffer
00046 // length is above a certain threshold, otherwise we use just the stack.
00047 int RawSerial::printf(const char *format, ...) {
00048     std::va_list arg;
00049     va_start(arg, format);
00050     int len = vsnprintf(NULL, 0, format, arg);
00051     if (len < STRING_STACK_LIMIT) {
00052         char temp[STRING_STACK_LIMIT];
00053         vsprintf(temp, format, arg);
00054         puts(temp);
00055     } else {
00056         char *temp = new char[len + 1];
00057         vsprintf(temp, format, arg);
00058         puts(temp);
00059         delete[] temp;
00060     }
00061     va_end(arg);
00062     return len;
00063 }
00064 
00065 } // namespace mbed
00066 
00067 #endif