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