![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/platform/Stream.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2006-2013 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | * |
kadonotakashi | 0:8fdf9a60065b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 5 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 6 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 7 | * |
kadonotakashi | 0:8fdf9a60065b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 9 | * |
kadonotakashi | 0:8fdf9a60065b | 10 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 13 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 14 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 15 | */ |
kadonotakashi | 0:8fdf9a60065b | 16 | #include "platform/Stream.h" |
kadonotakashi | 0:8fdf9a60065b | 17 | #include "platform/mbed_error.h" |
kadonotakashi | 0:8fdf9a60065b | 18 | #include <errno.h> |
kadonotakashi | 0:8fdf9a60065b | 19 | |
kadonotakashi | 0:8fdf9a60065b | 20 | namespace mbed { |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) |
kadonotakashi | 0:8fdf9a60065b | 23 | { |
kadonotakashi | 0:8fdf9a60065b | 24 | // No lock needed in constructor |
kadonotakashi | 0:8fdf9a60065b | 25 | /* open ourselves */ |
kadonotakashi | 0:8fdf9a60065b | 26 | _file = fdopen(this, "w+"); |
kadonotakashi | 0:8fdf9a60065b | 27 | // fdopen() will make us buffered because Stream::isatty() |
kadonotakashi | 0:8fdf9a60065b | 28 | // wrongly returns zero which is not being changed for |
kadonotakashi | 0:8fdf9a60065b | 29 | // backward compatibility |
kadonotakashi | 0:8fdf9a60065b | 30 | if (_file) { |
kadonotakashi | 0:8fdf9a60065b | 31 | mbed_set_unbuffered_stream(_file); |
kadonotakashi | 0:8fdf9a60065b | 32 | } else { |
kadonotakashi | 0:8fdf9a60065b | 33 | MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OPEN_FAILED), "Stream obj failure", _file); |
kadonotakashi | 0:8fdf9a60065b | 34 | } |
kadonotakashi | 0:8fdf9a60065b | 35 | } |
kadonotakashi | 0:8fdf9a60065b | 36 | |
kadonotakashi | 0:8fdf9a60065b | 37 | Stream::~Stream() |
kadonotakashi | 0:8fdf9a60065b | 38 | { |
kadonotakashi | 0:8fdf9a60065b | 39 | // No lock can be used in destructor |
kadonotakashi | 0:8fdf9a60065b | 40 | fclose(_file); |
kadonotakashi | 0:8fdf9a60065b | 41 | } |
kadonotakashi | 0:8fdf9a60065b | 42 | |
kadonotakashi | 0:8fdf9a60065b | 43 | int Stream::putc(int c) |
kadonotakashi | 0:8fdf9a60065b | 44 | { |
kadonotakashi | 0:8fdf9a60065b | 45 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 46 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 47 | int ret = std::fputc(c, _file); |
kadonotakashi | 0:8fdf9a60065b | 48 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 49 | return ret; |
kadonotakashi | 0:8fdf9a60065b | 50 | } |
kadonotakashi | 0:8fdf9a60065b | 51 | int Stream::puts(const char *s) |
kadonotakashi | 0:8fdf9a60065b | 52 | { |
kadonotakashi | 0:8fdf9a60065b | 53 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 54 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 55 | int ret = std::fputs(s, _file); |
kadonotakashi | 0:8fdf9a60065b | 56 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 57 | return ret; |
kadonotakashi | 0:8fdf9a60065b | 58 | } |
kadonotakashi | 0:8fdf9a60065b | 59 | int Stream::getc() |
kadonotakashi | 0:8fdf9a60065b | 60 | { |
kadonotakashi | 0:8fdf9a60065b | 61 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 62 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 63 | int ret = mbed_getc(_file); |
kadonotakashi | 0:8fdf9a60065b | 64 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 65 | return ret; |
kadonotakashi | 0:8fdf9a60065b | 66 | } |
kadonotakashi | 0:8fdf9a60065b | 67 | char *Stream::gets(char *s, int size) |
kadonotakashi | 0:8fdf9a60065b | 68 | { |
kadonotakashi | 0:8fdf9a60065b | 69 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 70 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 71 | char *ret = mbed_gets(s, size, _file); |
kadonotakashi | 0:8fdf9a60065b | 72 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 73 | return ret; |
kadonotakashi | 0:8fdf9a60065b | 74 | } |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | int Stream::close() |
kadonotakashi | 0:8fdf9a60065b | 77 | { |
kadonotakashi | 0:8fdf9a60065b | 78 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 79 | } |
kadonotakashi | 0:8fdf9a60065b | 80 | |
kadonotakashi | 0:8fdf9a60065b | 81 | ssize_t Stream::write(const void *buffer, size_t length) |
kadonotakashi | 0:8fdf9a60065b | 82 | { |
kadonotakashi | 0:8fdf9a60065b | 83 | const char *ptr = (const char *)buffer; |
kadonotakashi | 0:8fdf9a60065b | 84 | const char *end = ptr + length; |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 87 | while (ptr != end) { |
kadonotakashi | 0:8fdf9a60065b | 88 | if (_putc(*ptr++) == EOF) { |
kadonotakashi | 0:8fdf9a60065b | 89 | break; |
kadonotakashi | 0:8fdf9a60065b | 90 | } |
kadonotakashi | 0:8fdf9a60065b | 91 | } |
kadonotakashi | 0:8fdf9a60065b | 92 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 93 | |
kadonotakashi | 0:8fdf9a60065b | 94 | return ptr - (const char *)buffer; |
kadonotakashi | 0:8fdf9a60065b | 95 | } |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | ssize_t Stream::read(void *buffer, size_t length) |
kadonotakashi | 0:8fdf9a60065b | 98 | { |
kadonotakashi | 0:8fdf9a60065b | 99 | char *ptr = (char *)buffer; |
kadonotakashi | 0:8fdf9a60065b | 100 | char *end = ptr + length; |
kadonotakashi | 0:8fdf9a60065b | 101 | |
kadonotakashi | 0:8fdf9a60065b | 102 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 103 | while (ptr != end) { |
kadonotakashi | 0:8fdf9a60065b | 104 | int c = _getc(); |
kadonotakashi | 0:8fdf9a60065b | 105 | if (c == EOF) { |
kadonotakashi | 0:8fdf9a60065b | 106 | break; |
kadonotakashi | 0:8fdf9a60065b | 107 | } |
kadonotakashi | 0:8fdf9a60065b | 108 | *ptr++ = c; |
kadonotakashi | 0:8fdf9a60065b | 109 | } |
kadonotakashi | 0:8fdf9a60065b | 110 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | return ptr - (const char *)buffer; |
kadonotakashi | 0:8fdf9a60065b | 113 | } |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | off_t Stream::seek(off_t offset, int whence) |
kadonotakashi | 0:8fdf9a60065b | 116 | { |
kadonotakashi | 0:8fdf9a60065b | 117 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 118 | } |
kadonotakashi | 0:8fdf9a60065b | 119 | |
kadonotakashi | 0:8fdf9a60065b | 120 | off_t Stream::tell() |
kadonotakashi | 0:8fdf9a60065b | 121 | { |
kadonotakashi | 0:8fdf9a60065b | 122 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 123 | } |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | void Stream::rewind() |
kadonotakashi | 0:8fdf9a60065b | 126 | { |
kadonotakashi | 0:8fdf9a60065b | 127 | } |
kadonotakashi | 0:8fdf9a60065b | 128 | |
kadonotakashi | 0:8fdf9a60065b | 129 | int Stream::isatty() |
kadonotakashi | 0:8fdf9a60065b | 130 | { |
kadonotakashi | 0:8fdf9a60065b | 131 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 132 | } |
kadonotakashi | 0:8fdf9a60065b | 133 | |
kadonotakashi | 0:8fdf9a60065b | 134 | int Stream::sync() |
kadonotakashi | 0:8fdf9a60065b | 135 | { |
kadonotakashi | 0:8fdf9a60065b | 136 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 137 | } |
kadonotakashi | 0:8fdf9a60065b | 138 | |
kadonotakashi | 0:8fdf9a60065b | 139 | off_t Stream::size() |
kadonotakashi | 0:8fdf9a60065b | 140 | { |
kadonotakashi | 0:8fdf9a60065b | 141 | return 0; |
kadonotakashi | 0:8fdf9a60065b | 142 | } |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | int Stream::printf(const char *format, ...) |
kadonotakashi | 0:8fdf9a60065b | 145 | { |
kadonotakashi | 0:8fdf9a60065b | 146 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 147 | std::va_list arg; |
kadonotakashi | 0:8fdf9a60065b | 148 | va_start(arg, format); |
kadonotakashi | 0:8fdf9a60065b | 149 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 150 | int r = vfprintf(_file, format, arg); |
kadonotakashi | 0:8fdf9a60065b | 151 | va_end(arg); |
kadonotakashi | 0:8fdf9a60065b | 152 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 153 | return r; |
kadonotakashi | 0:8fdf9a60065b | 154 | } |
kadonotakashi | 0:8fdf9a60065b | 155 | |
kadonotakashi | 0:8fdf9a60065b | 156 | int Stream::scanf(const char *format, ...) |
kadonotakashi | 0:8fdf9a60065b | 157 | { |
kadonotakashi | 0:8fdf9a60065b | 158 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 159 | std::va_list arg; |
kadonotakashi | 0:8fdf9a60065b | 160 | va_start(arg, format); |
kadonotakashi | 0:8fdf9a60065b | 161 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 162 | int r = vfscanf(_file, format, arg); |
kadonotakashi | 0:8fdf9a60065b | 163 | va_end(arg); |
kadonotakashi | 0:8fdf9a60065b | 164 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 165 | return r; |
kadonotakashi | 0:8fdf9a60065b | 166 | } |
kadonotakashi | 0:8fdf9a60065b | 167 | |
kadonotakashi | 0:8fdf9a60065b | 168 | int Stream::vprintf(const char *format, std::va_list args) |
kadonotakashi | 0:8fdf9a60065b | 169 | { |
kadonotakashi | 0:8fdf9a60065b | 170 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 171 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 172 | int r = vfprintf(_file, format, args); |
kadonotakashi | 0:8fdf9a60065b | 173 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 174 | return r; |
kadonotakashi | 0:8fdf9a60065b | 175 | } |
kadonotakashi | 0:8fdf9a60065b | 176 | |
kadonotakashi | 0:8fdf9a60065b | 177 | int Stream::vscanf(const char *format, std::va_list args) |
kadonotakashi | 0:8fdf9a60065b | 178 | { |
kadonotakashi | 0:8fdf9a60065b | 179 | lock(); |
kadonotakashi | 0:8fdf9a60065b | 180 | fflush(_file); |
kadonotakashi | 0:8fdf9a60065b | 181 | int r = vfscanf(_file, format, args); |
kadonotakashi | 0:8fdf9a60065b | 182 | unlock(); |
kadonotakashi | 0:8fdf9a60065b | 183 | return r; |
kadonotakashi | 0:8fdf9a60065b | 184 | } |
kadonotakashi | 0:8fdf9a60065b | 185 | |
kadonotakashi | 0:8fdf9a60065b | 186 | } // namespace mbed |