mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
bogdanm
Date:
Mon Aug 19 18:17:02 2013 +0300
Revision:
19:398f4c622e1b
Parent:
15:4892fe388435
Child:
36:ab3ee77451e7
Sync with official mbed library release 66

Who changed what in which revision?

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