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