re-enabling serial
[clinton/Smoothieware.git] / gcc4mbed / external / mbed / .svn / text-base / Ticker.h.svn-base
1 /* mbed Microcontroller Library - Ticker
2 * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
3 * sford
4 */
5
6 #ifndef MBED_TICKER_H
7 #define MBED_TICKER_H
8
9 #include "TimerEvent.h"
10 #include "FunctionPointer.h"
11
12 namespace mbed {
13
14 /* Class: Ticker
15 * A Ticker is used to call a function at a recurring interval
16 *
17 * You can use as many seperate Ticker objects as you require.
18 *
19 * Example:
20 * > // Toggle the blinking led after 5 seconds
21 * >
22 * > #include "mbed.h"
23 * >
24 * > Ticker timer;
25 * > DigitalOut led1(LED1);
26 * > DigitalOut led2(LED2);
27 * >
28 * > int flip = 0;
29 * >
30 * > void attime() {
31 * > flip = !flip;
32 * > }
33 * >
34 * > int main() {
35 * > timer.attach(&attime, 5);
36 * > while(1) {
37 * > if(flip == 0) {
38 * > led1 = !led1;
39 * > } else {
40 * > led2 = !led2;
41 * > }
42 * > wait(0.2);
43 * > }
44 * > }
45 *
46 */
47 class Ticker : public TimerEvent {
48
49 public:
50
51 /* Function: attach
52 * Attach a function to be called by the Ticker, specifiying the interval in seconds
53 *
54 * Variables:
55 * fptr - pointer to the function to be called
56 * t - the time between calls in seconds
57 */
58 void attach(void (*fptr)(void), float t) {
59 attach_us(fptr, t * 1000000.0f);
60 }
61
62 /* Function: attach
63 * Attach a member function to be called by the Ticker, specifiying the interval in seconds
64 *
65 * Variables:
66 * tptr - pointer to the object to call the member function on
67 * mptr - pointer to the member function to be called
68 * t - the time between calls in seconds
69 */
70 template<typename T>
71 void attach(T* tptr, void (T::*mptr)(void), float t) {
72 attach_us(tptr, mptr, t * 1000000.0f);
73 }
74
75 /* Function: attach_us
76 * Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
77 *
78 * Variables:
79 * fptr - pointer to the function to be called
80 * t - the time between calls in micro-seconds
81 */
82 void attach_us(void (*fptr)(void), unsigned int t) {
83 _function.attach(fptr);
84 setup(t);
85 }
86
87 /* Function: attach_us
88 * Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
89 *
90 * Variables:
91 * tptr - pointer to the object to call the member function on
92 * mptr - pointer to the member function to be called
93 * t - the time between calls in micro-seconds
94 */
95 template<typename T>
96 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
97 _function.attach(tptr, mptr);
98 setup(t);
99 }
100
101 /* Function: detach
102 * Detach the function
103 */
104 void detach();
105
106 protected:
107
108 void setup(unsigned int t);
109 virtual void handler();
110
111 unsigned int _delay;
112 FunctionPointer _function;
113
114 };
115
116 } // namespace mbed
117
118 #endif