This is a repository for all my programs or modified programs.

Committer:
mturner5
Date:
Sun Sep 11 23:48:09 2016 +0000
Revision:
0:72480818e4a9
Made delays for the LEDS and button presses

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mturner5 0:72480818e4a9 1 /* mbed Microcontroller Library
mturner5 0:72480818e4a9 2 * Copyright (c) 2006-2013 ARM Limited
mturner5 0:72480818e4a9 3 *
mturner5 0:72480818e4a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mturner5 0:72480818e4a9 5 * you may not use this file except in compliance with the License.
mturner5 0:72480818e4a9 6 * You may obtain a copy of the License at
mturner5 0:72480818e4a9 7 *
mturner5 0:72480818e4a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mturner5 0:72480818e4a9 9 *
mturner5 0:72480818e4a9 10 * Unless required by applicable law or agreed to in writing, software
mturner5 0:72480818e4a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mturner5 0:72480818e4a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mturner5 0:72480818e4a9 13 * See the License for the specific language governing permissions and
mturner5 0:72480818e4a9 14 * limitations under the License.
mturner5 0:72480818e4a9 15 */
mturner5 0:72480818e4a9 16 #ifndef MBED_CALLCHAIN_H
mturner5 0:72480818e4a9 17 #define MBED_CALLCHAIN_H
mturner5 0:72480818e4a9 18
mturner5 0:72480818e4a9 19 #include "FunctionPointer.h"
mturner5 0:72480818e4a9 20 #include <string.h>
mturner5 0:72480818e4a9 21
mturner5 0:72480818e4a9 22 namespace mbed {
mturner5 0:72480818e4a9 23
mturner5 0:72480818e4a9 24 /** Group one or more functions in an instance of a CallChain, then call them in
mturner5 0:72480818e4a9 25 * sequence using CallChain::call(). Used mostly by the interrupt chaining code,
mturner5 0:72480818e4a9 26 * but can be used for other purposes.
mturner5 0:72480818e4a9 27 *
mturner5 0:72480818e4a9 28 * Example:
mturner5 0:72480818e4a9 29 * @code
mturner5 0:72480818e4a9 30 * #include "mbed.h"
mturner5 0:72480818e4a9 31 *
mturner5 0:72480818e4a9 32 * CallChain chain;
mturner5 0:72480818e4a9 33 *
mturner5 0:72480818e4a9 34 * void first(void) {
mturner5 0:72480818e4a9 35 * printf("'first' function.\n");
mturner5 0:72480818e4a9 36 * }
mturner5 0:72480818e4a9 37 *
mturner5 0:72480818e4a9 38 * void second(void) {
mturner5 0:72480818e4a9 39 * printf("'second' function.\n");
mturner5 0:72480818e4a9 40 * }
mturner5 0:72480818e4a9 41 *
mturner5 0:72480818e4a9 42 * class Test {
mturner5 0:72480818e4a9 43 * public:
mturner5 0:72480818e4a9 44 * void f(void) {
mturner5 0:72480818e4a9 45 * printf("A::f (class member).\n");
mturner5 0:72480818e4a9 46 * }
mturner5 0:72480818e4a9 47 * };
mturner5 0:72480818e4a9 48 *
mturner5 0:72480818e4a9 49 * int main() {
mturner5 0:72480818e4a9 50 * Test test;
mturner5 0:72480818e4a9 51 *
mturner5 0:72480818e4a9 52 * chain.add(second);
mturner5 0:72480818e4a9 53 * chain.add_front(first);
mturner5 0:72480818e4a9 54 * chain.add(&test, &Test::f);
mturner5 0:72480818e4a9 55 * chain.call();
mturner5 0:72480818e4a9 56 * }
mturner5 0:72480818e4a9 57 * @endcode
mturner5 0:72480818e4a9 58 */
mturner5 0:72480818e4a9 59
mturner5 0:72480818e4a9 60 typedef FunctionPointer* pFunctionPointer_t;
mturner5 0:72480818e4a9 61
mturner5 0:72480818e4a9 62 class CallChain {
mturner5 0:72480818e4a9 63 public:
mturner5 0:72480818e4a9 64 /** Create an empty chain
mturner5 0:72480818e4a9 65 *
mturner5 0:72480818e4a9 66 * @param size (optional) Initial size of the chain
mturner5 0:72480818e4a9 67 */
mturner5 0:72480818e4a9 68 CallChain(int size = 4);
mturner5 0:72480818e4a9 69 virtual ~CallChain();
mturner5 0:72480818e4a9 70
mturner5 0:72480818e4a9 71 /** Add a function at the end of the chain
mturner5 0:72480818e4a9 72 *
mturner5 0:72480818e4a9 73 * @param function A pointer to a void function
mturner5 0:72480818e4a9 74 *
mturner5 0:72480818e4a9 75 * @returns
mturner5 0:72480818e4a9 76 * The function object created for 'function'
mturner5 0:72480818e4a9 77 */
mturner5 0:72480818e4a9 78 pFunctionPointer_t add(void (*function)(void));
mturner5 0:72480818e4a9 79
mturner5 0:72480818e4a9 80 /** Add a function at the end of the chain
mturner5 0:72480818e4a9 81 *
mturner5 0:72480818e4a9 82 * @param tptr pointer to the object to call the member function on
mturner5 0:72480818e4a9 83 * @param mptr pointer to the member function to be called
mturner5 0:72480818e4a9 84 *
mturner5 0:72480818e4a9 85 * @returns
mturner5 0:72480818e4a9 86 * The function object created for 'tptr' and 'mptr'
mturner5 0:72480818e4a9 87 */
mturner5 0:72480818e4a9 88 template<typename T>
mturner5 0:72480818e4a9 89 pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) {
mturner5 0:72480818e4a9 90 return common_add(new FunctionPointer(tptr, mptr));
mturner5 0:72480818e4a9 91 }
mturner5 0:72480818e4a9 92
mturner5 0:72480818e4a9 93 /** Add a function at the beginning of the chain
mturner5 0:72480818e4a9 94 *
mturner5 0:72480818e4a9 95 * @param function A pointer to a void function
mturner5 0:72480818e4a9 96 *
mturner5 0:72480818e4a9 97 * @returns
mturner5 0:72480818e4a9 98 * The function object created for 'function'
mturner5 0:72480818e4a9 99 */
mturner5 0:72480818e4a9 100 pFunctionPointer_t add_front(void (*function)(void));
mturner5 0:72480818e4a9 101
mturner5 0:72480818e4a9 102 /** Add a function at the beginning of the chain
mturner5 0:72480818e4a9 103 *
mturner5 0:72480818e4a9 104 * @param tptr pointer to the object to call the member function on
mturner5 0:72480818e4a9 105 * @param mptr pointer to the member function to be called
mturner5 0:72480818e4a9 106 *
mturner5 0:72480818e4a9 107 * @returns
mturner5 0:72480818e4a9 108 * The function object created for 'tptr' and 'mptr'
mturner5 0:72480818e4a9 109 */
mturner5 0:72480818e4a9 110 template<typename T>
mturner5 0:72480818e4a9 111 pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) {
mturner5 0:72480818e4a9 112 return common_add_front(new FunctionPointer(tptr, mptr));
mturner5 0:72480818e4a9 113 }
mturner5 0:72480818e4a9 114
mturner5 0:72480818e4a9 115 /** Get the number of functions in the chain
mturner5 0:72480818e4a9 116 */
mturner5 0:72480818e4a9 117 int size() const;
mturner5 0:72480818e4a9 118
mturner5 0:72480818e4a9 119 /** Get a function object from the chain
mturner5 0:72480818e4a9 120 *
mturner5 0:72480818e4a9 121 * @param i function object index
mturner5 0:72480818e4a9 122 *
mturner5 0:72480818e4a9 123 * @returns
mturner5 0:72480818e4a9 124 * The function object at position 'i' in the chain
mturner5 0:72480818e4a9 125 */
mturner5 0:72480818e4a9 126 pFunctionPointer_t get(int i) const;
mturner5 0:72480818e4a9 127
mturner5 0:72480818e4a9 128 /** Look for a function object in the call chain
mturner5 0:72480818e4a9 129 *
mturner5 0:72480818e4a9 130 * @param f the function object to search
mturner5 0:72480818e4a9 131 *
mturner5 0:72480818e4a9 132 * @returns
mturner5 0:72480818e4a9 133 * The index of the function object if found, -1 otherwise.
mturner5 0:72480818e4a9 134 */
mturner5 0:72480818e4a9 135 int find(pFunctionPointer_t f) const;
mturner5 0:72480818e4a9 136
mturner5 0:72480818e4a9 137 /** Clear the call chain (remove all functions in the chain).
mturner5 0:72480818e4a9 138 */
mturner5 0:72480818e4a9 139 void clear();
mturner5 0:72480818e4a9 140
mturner5 0:72480818e4a9 141 /** Remove a function object from the chain
mturner5 0:72480818e4a9 142 *
mturner5 0:72480818e4a9 143 * @arg f the function object to remove
mturner5 0:72480818e4a9 144 *
mturner5 0:72480818e4a9 145 * @returns
mturner5 0:72480818e4a9 146 * true if the function object was found and removed, false otherwise.
mturner5 0:72480818e4a9 147 */
mturner5 0:72480818e4a9 148 bool remove(pFunctionPointer_t f);
mturner5 0:72480818e4a9 149
mturner5 0:72480818e4a9 150 /** Call all the functions in the chain in sequence
mturner5 0:72480818e4a9 151 */
mturner5 0:72480818e4a9 152 void call();
mturner5 0:72480818e4a9 153
mturner5 0:72480818e4a9 154 #ifdef MBED_OPERATORS
mturner5 0:72480818e4a9 155 void operator ()(void) {
mturner5 0:72480818e4a9 156 call();
mturner5 0:72480818e4a9 157 }
mturner5 0:72480818e4a9 158 pFunctionPointer_t operator [](int i) const {
mturner5 0:72480818e4a9 159 return get(i);
mturner5 0:72480818e4a9 160 }
mturner5 0:72480818e4a9 161 #endif
mturner5 0:72480818e4a9 162
mturner5 0:72480818e4a9 163 private:
mturner5 0:72480818e4a9 164 void _check_size();
mturner5 0:72480818e4a9 165 pFunctionPointer_t common_add(pFunctionPointer_t pf);
mturner5 0:72480818e4a9 166 pFunctionPointer_t common_add_front(pFunctionPointer_t pf);
mturner5 0:72480818e4a9 167
mturner5 0:72480818e4a9 168 pFunctionPointer_t* _chain;
mturner5 0:72480818e4a9 169 int _size;
mturner5 0:72480818e4a9 170 int _elements;
mturner5 0:72480818e4a9 171
mturner5 0:72480818e4a9 172 /* disallow copy constructor and assignment operators */
mturner5 0:72480818e4a9 173 private:
mturner5 0:72480818e4a9 174 CallChain(const CallChain&);
mturner5 0:72480818e4a9 175 CallChain & operator = (const CallChain&);
mturner5 0:72480818e4a9 176 };
mturner5 0:72480818e4a9 177
mturner5 0:72480818e4a9 178 } // namespace mbed
mturner5 0:72480818e4a9 179
mturner5 0:72480818e4a9 180 #endif
mturner5 0:72480818e4a9 181