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