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

Fork of mbed by mbed official

Committer:
ldyz
Date:
Fri Jul 05 13:16:13 2013 +0000
Revision:
64:75c1708b266b
Parent:
59:0883845fe643
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 44:24d45a770a51 1 /* mbed Microcontroller Library
emilmont 54:71b101360fb9 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 44:24d45a770a51 3 *
emilmont 59:0883845fe643 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 59:0883845fe643 5 * you may not use this file except in compliance with the License.
emilmont 59:0883845fe643 6 * You may obtain a copy of the License at
emilmont 59:0883845fe643 7 *
emilmont 59:0883845fe643 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 44:24d45a770a51 9 *
emilmont 59:0883845fe643 10 * Unless required by applicable law or agreed to in writing, software
emilmont 59:0883845fe643 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 59:0883845fe643 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 59:0883845fe643 13 * See the License for the specific language governing permissions and
emilmont 59:0883845fe643 14 * limitations under the License.
emilmont 44:24d45a770a51 15 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 16 #ifndef MBED_TICKER_H
simon.ford@mbed.co.uk 9:cf0d45ce28a6 17 #define MBED_TICKER_H
simon.ford@mbed.co.uk 9:cf0d45ce28a6 18
simon.ford@mbed.co.uk 9:cf0d45ce28a6 19 #include "TimerEvent.h"
rolf.meyer@arm.com 11:1c1ebd0324fa 20 #include "FunctionPointer.h"
simon.ford@mbed.co.uk 9:cf0d45ce28a6 21
simon.ford@mbed.co.uk 9:cf0d45ce28a6 22 namespace mbed {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 23
emilmont 43:e2ed12d17f06 24 /** A Ticker is used to call a function at a recurring interval
simon.ford@mbed.co.uk 9:cf0d45ce28a6 25 *
emilmont 55:d722ed6a4237 26 * You can use as many seperate Ticker objects as you require.
rolf.meyer@arm.com 11:1c1ebd0324fa 27 *
rolf.meyer@arm.com 11:1c1ebd0324fa 28 * Example:
emilmont 43:e2ed12d17f06 29 * @code
emilmont 43:e2ed12d17f06 30 * // Toggle the blinking led after 5 seconds
rolf.meyer@arm.com 11:1c1ebd0324fa 31 *
emilmont 43:e2ed12d17f06 32 * #include "mbed.h"
emilmont 44:24d45a770a51 33 *
emilmont 43:e2ed12d17f06 34 * Ticker timer;
emilmont 43:e2ed12d17f06 35 * DigitalOut led1(LED1);
emilmont 43:e2ed12d17f06 36 * DigitalOut led2(LED2);
emilmont 55:d722ed6a4237 37 *
emilmont 43:e2ed12d17f06 38 * int flip = 0;
emilmont 55:d722ed6a4237 39 *
emilmont 43:e2ed12d17f06 40 * void attime() {
emilmont 43:e2ed12d17f06 41 * flip = !flip;
emilmont 43:e2ed12d17f06 42 * }
emilmont 43:e2ed12d17f06 43 *
emilmont 43:e2ed12d17f06 44 * int main() {
emilmont 43:e2ed12d17f06 45 * timer.attach(&attime, 5);
emilmont 43:e2ed12d17f06 46 * while(1) {
emilmont 43:e2ed12d17f06 47 * if(flip == 0) {
emilmont 43:e2ed12d17f06 48 * led1 = !led1;
emilmont 43:e2ed12d17f06 49 * } else {
emilmont 43:e2ed12d17f06 50 * led2 = !led2;
emilmont 43:e2ed12d17f06 51 * }
emilmont 43:e2ed12d17f06 52 * wait(0.2);
emilmont 43:e2ed12d17f06 53 * }
emilmont 43:e2ed12d17f06 54 * }
emilmont 43:e2ed12d17f06 55 * @endcode
simon.ford@mbed.co.uk 9:cf0d45ce28a6 56 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 57 class Ticker : public TimerEvent {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 58
simon.ford@mbed.co.uk 9:cf0d45ce28a6 59 public:
simon.ford@mbed.co.uk 9:cf0d45ce28a6 60
emilmont 43:e2ed12d17f06 61 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
emilmont 44:24d45a770a51 62 *
emilmont 43:e2ed12d17f06 63 * @param fptr pointer to the function to be called
emilmont 43:e2ed12d17f06 64 * @param t the time between calls in seconds
simon.ford@mbed.co.uk 9:cf0d45ce28a6 65 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 66 void attach(void (*fptr)(void), float t) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 67 attach_us(fptr, t * 1000000.0f);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 68 }
emilmont 55:d722ed6a4237 69
emilmont 43:e2ed12d17f06 70 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
emilmont 44:24d45a770a51 71 *
emilmont 43:e2ed12d17f06 72 * @param tptr pointer to the object to call the member function on
emilmont 43:e2ed12d17f06 73 * @param mptr pointer to the member function to be called
emilmont 43:e2ed12d17f06 74 * @param t the time between calls in seconds
simon.ford@mbed.co.uk 9:cf0d45ce28a6 75 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 76 template<typename T>
simon.ford@mbed.co.uk 9:cf0d45ce28a6 77 void attach(T* tptr, void (T::*mptr)(void), float t) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 78 attach_us(tptr, mptr, t * 1000000.0f);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 79 }
emilmont 55:d722ed6a4237 80
emilmont 43:e2ed12d17f06 81 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
emilmont 44:24d45a770a51 82 *
emilmont 43:e2ed12d17f06 83 * @param fptr pointer to the function to be called
emilmont 43:e2ed12d17f06 84 * @param t the time between calls in micro-seconds
simon.ford@mbed.co.uk 9:cf0d45ce28a6 85 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 86 void attach_us(void (*fptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 87 _function.attach(fptr);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 88 setup(t);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 89 }
simon.ford@mbed.co.uk 9:cf0d45ce28a6 90
emilmont 43:e2ed12d17f06 91 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
emilmont 44:24d45a770a51 92 *
emilmont 43:e2ed12d17f06 93 * @param tptr pointer to the object to call the member function on
emilmont 43:e2ed12d17f06 94 * @param mptr pointer to the member function to be called
emilmont 43:e2ed12d17f06 95 * @param t the time between calls in micro-seconds
emilmont 55:d722ed6a4237 96 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 97 template<typename T>
simon.ford@mbed.co.uk 9:cf0d45ce28a6 98 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
simon.ford@mbed.co.uk 9:cf0d45ce28a6 99 _function.attach(tptr, mptr);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 100 setup(t);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 101 }
emilmont 55:d722ed6a4237 102
emilmont 43:e2ed12d17f06 103 /** Detach the function
emilmont 44:24d45a770a51 104 */
simon.ford@mbed.co.uk 9:cf0d45ce28a6 105 void detach();
rolf.meyer@arm.com 11:1c1ebd0324fa 106
simon.ford@mbed.co.uk 9:cf0d45ce28a6 107 protected:
simon.ford@mbed.co.uk 9:cf0d45ce28a6 108 void setup(unsigned int t);
simon.ford@mbed.co.uk 9:cf0d45ce28a6 109 virtual void handler();
emilmont 55:d722ed6a4237 110
rolf.meyer@arm.com 11:1c1ebd0324fa 111 unsigned int _delay;
rolf.meyer@arm.com 11:1c1ebd0324fa 112 FunctionPointer _function;
rolf.meyer@arm.com 11:1c1ebd0324fa 113 };
simon.ford@mbed.co.uk 9:cf0d45ce28a6 114
rolf.meyer@arm.com 11:1c1ebd0324fa 115 } // namespace mbed
simon.ford@mbed.co.uk 9:cf0d45ce28a6 116
simon.ford@mbed.co.uk 9:cf0d45ce28a6 117 #endif