5.2.1 - Updated I2C files
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
drivers/Stream.cpp@0:098463de4c5d, 2017-01-25 (annotated)
- Committer:
- group-onsemi
- Date:
- Wed Jan 25 20:34:15 2017 +0000
- Revision:
- 0:098463de4c5d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-onsemi | 0:098463de4c5d | 1 | /* mbed Microcontroller Library |
group-onsemi | 0:098463de4c5d | 2 | * Copyright (c) 2006-2013 ARM Limited |
group-onsemi | 0:098463de4c5d | 3 | * |
group-onsemi | 0:098463de4c5d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
group-onsemi | 0:098463de4c5d | 5 | * you may not use this file except in compliance with the License. |
group-onsemi | 0:098463de4c5d | 6 | * You may obtain a copy of the License at |
group-onsemi | 0:098463de4c5d | 7 | * |
group-onsemi | 0:098463de4c5d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
group-onsemi | 0:098463de4c5d | 9 | * |
group-onsemi | 0:098463de4c5d | 10 | * Unless required by applicable law or agreed to in writing, software |
group-onsemi | 0:098463de4c5d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
group-onsemi | 0:098463de4c5d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-onsemi | 0:098463de4c5d | 13 | * See the License for the specific language governing permissions and |
group-onsemi | 0:098463de4c5d | 14 | * limitations under the License. |
group-onsemi | 0:098463de4c5d | 15 | */ |
group-onsemi | 0:098463de4c5d | 16 | #include "drivers/Stream.h" |
group-onsemi | 0:098463de4c5d | 17 | |
group-onsemi | 0:098463de4c5d | 18 | namespace mbed { |
group-onsemi | 0:098463de4c5d | 19 | |
group-onsemi | 0:098463de4c5d | 20 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
group-onsemi | 0:098463de4c5d | 21 | // No lock needed in constructor |
group-onsemi | 0:098463de4c5d | 22 | /* open ourselves */ |
group-onsemi | 0:098463de4c5d | 23 | char buf[12]; /* :0x12345678 + null byte */ |
group-onsemi | 0:098463de4c5d | 24 | std::sprintf(buf, ":%p", this); |
group-onsemi | 0:098463de4c5d | 25 | _file = std::fopen(buf, "w+"); |
group-onsemi | 0:098463de4c5d | 26 | mbed_set_unbuffered_stream(_file); |
group-onsemi | 0:098463de4c5d | 27 | } |
group-onsemi | 0:098463de4c5d | 28 | |
group-onsemi | 0:098463de4c5d | 29 | Stream::~Stream() { |
group-onsemi | 0:098463de4c5d | 30 | // No lock can be used in destructor |
group-onsemi | 0:098463de4c5d | 31 | fclose(_file); |
group-onsemi | 0:098463de4c5d | 32 | } |
group-onsemi | 0:098463de4c5d | 33 | |
group-onsemi | 0:098463de4c5d | 34 | int Stream::putc(int c) { |
group-onsemi | 0:098463de4c5d | 35 | lock(); |
group-onsemi | 0:098463de4c5d | 36 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 37 | int ret = std::fputc(c, _file); |
group-onsemi | 0:098463de4c5d | 38 | unlock(); |
group-onsemi | 0:098463de4c5d | 39 | return ret; |
group-onsemi | 0:098463de4c5d | 40 | } |
group-onsemi | 0:098463de4c5d | 41 | int Stream::puts(const char *s) { |
group-onsemi | 0:098463de4c5d | 42 | lock(); |
group-onsemi | 0:098463de4c5d | 43 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 44 | int ret = std::fputs(s, _file); |
group-onsemi | 0:098463de4c5d | 45 | unlock(); |
group-onsemi | 0:098463de4c5d | 46 | return ret; |
group-onsemi | 0:098463de4c5d | 47 | } |
group-onsemi | 0:098463de4c5d | 48 | int Stream::getc() { |
group-onsemi | 0:098463de4c5d | 49 | lock(); |
group-onsemi | 0:098463de4c5d | 50 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 51 | int ret = mbed_getc(_file); |
group-onsemi | 0:098463de4c5d | 52 | unlock(); |
group-onsemi | 0:098463de4c5d | 53 | return ret; |
group-onsemi | 0:098463de4c5d | 54 | } |
group-onsemi | 0:098463de4c5d | 55 | char* Stream::gets(char *s, int size) { |
group-onsemi | 0:098463de4c5d | 56 | lock(); |
group-onsemi | 0:098463de4c5d | 57 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 58 | char *ret = mbed_gets(s,size,_file); |
group-onsemi | 0:098463de4c5d | 59 | unlock(); |
group-onsemi | 0:098463de4c5d | 60 | return ret; |
group-onsemi | 0:098463de4c5d | 61 | } |
group-onsemi | 0:098463de4c5d | 62 | |
group-onsemi | 0:098463de4c5d | 63 | int Stream::close() { |
group-onsemi | 0:098463de4c5d | 64 | return 0; |
group-onsemi | 0:098463de4c5d | 65 | } |
group-onsemi | 0:098463de4c5d | 66 | |
group-onsemi | 0:098463de4c5d | 67 | ssize_t Stream::write(const void* buffer, size_t length) { |
group-onsemi | 0:098463de4c5d | 68 | const char* ptr = (const char*)buffer; |
group-onsemi | 0:098463de4c5d | 69 | const char* end = ptr + length; |
group-onsemi | 0:098463de4c5d | 70 | |
group-onsemi | 0:098463de4c5d | 71 | lock(); |
group-onsemi | 0:098463de4c5d | 72 | while (ptr != end) { |
group-onsemi | 0:098463de4c5d | 73 | if (_putc(*ptr++) == EOF) { |
group-onsemi | 0:098463de4c5d | 74 | break; |
group-onsemi | 0:098463de4c5d | 75 | } |
group-onsemi | 0:098463de4c5d | 76 | } |
group-onsemi | 0:098463de4c5d | 77 | unlock(); |
group-onsemi | 0:098463de4c5d | 78 | |
group-onsemi | 0:098463de4c5d | 79 | return ptr - (const char*)buffer; |
group-onsemi | 0:098463de4c5d | 80 | } |
group-onsemi | 0:098463de4c5d | 81 | |
group-onsemi | 0:098463de4c5d | 82 | ssize_t Stream::read(void* buffer, size_t length) { |
group-onsemi | 0:098463de4c5d | 83 | char* ptr = (char*)buffer; |
group-onsemi | 0:098463de4c5d | 84 | char* end = ptr + length; |
group-onsemi | 0:098463de4c5d | 85 | |
group-onsemi | 0:098463de4c5d | 86 | lock(); |
group-onsemi | 0:098463de4c5d | 87 | while (ptr != end) { |
group-onsemi | 0:098463de4c5d | 88 | int c = _getc(); |
group-onsemi | 0:098463de4c5d | 89 | if (c==EOF) break; |
group-onsemi | 0:098463de4c5d | 90 | *ptr++ = c; |
group-onsemi | 0:098463de4c5d | 91 | } |
group-onsemi | 0:098463de4c5d | 92 | unlock(); |
group-onsemi | 0:098463de4c5d | 93 | |
group-onsemi | 0:098463de4c5d | 94 | return ptr - (const char*)buffer; |
group-onsemi | 0:098463de4c5d | 95 | } |
group-onsemi | 0:098463de4c5d | 96 | |
group-onsemi | 0:098463de4c5d | 97 | off_t Stream::lseek(off_t offset, int whence) { |
group-onsemi | 0:098463de4c5d | 98 | return 0; |
group-onsemi | 0:098463de4c5d | 99 | } |
group-onsemi | 0:098463de4c5d | 100 | |
group-onsemi | 0:098463de4c5d | 101 | int Stream::isatty() { |
group-onsemi | 0:098463de4c5d | 102 | return 0; |
group-onsemi | 0:098463de4c5d | 103 | } |
group-onsemi | 0:098463de4c5d | 104 | |
group-onsemi | 0:098463de4c5d | 105 | int Stream::fsync() { |
group-onsemi | 0:098463de4c5d | 106 | return 0; |
group-onsemi | 0:098463de4c5d | 107 | } |
group-onsemi | 0:098463de4c5d | 108 | |
group-onsemi | 0:098463de4c5d | 109 | off_t Stream::flen() { |
group-onsemi | 0:098463de4c5d | 110 | return 0; |
group-onsemi | 0:098463de4c5d | 111 | } |
group-onsemi | 0:098463de4c5d | 112 | |
group-onsemi | 0:098463de4c5d | 113 | int Stream::printf(const char* format, ...) { |
group-onsemi | 0:098463de4c5d | 114 | lock(); |
group-onsemi | 0:098463de4c5d | 115 | std::va_list arg; |
group-onsemi | 0:098463de4c5d | 116 | va_start(arg, format); |
group-onsemi | 0:098463de4c5d | 117 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 118 | int r = vfprintf(_file, format, arg); |
group-onsemi | 0:098463de4c5d | 119 | va_end(arg); |
group-onsemi | 0:098463de4c5d | 120 | unlock(); |
group-onsemi | 0:098463de4c5d | 121 | return r; |
group-onsemi | 0:098463de4c5d | 122 | } |
group-onsemi | 0:098463de4c5d | 123 | |
group-onsemi | 0:098463de4c5d | 124 | int Stream::scanf(const char* format, ...) { |
group-onsemi | 0:098463de4c5d | 125 | lock(); |
group-onsemi | 0:098463de4c5d | 126 | std::va_list arg; |
group-onsemi | 0:098463de4c5d | 127 | va_start(arg, format); |
group-onsemi | 0:098463de4c5d | 128 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 129 | int r = vfscanf(_file, format, arg); |
group-onsemi | 0:098463de4c5d | 130 | va_end(arg); |
group-onsemi | 0:098463de4c5d | 131 | unlock(); |
group-onsemi | 0:098463de4c5d | 132 | return r; |
group-onsemi | 0:098463de4c5d | 133 | } |
group-onsemi | 0:098463de4c5d | 134 | |
group-onsemi | 0:098463de4c5d | 135 | int Stream::vprintf(const char* format, std::va_list args) { |
group-onsemi | 0:098463de4c5d | 136 | lock(); |
group-onsemi | 0:098463de4c5d | 137 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 138 | int r = vfprintf(_file, format, args); |
group-onsemi | 0:098463de4c5d | 139 | unlock(); |
group-onsemi | 0:098463de4c5d | 140 | return r; |
group-onsemi | 0:098463de4c5d | 141 | } |
group-onsemi | 0:098463de4c5d | 142 | |
group-onsemi | 0:098463de4c5d | 143 | int Stream::vscanf(const char* format, std::va_list args) { |
group-onsemi | 0:098463de4c5d | 144 | lock(); |
group-onsemi | 0:098463de4c5d | 145 | fflush(_file); |
group-onsemi | 0:098463de4c5d | 146 | int r = vfscanf(_file, format, args); |
group-onsemi | 0:098463de4c5d | 147 | unlock(); |
group-onsemi | 0:098463de4c5d | 148 | return r; |
group-onsemi | 0:098463de4c5d | 149 | } |
group-onsemi | 0:098463de4c5d | 150 | |
group-onsemi | 0:098463de4c5d | 151 | } // namespace mbed |