mbed
Dependents: DHTSensor_Test K64F_eCompass_OneNET_JW
FileHandle.h@0:2cc6bb4d7fea, 2015-02-25 (annotated)
- Committer:
- mbotkinl
- Date:
- Wed Feb 25 20:22:22 2015 +0000
- Revision:
- 0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbotkinl | 0:2cc6bb4d7fea | 1 | /* mbed Microcontroller Library |
mbotkinl | 0:2cc6bb4d7fea | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbotkinl | 0:2cc6bb4d7fea | 3 | * |
mbotkinl | 0:2cc6bb4d7fea | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbotkinl | 0:2cc6bb4d7fea | 5 | * you may not use this file except in compliance with the License. |
mbotkinl | 0:2cc6bb4d7fea | 6 | * You may obtain a copy of the License at |
mbotkinl | 0:2cc6bb4d7fea | 7 | * |
mbotkinl | 0:2cc6bb4d7fea | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbotkinl | 0:2cc6bb4d7fea | 9 | * |
mbotkinl | 0:2cc6bb4d7fea | 10 | * Unless required by applicable law or agreed to in writing, software |
mbotkinl | 0:2cc6bb4d7fea | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbotkinl | 0:2cc6bb4d7fea | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbotkinl | 0:2cc6bb4d7fea | 13 | * See the License for the specific language governing permissions and |
mbotkinl | 0:2cc6bb4d7fea | 14 | * limitations under the License. |
mbotkinl | 0:2cc6bb4d7fea | 15 | */ |
mbotkinl | 0:2cc6bb4d7fea | 16 | #ifndef MBED_FILEHANDLE_H |
mbotkinl | 0:2cc6bb4d7fea | 17 | #define MBED_FILEHANDLE_H |
mbotkinl | 0:2cc6bb4d7fea | 18 | |
mbotkinl | 0:2cc6bb4d7fea | 19 | typedef int FILEHANDLE; |
mbotkinl | 0:2cc6bb4d7fea | 20 | |
mbotkinl | 0:2cc6bb4d7fea | 21 | #include <stdio.h> |
mbotkinl | 0:2cc6bb4d7fea | 22 | |
mbotkinl | 0:2cc6bb4d7fea | 23 | #if defined(__ARMCC_VERSION) || defined(__ICCARM__) |
mbotkinl | 0:2cc6bb4d7fea | 24 | typedef int ssize_t; |
mbotkinl | 0:2cc6bb4d7fea | 25 | typedef long off_t; |
mbotkinl | 0:2cc6bb4d7fea | 26 | |
mbotkinl | 0:2cc6bb4d7fea | 27 | #else |
mbotkinl | 0:2cc6bb4d7fea | 28 | # include <sys/types.h> |
mbotkinl | 0:2cc6bb4d7fea | 29 | #endif |
mbotkinl | 0:2cc6bb4d7fea | 30 | |
mbotkinl | 0:2cc6bb4d7fea | 31 | namespace mbed { |
mbotkinl | 0:2cc6bb4d7fea | 32 | |
mbotkinl | 0:2cc6bb4d7fea | 33 | /** An OO equivalent of the internal FILEHANDLE variable |
mbotkinl | 0:2cc6bb4d7fea | 34 | * and associated _sys_* functions. |
mbotkinl | 0:2cc6bb4d7fea | 35 | * |
mbotkinl | 0:2cc6bb4d7fea | 36 | * FileHandle is an abstract class, needing at least sys_write and |
mbotkinl | 0:2cc6bb4d7fea | 37 | * sys_read to be implmented for a simple interactive device. |
mbotkinl | 0:2cc6bb4d7fea | 38 | * |
mbotkinl | 0:2cc6bb4d7fea | 39 | * No one ever directly tals to/instanciates a FileHandle - it gets |
mbotkinl | 0:2cc6bb4d7fea | 40 | * created by FileSystem, and wrapped up by stdio. |
mbotkinl | 0:2cc6bb4d7fea | 41 | */ |
mbotkinl | 0:2cc6bb4d7fea | 42 | class FileHandle { |
mbotkinl | 0:2cc6bb4d7fea | 43 | |
mbotkinl | 0:2cc6bb4d7fea | 44 | public: |
mbotkinl | 0:2cc6bb4d7fea | 45 | /** Write the contents of a buffer to the file |
mbotkinl | 0:2cc6bb4d7fea | 46 | * |
mbotkinl | 0:2cc6bb4d7fea | 47 | * @param buffer the buffer to write from |
mbotkinl | 0:2cc6bb4d7fea | 48 | * @param length the number of characters to write |
mbotkinl | 0:2cc6bb4d7fea | 49 | * |
mbotkinl | 0:2cc6bb4d7fea | 50 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 51 | * The number of characters written (possibly 0) on success, -1 on error. |
mbotkinl | 0:2cc6bb4d7fea | 52 | */ |
mbotkinl | 0:2cc6bb4d7fea | 53 | virtual ssize_t write(const void* buffer, size_t length) = 0; |
mbotkinl | 0:2cc6bb4d7fea | 54 | |
mbotkinl | 0:2cc6bb4d7fea | 55 | /** Close the file |
mbotkinl | 0:2cc6bb4d7fea | 56 | * |
mbotkinl | 0:2cc6bb4d7fea | 57 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 58 | * Zero on success, -1 on error. |
mbotkinl | 0:2cc6bb4d7fea | 59 | */ |
mbotkinl | 0:2cc6bb4d7fea | 60 | virtual int close() = 0; |
mbotkinl | 0:2cc6bb4d7fea | 61 | |
mbotkinl | 0:2cc6bb4d7fea | 62 | /** Function read |
mbotkinl | 0:2cc6bb4d7fea | 63 | * Reads the contents of the file into a buffer |
mbotkinl | 0:2cc6bb4d7fea | 64 | * |
mbotkinl | 0:2cc6bb4d7fea | 65 | * @param buffer the buffer to read in to |
mbotkinl | 0:2cc6bb4d7fea | 66 | * @param length the number of characters to read |
mbotkinl | 0:2cc6bb4d7fea | 67 | * |
mbotkinl | 0:2cc6bb4d7fea | 68 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 69 | * The number of characters read (zero at end of file) on success, -1 on error. |
mbotkinl | 0:2cc6bb4d7fea | 70 | */ |
mbotkinl | 0:2cc6bb4d7fea | 71 | virtual ssize_t read(void* buffer, size_t length) = 0; |
mbotkinl | 0:2cc6bb4d7fea | 72 | |
mbotkinl | 0:2cc6bb4d7fea | 73 | /** Check if the handle is for a interactive terminal device. |
mbotkinl | 0:2cc6bb4d7fea | 74 | * If so, line buffered behaviour is used by default |
mbotkinl | 0:2cc6bb4d7fea | 75 | * |
mbotkinl | 0:2cc6bb4d7fea | 76 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 77 | * 1 if it is a terminal, |
mbotkinl | 0:2cc6bb4d7fea | 78 | * 0 otherwise |
mbotkinl | 0:2cc6bb4d7fea | 79 | */ |
mbotkinl | 0:2cc6bb4d7fea | 80 | virtual int isatty() = 0; |
mbotkinl | 0:2cc6bb4d7fea | 81 | |
mbotkinl | 0:2cc6bb4d7fea | 82 | /** Move the file position to a given offset from a given location. |
mbotkinl | 0:2cc6bb4d7fea | 83 | * |
mbotkinl | 0:2cc6bb4d7fea | 84 | * @param offset The offset from whence to move to |
mbotkinl | 0:2cc6bb4d7fea | 85 | * @param whence SEEK_SET for the start of the file, SEEK_CUR for the |
mbotkinl | 0:2cc6bb4d7fea | 86 | * current file position, or SEEK_END for the end of the file. |
mbotkinl | 0:2cc6bb4d7fea | 87 | * |
mbotkinl | 0:2cc6bb4d7fea | 88 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 89 | * new file position on success, |
mbotkinl | 0:2cc6bb4d7fea | 90 | * -1 on failure or unsupported |
mbotkinl | 0:2cc6bb4d7fea | 91 | */ |
mbotkinl | 0:2cc6bb4d7fea | 92 | virtual off_t lseek(off_t offset, int whence) = 0; |
mbotkinl | 0:2cc6bb4d7fea | 93 | |
mbotkinl | 0:2cc6bb4d7fea | 94 | /** Flush any buffers associated with the FileHandle, ensuring it |
mbotkinl | 0:2cc6bb4d7fea | 95 | * is up to date on disk |
mbotkinl | 0:2cc6bb4d7fea | 96 | * |
mbotkinl | 0:2cc6bb4d7fea | 97 | * @returns |
mbotkinl | 0:2cc6bb4d7fea | 98 | * 0 on success or un-needed, |
mbotkinl | 0:2cc6bb4d7fea | 99 | * -1 on error |
mbotkinl | 0:2cc6bb4d7fea | 100 | */ |
mbotkinl | 0:2cc6bb4d7fea | 101 | virtual int fsync() = 0; |
mbotkinl | 0:2cc6bb4d7fea | 102 | |
mbotkinl | 0:2cc6bb4d7fea | 103 | virtual off_t flen() { |
mbotkinl | 0:2cc6bb4d7fea | 104 | /* remember our current position */ |
mbotkinl | 0:2cc6bb4d7fea | 105 | off_t pos = lseek(0, SEEK_CUR); |
mbotkinl | 0:2cc6bb4d7fea | 106 | if(pos == -1) return -1; |
mbotkinl | 0:2cc6bb4d7fea | 107 | /* seek to the end to get the file length */ |
mbotkinl | 0:2cc6bb4d7fea | 108 | off_t res = lseek(0, SEEK_END); |
mbotkinl | 0:2cc6bb4d7fea | 109 | /* return to our old position */ |
mbotkinl | 0:2cc6bb4d7fea | 110 | lseek(pos, SEEK_SET); |
mbotkinl | 0:2cc6bb4d7fea | 111 | return res; |
mbotkinl | 0:2cc6bb4d7fea | 112 | } |
mbotkinl | 0:2cc6bb4d7fea | 113 | |
mbotkinl | 0:2cc6bb4d7fea | 114 | virtual ~FileHandle(); |
mbotkinl | 0:2cc6bb4d7fea | 115 | }; |
mbotkinl | 0:2cc6bb4d7fea | 116 | |
mbotkinl | 0:2cc6bb4d7fea | 117 | } // namespace mbed |
mbotkinl | 0:2cc6bb4d7fea | 118 | |
mbotkinl | 0:2cc6bb4d7fea | 119 | #endif |