Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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