]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/Ticker.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / api / Ticker.h
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 #ifndef MBED_TICKER_H
17 #define MBED_TICKER_H
18
19 #include "TimerEvent.h"
20 #include "FunctionPointer.h"
21
22 namespace mbed {
23
24 /** A Ticker is used to call a function at a recurring interval
25 *
26 * You can use as many seperate Ticker objects as you require.
27 *
28 * Example:
29 * @code
30 * // Toggle the blinking led after 5 seconds
31 *
32 * #include "mbed.h"
33 *
34 * Ticker timer;
35 * DigitalOut led1(LED1);
36 * DigitalOut led2(LED2);
37 *
38 * int flip = 0;
39 *
40 * void attime() {
41 * flip = !flip;
42 * }
43 *
44 * int main() {
45 * timer.attach(&attime, 5);
46 * while(1) {
47 * if(flip == 0) {
48 * led1 = !led1;
49 * } else {
50 * led2 = !led2;
51 * }
52 * wait(0.2);
53 * }
54 * }
55 * @endcode
56 */
57 class Ticker : public TimerEvent {
58
59 public:
60
61 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
62 *
63 * @param fptr pointer to the function to be called
64 * @param t the time between calls in seconds
65 */
66 void attach(void (*fptr)(void), float t) {
67 attach_us(fptr, t * 1000000.0f);
68 }
69
70 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
71 *
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
75 */
76 template<typename T>
77 void attach(T* tptr, void (T::*mptr)(void), float t) {
78 attach_us(tptr, mptr, t * 1000000.0f);
79 }
80
81 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
82 *
83 * @param fptr pointer to the function to be called
84 * @param t the time between calls in micro-seconds
85 */
86 void attach_us(void (*fptr)(void), timestamp_t t) {
87 _function.attach(fptr);
88 setup(t);
89 }
90
91 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
92 *
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
96 */
97 template<typename T>
98 void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
99 _function.attach(tptr, mptr);
100 setup(t);
101 }
102
103 virtual ~Ticker() {
104 detach();
105 }
106
107 /** Detach the function
108 */
109 void detach();
110
111 protected:
112 void setup(timestamp_t t);
113 virtual void handler();
114
115 protected:
116 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
117 FunctionPointer _function; /**< Callback. */
118 };
119
120 } // namespace mbed
121
122 #endif
Imprint / Impressum