mbed library sources with internal temperature sensor for nucleo f401
common/Stream.cpp@0:7e2bd16f80af, 2015-01-17 (annotated)
- Committer:
- elessair
- Date:
- Sat Jan 17 18:03:58 2015 +0000
- Revision:
- 0:7e2bd16f80af
nucleo f401re internal temperature added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:7e2bd16f80af | 1 | /* mbed Microcontroller Library |
elessair | 0:7e2bd16f80af | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:7e2bd16f80af | 3 | * |
elessair | 0:7e2bd16f80af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:7e2bd16f80af | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:7e2bd16f80af | 6 | * You may obtain a copy of the License at |
elessair | 0:7e2bd16f80af | 7 | * |
elessair | 0:7e2bd16f80af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:7e2bd16f80af | 9 | * |
elessair | 0:7e2bd16f80af | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:7e2bd16f80af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:7e2bd16f80af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:7e2bd16f80af | 13 | * See the License for the specific language governing permissions and |
elessair | 0:7e2bd16f80af | 14 | * limitations under the License. |
elessair | 0:7e2bd16f80af | 15 | */ |
elessair | 0:7e2bd16f80af | 16 | #include "Stream.h" |
elessair | 0:7e2bd16f80af | 17 | |
elessair | 0:7e2bd16f80af | 18 | #include <cstdarg> |
elessair | 0:7e2bd16f80af | 19 | |
elessair | 0:7e2bd16f80af | 20 | namespace mbed { |
elessair | 0:7e2bd16f80af | 21 | |
elessair | 0:7e2bd16f80af | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
elessair | 0:7e2bd16f80af | 23 | /* open ourselves */ |
elessair | 0:7e2bd16f80af | 24 | char buf[12]; /* :0x12345678 + null byte */ |
elessair | 0:7e2bd16f80af | 25 | std::sprintf(buf, ":%p", this); |
elessair | 0:7e2bd16f80af | 26 | _file = std::fopen(buf, "w+"); |
elessair | 0:7e2bd16f80af | 27 | mbed_set_unbuffered_stream(_file); |
elessair | 0:7e2bd16f80af | 28 | } |
elessair | 0:7e2bd16f80af | 29 | |
elessair | 0:7e2bd16f80af | 30 | Stream::~Stream() { |
elessair | 0:7e2bd16f80af | 31 | fclose(_file); |
elessair | 0:7e2bd16f80af | 32 | } |
elessair | 0:7e2bd16f80af | 33 | |
elessair | 0:7e2bd16f80af | 34 | int Stream::putc(int c) { |
elessair | 0:7e2bd16f80af | 35 | fflush(_file); |
elessair | 0:7e2bd16f80af | 36 | return std::fputc(c, _file); |
elessair | 0:7e2bd16f80af | 37 | } |
elessair | 0:7e2bd16f80af | 38 | int Stream::puts(const char *s) { |
elessair | 0:7e2bd16f80af | 39 | fflush(_file); |
elessair | 0:7e2bd16f80af | 40 | return std::fputs(s, _file); |
elessair | 0:7e2bd16f80af | 41 | } |
elessair | 0:7e2bd16f80af | 42 | int Stream::getc() { |
elessair | 0:7e2bd16f80af | 43 | fflush(_file); |
elessair | 0:7e2bd16f80af | 44 | return mbed_getc(_file); |
elessair | 0:7e2bd16f80af | 45 | } |
elessair | 0:7e2bd16f80af | 46 | char* Stream::gets(char *s, int size) { |
elessair | 0:7e2bd16f80af | 47 | fflush(_file); |
elessair | 0:7e2bd16f80af | 48 | return mbed_gets(s,size,_file); |
elessair | 0:7e2bd16f80af | 49 | } |
elessair | 0:7e2bd16f80af | 50 | |
elessair | 0:7e2bd16f80af | 51 | int Stream::close() { |
elessair | 0:7e2bd16f80af | 52 | return 0; |
elessair | 0:7e2bd16f80af | 53 | } |
elessair | 0:7e2bd16f80af | 54 | |
elessair | 0:7e2bd16f80af | 55 | ssize_t Stream::write(const void* buffer, size_t length) { |
elessair | 0:7e2bd16f80af | 56 | const char* ptr = (const char*)buffer; |
elessair | 0:7e2bd16f80af | 57 | const char* end = ptr + length; |
elessair | 0:7e2bd16f80af | 58 | while (ptr != end) { |
elessair | 0:7e2bd16f80af | 59 | if (_putc(*ptr++) == EOF) { |
elessair | 0:7e2bd16f80af | 60 | break; |
elessair | 0:7e2bd16f80af | 61 | } |
elessair | 0:7e2bd16f80af | 62 | } |
elessair | 0:7e2bd16f80af | 63 | return ptr - (const char*)buffer; |
elessair | 0:7e2bd16f80af | 64 | } |
elessair | 0:7e2bd16f80af | 65 | |
elessair | 0:7e2bd16f80af | 66 | ssize_t Stream::read(void* buffer, size_t length) { |
elessair | 0:7e2bd16f80af | 67 | char* ptr = (char*)buffer; |
elessair | 0:7e2bd16f80af | 68 | char* end = ptr + length; |
elessair | 0:7e2bd16f80af | 69 | while (ptr != end) { |
elessair | 0:7e2bd16f80af | 70 | int c = _getc(); |
elessair | 0:7e2bd16f80af | 71 | if (c==EOF) break; |
elessair | 0:7e2bd16f80af | 72 | *ptr++ = c; |
elessair | 0:7e2bd16f80af | 73 | } |
elessair | 0:7e2bd16f80af | 74 | return ptr - (const char*)buffer; |
elessair | 0:7e2bd16f80af | 75 | } |
elessair | 0:7e2bd16f80af | 76 | |
elessair | 0:7e2bd16f80af | 77 | off_t Stream::lseek(off_t offset, int whence) { |
elessair | 0:7e2bd16f80af | 78 | return 0; |
elessair | 0:7e2bd16f80af | 79 | } |
elessair | 0:7e2bd16f80af | 80 | |
elessair | 0:7e2bd16f80af | 81 | int Stream::isatty() { |
elessair | 0:7e2bd16f80af | 82 | return 0; |
elessair | 0:7e2bd16f80af | 83 | } |
elessair | 0:7e2bd16f80af | 84 | |
elessair | 0:7e2bd16f80af | 85 | int Stream::fsync() { |
elessair | 0:7e2bd16f80af | 86 | return 0; |
elessair | 0:7e2bd16f80af | 87 | } |
elessair | 0:7e2bd16f80af | 88 | |
elessair | 0:7e2bd16f80af | 89 | off_t Stream::flen() { |
elessair | 0:7e2bd16f80af | 90 | return 0; |
elessair | 0:7e2bd16f80af | 91 | } |
elessair | 0:7e2bd16f80af | 92 | |
elessair | 0:7e2bd16f80af | 93 | int Stream::printf(const char* format, ...) { |
elessair | 0:7e2bd16f80af | 94 | std::va_list arg; |
elessair | 0:7e2bd16f80af | 95 | va_start(arg, format); |
elessair | 0:7e2bd16f80af | 96 | fflush(_file); |
elessair | 0:7e2bd16f80af | 97 | int r = vfprintf(_file, format, arg); |
elessair | 0:7e2bd16f80af | 98 | va_end(arg); |
elessair | 0:7e2bd16f80af | 99 | return r; |
elessair | 0:7e2bd16f80af | 100 | } |
elessair | 0:7e2bd16f80af | 101 | |
elessair | 0:7e2bd16f80af | 102 | int Stream::scanf(const char* format, ...) { |
elessair | 0:7e2bd16f80af | 103 | std::va_list arg; |
elessair | 0:7e2bd16f80af | 104 | va_start(arg, format); |
elessair | 0:7e2bd16f80af | 105 | fflush(_file); |
elessair | 0:7e2bd16f80af | 106 | int r = vfscanf(_file, format, arg); |
elessair | 0:7e2bd16f80af | 107 | va_end(arg); |
elessair | 0:7e2bd16f80af | 108 | return r; |
elessair | 0:7e2bd16f80af | 109 | } |
elessair | 0:7e2bd16f80af | 110 | |
elessair | 0:7e2bd16f80af | 111 | } // namespace mbed |