Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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