mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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