Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

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