1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "TimerEvent.h"
20 #include "FunctionPointer.h"
24 /** A Ticker is used to call a function at a recurring interval
26 * You can use as many seperate Ticker objects as you require.
30 * // Toggle the blinking led after 5 seconds
35 * DigitalOut led1(LED1);
36 * DigitalOut led2(LED2);
45 * timer.attach(&attime, 5);
57 class Ticker : public TimerEvent {
61 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
63 * @param fptr pointer to the function to be called
64 * @param t the time between calls in seconds
66 void attach(void (*fptr)(void), float t) {
67 attach_us(fptr, t * 1000000.0f);
70 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
72 * @param tptr pointer to the object to call the member function on
73 * @param mptr pointer to the member function to be called
74 * @param t the time between calls in seconds
77 void attach(T* tptr, void (T::*mptr)(void), float t) {
78 attach_us(tptr, mptr, t * 1000000.0f);
81 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
83 * @param fptr pointer to the function to be called
84 * @param t the time between calls in micro-seconds
86 void attach_us(void (*fptr)(void), timestamp_t t) {
87 _function.attach(fptr);
91 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
93 * @param tptr pointer to the object to call the member function on
94 * @param mptr pointer to the member function to be called
95 * @param t the time between calls in micro-seconds
98 void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
99 _function.attach(tptr, mptr);
107 /** Detach the function
112 void setup(timestamp_t t);
113 virtual void handler();
116 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
117 FunctionPointer _function; /**< Callback. */