Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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