Fork of mbed for KL05Z based smart sensor which has no crystal on the board, so MCG FEI mode is required.

Dependents:   smart-sensor-KL05Z-debug

Fork of mbed-src by Ermanno Brusadin

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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