LPC11U35 ADC Tick & USBSerial

Dependencies:   mbed

Dependents:   SmallDoseMeter_SingleCH_AE_lpc11u35_V1_00

Committer:
H_Tsunemoto
Date:
Mon Feb 19 09:09:52 2018 +0000
Revision:
1:b1a3be5f48ab
Parent:
0:871ab6846b18
test

Who changed what in which revision?

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