The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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