Fork of mbed-dev with the NUCLEO-L152RE target modified for use with the STM32L151RB (128 kB flash, 16 kB RAM).
Fork of mbed-dev by
To use this, remove the default "mbed" library and import this one instead. Target must be NUCLEO_L152RE.
common/Stream.cpp@28:a132a70c6c08, 2016-02-11 (annotated)
- Committer:
- Jim Paris
- Date:
- Thu Feb 11 15:22:33 2016 -0500
- Revision:
- 28:a132a70c6c08
- Parent:
- 0:9b334a45a8ff
Merge to get rid of old heads
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "Stream.h" |
bogdanm | 0:9b334a45a8ff | 17 | |
bogdanm | 0:9b334a45a8ff | 18 | #include <cstdarg> |
bogdanm | 0:9b334a45a8ff | 19 | |
bogdanm | 0:9b334a45a8ff | 20 | namespace mbed { |
bogdanm | 0:9b334a45a8ff | 21 | |
bogdanm | 0:9b334a45a8ff | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
bogdanm | 0:9b334a45a8ff | 23 | /* open ourselves */ |
bogdanm | 0:9b334a45a8ff | 24 | char buf[12]; /* :0x12345678 + null byte */ |
bogdanm | 0:9b334a45a8ff | 25 | std::sprintf(buf, ":%p", this); |
bogdanm | 0:9b334a45a8ff | 26 | _file = std::fopen(buf, "w+"); |
bogdanm | 0:9b334a45a8ff | 27 | mbed_set_unbuffered_stream(_file); |
bogdanm | 0:9b334a45a8ff | 28 | } |
bogdanm | 0:9b334a45a8ff | 29 | |
bogdanm | 0:9b334a45a8ff | 30 | Stream::~Stream() { |
bogdanm | 0:9b334a45a8ff | 31 | fclose(_file); |
bogdanm | 0:9b334a45a8ff | 32 | } |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | int Stream::putc(int c) { |
bogdanm | 0:9b334a45a8ff | 35 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 36 | return std::fputc(c, _file); |
bogdanm | 0:9b334a45a8ff | 37 | } |
bogdanm | 0:9b334a45a8ff | 38 | int Stream::puts(const char *s) { |
bogdanm | 0:9b334a45a8ff | 39 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 40 | return std::fputs(s, _file); |
bogdanm | 0:9b334a45a8ff | 41 | } |
bogdanm | 0:9b334a45a8ff | 42 | int Stream::getc() { |
bogdanm | 0:9b334a45a8ff | 43 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 44 | return mbed_getc(_file); |
bogdanm | 0:9b334a45a8ff | 45 | } |
bogdanm | 0:9b334a45a8ff | 46 | char* Stream::gets(char *s, int size) { |
bogdanm | 0:9b334a45a8ff | 47 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 48 | return mbed_gets(s,size,_file); |
bogdanm | 0:9b334a45a8ff | 49 | } |
bogdanm | 0:9b334a45a8ff | 50 | |
bogdanm | 0:9b334a45a8ff | 51 | int Stream::close() { |
bogdanm | 0:9b334a45a8ff | 52 | return 0; |
bogdanm | 0:9b334a45a8ff | 53 | } |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | ssize_t Stream::write(const void* buffer, size_t length) { |
bogdanm | 0:9b334a45a8ff | 56 | const char* ptr = (const char*)buffer; |
bogdanm | 0:9b334a45a8ff | 57 | const char* end = ptr + length; |
bogdanm | 0:9b334a45a8ff | 58 | while (ptr != end) { |
bogdanm | 0:9b334a45a8ff | 59 | if (_putc(*ptr++) == EOF) { |
bogdanm | 0:9b334a45a8ff | 60 | break; |
bogdanm | 0:9b334a45a8ff | 61 | } |
bogdanm | 0:9b334a45a8ff | 62 | } |
bogdanm | 0:9b334a45a8ff | 63 | return ptr - (const char*)buffer; |
bogdanm | 0:9b334a45a8ff | 64 | } |
bogdanm | 0:9b334a45a8ff | 65 | |
bogdanm | 0:9b334a45a8ff | 66 | ssize_t Stream::read(void* buffer, size_t length) { |
bogdanm | 0:9b334a45a8ff | 67 | char* ptr = (char*)buffer; |
bogdanm | 0:9b334a45a8ff | 68 | char* end = ptr + length; |
bogdanm | 0:9b334a45a8ff | 69 | while (ptr != end) { |
bogdanm | 0:9b334a45a8ff | 70 | int c = _getc(); |
bogdanm | 0:9b334a45a8ff | 71 | if (c==EOF) break; |
bogdanm | 0:9b334a45a8ff | 72 | *ptr++ = c; |
bogdanm | 0:9b334a45a8ff | 73 | } |
bogdanm | 0:9b334a45a8ff | 74 | return ptr - (const char*)buffer; |
bogdanm | 0:9b334a45a8ff | 75 | } |
bogdanm | 0:9b334a45a8ff | 76 | |
bogdanm | 0:9b334a45a8ff | 77 | off_t Stream::lseek(off_t offset, int whence) { |
bogdanm | 0:9b334a45a8ff | 78 | return 0; |
bogdanm | 0:9b334a45a8ff | 79 | } |
bogdanm | 0:9b334a45a8ff | 80 | |
bogdanm | 0:9b334a45a8ff | 81 | int Stream::isatty() { |
bogdanm | 0:9b334a45a8ff | 82 | return 0; |
bogdanm | 0:9b334a45a8ff | 83 | } |
bogdanm | 0:9b334a45a8ff | 84 | |
bogdanm | 0:9b334a45a8ff | 85 | int Stream::fsync() { |
bogdanm | 0:9b334a45a8ff | 86 | return 0; |
bogdanm | 0:9b334a45a8ff | 87 | } |
bogdanm | 0:9b334a45a8ff | 88 | |
bogdanm | 0:9b334a45a8ff | 89 | off_t Stream::flen() { |
bogdanm | 0:9b334a45a8ff | 90 | return 0; |
bogdanm | 0:9b334a45a8ff | 91 | } |
bogdanm | 0:9b334a45a8ff | 92 | |
bogdanm | 0:9b334a45a8ff | 93 | int Stream::printf(const char* format, ...) { |
bogdanm | 0:9b334a45a8ff | 94 | std::va_list arg; |
bogdanm | 0:9b334a45a8ff | 95 | va_start(arg, format); |
bogdanm | 0:9b334a45a8ff | 96 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 97 | int r = vfprintf(_file, format, arg); |
bogdanm | 0:9b334a45a8ff | 98 | va_end(arg); |
bogdanm | 0:9b334a45a8ff | 99 | return r; |
bogdanm | 0:9b334a45a8ff | 100 | } |
bogdanm | 0:9b334a45a8ff | 101 | |
bogdanm | 0:9b334a45a8ff | 102 | int Stream::scanf(const char* format, ...) { |
bogdanm | 0:9b334a45a8ff | 103 | std::va_list arg; |
bogdanm | 0:9b334a45a8ff | 104 | va_start(arg, format); |
bogdanm | 0:9b334a45a8ff | 105 | fflush(_file); |
bogdanm | 0:9b334a45a8ff | 106 | int r = vfscanf(_file, format, arg); |
bogdanm | 0:9b334a45a8ff | 107 | va_end(arg); |
bogdanm | 0:9b334a45a8ff | 108 | return r; |
bogdanm | 0:9b334a45a8ff | 109 | } |
bogdanm | 0:9b334a45a8ff | 110 | |
bogdanm | 0:9b334a45a8ff | 111 | } // namespace mbed |