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_CALLCHAIN_H
mturner5 0:cf7af2656659 17 #define MBED_CALLCHAIN_H
mturner5 0:cf7af2656659 18
mturner5 0:cf7af2656659 19 #include "FunctionPointer.h"
mturner5 0:cf7af2656659 20 #include <string.h>
mturner5 0:cf7af2656659 21
mturner5 0:cf7af2656659 22 namespace mbed {
mturner5 0:cf7af2656659 23
mturner5 0:cf7af2656659 24 /** Group one or more functions in an instance of a CallChain, then call them in
mturner5 0:cf7af2656659 25 * sequence using CallChain::call(). Used mostly by the interrupt chaining code,
mturner5 0:cf7af2656659 26 * but can be used for other purposes.
mturner5 0:cf7af2656659 27 *
mturner5 0:cf7af2656659 28 * Example:
mturner5 0:cf7af2656659 29 * @code
mturner5 0:cf7af2656659 30 * #include "mbed.h"
mturner5 0:cf7af2656659 31 *
mturner5 0:cf7af2656659 32 * CallChain chain;
mturner5 0:cf7af2656659 33 *
mturner5 0:cf7af2656659 34 * void first(void) {
mturner5 0:cf7af2656659 35 * printf("'first' function.\n");
mturner5 0:cf7af2656659 36 * }
mturner5 0:cf7af2656659 37 *
mturner5 0:cf7af2656659 38 * void second(void) {
mturner5 0:cf7af2656659 39 * printf("'second' function.\n");
mturner5 0:cf7af2656659 40 * }
mturner5 0:cf7af2656659 41 *
mturner5 0:cf7af2656659 42 * class Test {
mturner5 0:cf7af2656659 43 * public:
mturner5 0:cf7af2656659 44 * void f(void) {
mturner5 0:cf7af2656659 45 * printf("A::f (class member).\n");
mturner5 0:cf7af2656659 46 * }
mturner5 0:cf7af2656659 47 * };
mturner5 0:cf7af2656659 48 *
mturner5 0:cf7af2656659 49 * int main() {
mturner5 0:cf7af2656659 50 * Test test;
mturner5 0:cf7af2656659 51 *
mturner5 0:cf7af2656659 52 * chain.add(second);
mturner5 0:cf7af2656659 53 * chain.add_front(first);
mturner5 0:cf7af2656659 54 * chain.add(&test, &Test::f);
mturner5 0:cf7af2656659 55 * chain.call();
mturner5 0:cf7af2656659 56 * }
mturner5 0:cf7af2656659 57 * @endcode
mturner5 0:cf7af2656659 58 */
mturner5 0:cf7af2656659 59
mturner5 0:cf7af2656659 60 typedef FunctionPointer* pFunctionPointer_t;
mturner5 0:cf7af2656659 61
mturner5 0:cf7af2656659 62 class CallChain {
mturner5 0:cf7af2656659 63 public:
mturner5 0:cf7af2656659 64 /** Create an empty chain
mturner5 0:cf7af2656659 65 *
mturner5 0:cf7af2656659 66 * @param size (optional) Initial size of the chain
mturner5 0:cf7af2656659 67 */
mturner5 0:cf7af2656659 68 CallChain(int size = 4);
mturner5 0:cf7af2656659 69 virtual ~CallChain();
mturner5 0:cf7af2656659 70
mturner5 0:cf7af2656659 71 /** Add a function at the end of the chain
mturner5 0:cf7af2656659 72 *
mturner5 0:cf7af2656659 73 * @param function A pointer to a void function
mturner5 0:cf7af2656659 74 *
mturner5 0:cf7af2656659 75 * @returns
mturner5 0:cf7af2656659 76 * The function object created for 'function'
mturner5 0:cf7af2656659 77 */
mturner5 0:cf7af2656659 78 pFunctionPointer_t add(void (*function)(void));
mturner5 0:cf7af2656659 79
mturner5 0:cf7af2656659 80 /** Add a function at the end of the chain
mturner5 0:cf7af2656659 81 *
mturner5 0:cf7af2656659 82 * @param tptr pointer to the object to call the member function on
mturner5 0:cf7af2656659 83 * @param mptr pointer to the member function to be called
mturner5 0:cf7af2656659 84 *
mturner5 0:cf7af2656659 85 * @returns
mturner5 0:cf7af2656659 86 * The function object created for 'tptr' and 'mptr'
mturner5 0:cf7af2656659 87 */
mturner5 0:cf7af2656659 88 template<typename T>
mturner5 0:cf7af2656659 89 pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
mturner5 0:cf7af2656659 90 return common_add(new FunctionPointer(tptr, mptr));
mturner5 0:cf7af2656659 91 }
mturner5 0:cf7af2656659 92
mturner5 0:cf7af2656659 93 /** Add a function at the beginning of the chain
mturner5 0:cf7af2656659 94 *
mturner5 0:cf7af2656659 95 * @param function A pointer to a void function
mturner5 0:cf7af2656659 96 *
mturner5 0:cf7af2656659 97 * @returns
mturner5 0:cf7af2656659 98 * The function object created for 'function'
mturner5 0:cf7af2656659 99 */
mturner5 0:cf7af2656659 100 pFunctionPointer_t add_front(void (*function)(void));
mturner5 0:cf7af2656659 101
mturner5 0:cf7af2656659 102 /** Add a function at the beginning of the chain
mturner5 0:cf7af2656659 103 *
mturner5 0:cf7af2656659 104 * @param tptr pointer to the object to call the member function on
mturner5 0:cf7af2656659 105 * @param mptr pointer to the member function to be called
mturner5 0:cf7af2656659 106 *
mturner5 0:cf7af2656659 107 * @returns
mturner5 0:cf7af2656659 108 * The function object created for 'tptr' and 'mptr'
mturner5 0:cf7af2656659 109 */
mturner5 0:cf7af2656659 110 template<typename T>
mturner5 0:cf7af2656659 111 pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
mturner5 0:cf7af2656659 112 return common_add_front(new FunctionPointer(tptr, mptr));
mturner5 0:cf7af2656659 113 }
mturner5 0:cf7af2656659 114
mturner5 0:cf7af2656659 115 /** Get the number of functions in the chain
mturner5 0:cf7af2656659 116 */
mturner5 0:cf7af2656659 117 int size() const;
mturner5 0:cf7af2656659 118
mturner5 0:cf7af2656659 119 /** Get a function object from the chain
mturner5 0:cf7af2656659 120 *
mturner5 0:cf7af2656659 121 * @param i function object index
mturner5 0:cf7af2656659 122 *
mturner5 0:cf7af2656659 123 * @returns
mturner5 0:cf7af2656659 124 * The function object at position 'i' in the chain
mturner5 0:cf7af2656659 125 */
mturner5 0:cf7af2656659 126 pFunctionPointer_t get(int i) const;
mturner5 0:cf7af2656659 127
mturner5 0:cf7af2656659 128 /** Look for a function object in the call chain
mturner5 0:cf7af2656659 129 *
mturner5 0:cf7af2656659 130 * @param f the function object to search
mturner5 0:cf7af2656659 131 *
mturner5 0:cf7af2656659 132 * @returns
mturner5 0:cf7af2656659 133 * The index of the function object if found, -1 otherwise.
mturner5 0:cf7af2656659 134 */
mturner5 0:cf7af2656659 135 int find(pFunctionPointer_t f) const;
mturner5 0:cf7af2656659 136
mturner5 0:cf7af2656659 137 /** Clear the call chain (remove all functions in the chain).
mturner5 0:cf7af2656659 138 */
mturner5 0:cf7af2656659 139 void clear();
mturner5 0:cf7af2656659 140
mturner5 0:cf7af2656659 141 /** Remove a function object from the chain
mturner5 0:cf7af2656659 142 *
mturner5 0:cf7af2656659 143 * @arg f the function object to remove
mturner5 0:cf7af2656659 144 *
mturner5 0:cf7af2656659 145 * @returns
mturner5 0:cf7af2656659 146 * true if the function object was found and removed, false otherwise.
mturner5 0:cf7af2656659 147 */
mturner5 0:cf7af2656659 148 bool remove(pFunctionPointer_t f);
mturner5 0:cf7af2656659 149
mturner5 0:cf7af2656659 150 /** Call all the functions in the chain in sequence
mturner5 0:cf7af2656659 151 */
mturner5 0:cf7af2656659 152 void call();
mturner5 0:cf7af2656659 153
mturner5 0:cf7af2656659 154 #ifdef MBED_OPERATORS
mturner5 0:cf7af2656659 155 void operator ()(void) {
mturner5 0:cf7af2656659 156 call();
mturner5 0:cf7af2656659 157 }
mturner5 0:cf7af2656659 158 pFunctionPointer_t operator [](int i) const {
mturner5 0:cf7af2656659 159 return get(i);
mturner5 0:cf7af2656659 160 }
mturner5 0:cf7af2656659 161 #endif
mturner5 0:cf7af2656659 162
mturner5 0:cf7af2656659 163 private:
mturner5 0:cf7af2656659 164 void _check_size();
mturner5 0:cf7af2656659 165 pFunctionPointer_t common_add(pFunctionPointer_t pf);
mturner5 0:cf7af2656659 166 pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
mturner5 0:cf7af2656659 167
mturner5 0:cf7af2656659 168 pFunctionPointer_t* _chain;
mturner5 0:cf7af2656659 169 int _size;
mturner5 0:cf7af2656659 170 int _elements;
mturner5 0:cf7af2656659 171
mturner5 0:cf7af2656659 172 /* disallow copy constructor and assignment operators */
mturner5 0:cf7af2656659 173 private:
mturner5 0:cf7af2656659 174 CallChain(const CallChain&);
mturner5 0:cf7af2656659 175 CallChain & operator = (const CallChain&);
mturner5 0:cf7af2656659 176 };
mturner5 0:cf7af2656659 177
mturner5 0:cf7af2656659 178 } // namespace mbed
mturner5 0:cf7af2656659 179
mturner5 0:cf7af2656659 180 #endif
mturner5 0:cf7af2656659 181