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