I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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