svn / mbed / trunk / Ticker.h

Revision 29, 2.8 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

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