initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #include "RawSerial.h"
yihui 0:638edba3adf6 17 #include "wait_api.h"
yihui 0:638edba3adf6 18 #include <cstdarg>
yihui 0:638edba3adf6 19
yihui 0:638edba3adf6 20 #if DEVICE_SERIAL
yihui 0:638edba3adf6 21
yihui 0:638edba3adf6 22 #define STRING_STACK_LIMIT 120
yihui 0:638edba3adf6 23
yihui 0:638edba3adf6 24 namespace mbed {
yihui 0:638edba3adf6 25
yihui 0:638edba3adf6 26 RawSerial::RawSerial(PinName tx, PinName rx) : SerialBase(tx, rx) {
yihui 0:638edba3adf6 27 }
yihui 0:638edba3adf6 28
yihui 0:638edba3adf6 29 int RawSerial::getc() {
yihui 0:638edba3adf6 30 return _base_getc();
yihui 0:638edba3adf6 31 }
yihui 0:638edba3adf6 32
yihui 0:638edba3adf6 33 int RawSerial::putc(int c) {
yihui 0:638edba3adf6 34 return _base_putc(c);
yihui 0:638edba3adf6 35 }
yihui 0:638edba3adf6 36
yihui 0:638edba3adf6 37 int RawSerial::puts(const char *str) {
yihui 0:638edba3adf6 38 while (*str)
yihui 0:638edba3adf6 39 putc(*str ++);
yihui 0:638edba3adf6 40 return 0;
yihui 0:638edba3adf6 41 }
yihui 0:638edba3adf6 42
yihui 0:638edba3adf6 43 // Experimental support for printf in RawSerial. No Stream inheritance
yihui 0:638edba3adf6 44 // means we can't call printf() directly, so we use sprintf() instead.
yihui 0:638edba3adf6 45 // We only call malloc() for the sprintf() buffer if the buffer
yihui 0:638edba3adf6 46 // length is above a certain threshold, otherwise we use just the stack.
yihui 0:638edba3adf6 47 int RawSerial::printf(const char *format, ...) {
yihui 0:638edba3adf6 48 std::va_list arg;
yihui 0:638edba3adf6 49 va_start(arg, format);
yihui 0:638edba3adf6 50 int len = vsnprintf(NULL, 0, format, arg);
yihui 0:638edba3adf6 51 if (len < STRING_STACK_LIMIT) {
yihui 0:638edba3adf6 52 char temp[STRING_STACK_LIMIT];
yihui 0:638edba3adf6 53 vsprintf(temp, format, arg);
yihui 0:638edba3adf6 54 puts(temp);
yihui 0:638edba3adf6 55 } else {
yihui 0:638edba3adf6 56 char *temp = new char[len + 1];
yihui 0:638edba3adf6 57 vsprintf(temp, format, arg);
yihui 0:638edba3adf6 58 puts(temp);
yihui 0:638edba3adf6 59 delete[] temp;
yihui 0:638edba3adf6 60 }
yihui 0:638edba3adf6 61 va_end(arg);
yihui 0:638edba3adf6 62 return len;
yihui 0:638edba3adf6 63 }
yihui 0:638edba3adf6 64
yihui 0:638edba3adf6 65 } // namespace mbed
yihui 0:638edba3adf6 66
yihui 0:638edba3adf6 67 #endif