meh

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Mon Aug 19 13:34:54 2013 +0300
Revision:
66:9c8f0e3462fb
Parent:
65:5798e58a58b1
Child:
68:f37f3b9c9f0b
New mbed library build with support for LPC1114.

Built from github tag 'mbed_lib_rev66'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 65:5798e58a58b1 1 /* mbed Microcontroller Library
bogdanm 65:5798e58a58b1 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 65:5798e58a58b1 3 *
bogdanm 65:5798e58a58b1 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 65:5798e58a58b1 5 * you may not use this file except in compliance with the License.
bogdanm 65:5798e58a58b1 6 * You may obtain a copy of the License at
bogdanm 65:5798e58a58b1 7 *
bogdanm 65:5798e58a58b1 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 65:5798e58a58b1 9 *
bogdanm 65:5798e58a58b1 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 65:5798e58a58b1 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 65:5798e58a58b1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 65:5798e58a58b1 13 * See the License for the specific language governing permissions and
bogdanm 65:5798e58a58b1 14 * limitations under the License.
bogdanm 65:5798e58a58b1 15 */
bogdanm 65:5798e58a58b1 16 #ifndef MBED_TICKER_H
bogdanm 65:5798e58a58b1 17 #define MBED_TICKER_H
bogdanm 65:5798e58a58b1 18
bogdanm 65:5798e58a58b1 19 #include "TimerEvent.h"
bogdanm 65:5798e58a58b1 20 #include "FunctionPointer.h"
bogdanm 65:5798e58a58b1 21 #include "CallChain.h"
bogdanm 65:5798e58a58b1 22
bogdanm 65:5798e58a58b1 23 namespace mbed {
bogdanm 65:5798e58a58b1 24
bogdanm 65:5798e58a58b1 25 /** A Ticker is used to call a function at a recurring interval
bogdanm 65:5798e58a58b1 26 *
bogdanm 65:5798e58a58b1 27 * You can use as many seperate Ticker objects as you require.
bogdanm 65:5798e58a58b1 28 *
bogdanm 65:5798e58a58b1 29 * Example:
bogdanm 65:5798e58a58b1 30 * @code
bogdanm 65:5798e58a58b1 31 * // Toggle the blinking led after 5 seconds
bogdanm 65:5798e58a58b1 32 *
bogdanm 65:5798e58a58b1 33 * #include "mbed.h"
bogdanm 65:5798e58a58b1 34 *
bogdanm 65:5798e58a58b1 35 * Ticker timer;
bogdanm 65:5798e58a58b1 36 * DigitalOut led1(LED1);
bogdanm 65:5798e58a58b1 37 * DigitalOut led2(LED2);
bogdanm 65:5798e58a58b1 38 *
bogdanm 65:5798e58a58b1 39 * int flip = 0;
bogdanm 65:5798e58a58b1 40 *
bogdanm 65:5798e58a58b1 41 * void attime() {
bogdanm 65:5798e58a58b1 42 * flip = !flip;
bogdanm 65:5798e58a58b1 43 * }
bogdanm 65:5798e58a58b1 44 *
bogdanm 65:5798e58a58b1 45 * int main() {
bogdanm 65:5798e58a58b1 46 * timer.attach(&attime, 5);
bogdanm 65:5798e58a58b1 47 * while(1) {
bogdanm 65:5798e58a58b1 48 * if(flip == 0) {
bogdanm 65:5798e58a58b1 49 * led1 = !led1;
bogdanm 65:5798e58a58b1 50 * } else {
bogdanm 65:5798e58a58b1 51 * led2 = !led2;
bogdanm 65:5798e58a58b1 52 * }
bogdanm 65:5798e58a58b1 53 * wait(0.2);
bogdanm 65:5798e58a58b1 54 * }
bogdanm 65:5798e58a58b1 55 * }
bogdanm 65:5798e58a58b1 56 * @endcode
bogdanm 65:5798e58a58b1 57 */
bogdanm 65:5798e58a58b1 58 class Ticker : public TimerEvent {
bogdanm 65:5798e58a58b1 59
bogdanm 65:5798e58a58b1 60 public:
bogdanm 65:5798e58a58b1 61
bogdanm 65:5798e58a58b1 62 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
bogdanm 65:5798e58a58b1 63 *
bogdanm 65:5798e58a58b1 64 * @param fptr pointer to the function to be called
bogdanm 65:5798e58a58b1 65 * @param t the time between calls in seconds
bogdanm 65:5798e58a58b1 66 *
bogdanm 65:5798e58a58b1 67 * @returns
bogdanm 65:5798e58a58b1 68 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 69 */
bogdanm 65:5798e58a58b1 70 pFunctionPointer_t attach(void (*fptr)(void), float t) {
bogdanm 65:5798e58a58b1 71 return attach_us(fptr, t * 1000000.0f);
bogdanm 65:5798e58a58b1 72 }
bogdanm 65:5798e58a58b1 73
bogdanm 65:5798e58a58b1 74 /** Add a function to be called by the Ticker at the end of the call chain
bogdanm 65:5798e58a58b1 75 *
bogdanm 65:5798e58a58b1 76 * @param fptr the function to add
bogdanm 65:5798e58a58b1 77 *
bogdanm 65:5798e58a58b1 78 * @returns
bogdanm 65:5798e58a58b1 79 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 80 */
bogdanm 65:5798e58a58b1 81 pFunctionPointer_t add_function(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 82 return add_function_helper(fptr);
bogdanm 65:5798e58a58b1 83 }
bogdanm 65:5798e58a58b1 84
bogdanm 65:5798e58a58b1 85 /** Add a function to be called by the Ticker at the beginning of the call chain
bogdanm 65:5798e58a58b1 86 *
bogdanm 65:5798e58a58b1 87 * @param fptr the function to add
bogdanm 65:5798e58a58b1 88 *
bogdanm 65:5798e58a58b1 89 * @returns
bogdanm 65:5798e58a58b1 90 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 91 */
bogdanm 65:5798e58a58b1 92 pFunctionPointer_t add_function_front(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 93 return add_function_helper(fptr, true);
bogdanm 65:5798e58a58b1 94 }
bogdanm 65:5798e58a58b1 95
bogdanm 65:5798e58a58b1 96 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
bogdanm 65:5798e58a58b1 97 *
bogdanm 65:5798e58a58b1 98 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 99 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 100 * @param t the time between calls in seconds
bogdanm 65:5798e58a58b1 101 *
bogdanm 65:5798e58a58b1 102 * @returns
bogdanm 65:5798e58a58b1 103 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 104 */
bogdanm 65:5798e58a58b1 105 template<typename T>
bogdanm 65:5798e58a58b1 106 pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), float t) {
bogdanm 66:9c8f0e3462fb 107 return attach_us(tptr, mptr, t * 1000000.0f);
bogdanm 65:5798e58a58b1 108 }
bogdanm 65:5798e58a58b1 109
bogdanm 65:5798e58a58b1 110 /** Add a function to be called by the Ticker at the end of the call chain
bogdanm 65:5798e58a58b1 111 *
bogdanm 65:5798e58a58b1 112 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 113 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 114 *
bogdanm 65:5798e58a58b1 115 * @returns
bogdanm 65:5798e58a58b1 116 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 117 */
bogdanm 65:5798e58a58b1 118 template<typename T>
bogdanm 65:5798e58a58b1 119 pFunctionPointer_t add_function(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 120 return add_function_helper(tptr, mptr);
bogdanm 65:5798e58a58b1 121 }
bogdanm 65:5798e58a58b1 122
bogdanm 65:5798e58a58b1 123 /** Add a function to be called by the Ticker at the beginning of the call chain
bogdanm 65:5798e58a58b1 124 *
bogdanm 65:5798e58a58b1 125 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 126 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 127 *
bogdanm 65:5798e58a58b1 128 * @returns
bogdanm 65:5798e58a58b1 129 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 130 */
bogdanm 65:5798e58a58b1 131 template<typename T>
bogdanm 65:5798e58a58b1 132 pFunctionPointer_t add_function_front(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 133 return add_function_helper(tptr, mptr, true);
bogdanm 65:5798e58a58b1 134 }
bogdanm 65:5798e58a58b1 135
bogdanm 65:5798e58a58b1 136 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
bogdanm 65:5798e58a58b1 137 *
bogdanm 65:5798e58a58b1 138 * @param fptr pointer to the function to be called
bogdanm 65:5798e58a58b1 139 * @param t the time between calls in micro-seconds
bogdanm 65:5798e58a58b1 140 *
bogdanm 65:5798e58a58b1 141 * @returns
bogdanm 65:5798e58a58b1 142 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 143 */
bogdanm 65:5798e58a58b1 144 pFunctionPointer_t attach_us(void (*fptr)(void), unsigned int t) {
bogdanm 66:9c8f0e3462fb 145 _chain.clear();
bogdanm 65:5798e58a58b1 146 pFunctionPointer_t pf = _chain.add(fptr);
bogdanm 65:5798e58a58b1 147 setup(t);
bogdanm 65:5798e58a58b1 148 return pf;
bogdanm 65:5798e58a58b1 149 }
bogdanm 65:5798e58a58b1 150
bogdanm 65:5798e58a58b1 151 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
bogdanm 65:5798e58a58b1 152 *
bogdanm 65:5798e58a58b1 153 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 154 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 155 * @param t the time between calls in micro-seconds
bogdanm 65:5798e58a58b1 156 *
bogdanm 65:5798e58a58b1 157 * @returns
bogdanm 65:5798e58a58b1 158 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 159 */
bogdanm 65:5798e58a58b1 160 template<typename T>
bogdanm 65:5798e58a58b1 161 pFunctionPointer_t attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
bogdanm 66:9c8f0e3462fb 162 _chain.clear();
bogdanm 66:9c8f0e3462fb 163 pFunctionPointer_t pf = _chain.add(tptr, mptr);
bogdanm 65:5798e58a58b1 164 setup(t);
bogdanm 65:5798e58a58b1 165 return pf;
bogdanm 65:5798e58a58b1 166 }
bogdanm 65:5798e58a58b1 167
bogdanm 65:5798e58a58b1 168 /** Detach the function
bogdanm 65:5798e58a58b1 169 */
bogdanm 65:5798e58a58b1 170 void detach();
bogdanm 65:5798e58a58b1 171
bogdanm 65:5798e58a58b1 172 /** Remove a function from the Ticker's call chain
bogdanm 65:5798e58a58b1 173 *
bogdanm 65:5798e58a58b1 174 * @param pf the function object to remove
bogdanm 65:5798e58a58b1 175 *
bogdanm 65:5798e58a58b1 176 * @returns
bogdanm 65:5798e58a58b1 177 * true if the function was found and removed, false otherwise
bogdanm 65:5798e58a58b1 178 */
bogdanm 65:5798e58a58b1 179 bool remove_function(pFunctionPointer_t pf) {
bogdanm 65:5798e58a58b1 180 bool res = _chain.remove(pf);
bogdanm 65:5798e58a58b1 181 if (res && _chain.size() == 0)
bogdanm 65:5798e58a58b1 182 detach();
bogdanm 65:5798e58a58b1 183 return res;
bogdanm 65:5798e58a58b1 184 }
bogdanm 65:5798e58a58b1 185
bogdanm 65:5798e58a58b1 186 protected:
bogdanm 65:5798e58a58b1 187 void setup(unsigned int t);
bogdanm 65:5798e58a58b1 188 pFunctionPointer_t add_function_helper(void (*fptr)(void), bool front=false);
bogdanm 65:5798e58a58b1 189 virtual void handler();
bogdanm 65:5798e58a58b1 190
bogdanm 65:5798e58a58b1 191 template<typename T>
bogdanm 65:5798e58a58b1 192 pFunctionPointer_t add_function_helper(T* tptr, void (T::*mptr)(void), bool front=false) {
bogdanm 65:5798e58a58b1 193 if (_chain.size() == 0)
bogdanm 65:5798e58a58b1 194 return NULL;
bogdanm 65:5798e58a58b1 195 return front ? _chain.add_front(tptr, mptr) : _chain.add(tptr, mptr);
bogdanm 65:5798e58a58b1 196 }
bogdanm 65:5798e58a58b1 197
bogdanm 65:5798e58a58b1 198 unsigned int _delay;
bogdanm 65:5798e58a58b1 199 CallChain _chain;
bogdanm 65:5798e58a58b1 200 };
bogdanm 65:5798e58a58b1 201
bogdanm 65:5798e58a58b1 202 } // namespace mbed
bogdanm 65:5798e58a58b1 203
bogdanm 65:5798e58a58b1 204 #endif