PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Wed Oct 11 20:35:27 2017 +0000
Revision:
5:ea7377f3d1af
Fixed PokittoLib. Includes a working custom mbed-src

Who changed what in which revision?

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