printer

Dependents:   Good_Serial_HelloWorld_Mbed

Fork of mbed by gokmen ascioglu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ticker.h Source File

Ticker.h

00001 /* mbed Microcontroller Library - Ticker
00002  * Copyright (c) 2007-2009 ARM Limited. All rights reserved.
00003  */ 
00004 
00005 #ifndef MBED_TICKER_H
00006 #define MBED_TICKER_H
00007 
00008 #include "TimerEvent.h"
00009 #include "FunctionPointer.h"
00010 
00011 namespace mbed {
00012 
00013 /* Class: Ticker
00014  *  A Ticker is used to call a function at a recurring interval
00015  *
00016  * You can use as many seperate Ticker objects as you require. 
00017  *
00018  * Example:
00019  * > // Toggle the blinking led after 5 seconds
00020  * >
00021  * > #include "mbed.h"
00022  * > 
00023  * > Ticker timer;
00024  * > DigitalOut led1(LED1);
00025  * > DigitalOut led2(LED2);
00026  * > 
00027  * > int flip = 0;
00028  * > 
00029  * > void attime() {
00030  * >     flip = !flip;
00031  * > }
00032  * >
00033  * > int main() {
00034  * >     timer.attach(&attime, 5);
00035  * >     while(1) {
00036  * >         if(flip == 0) {
00037  * >             led1 = !led1;
00038  * >         } else {
00039  * >             led2 = !led2;
00040  * >         }
00041  * >         wait(0.2);
00042  * >     }
00043  * > }
00044  *
00045  */
00046 class Ticker : public TimerEvent {
00047 
00048 public:
00049 
00050     /* Function: attach
00051      *  Attach a function to be called by the Ticker, specifiying the interval in seconds
00052      *     
00053      * Variables:
00054      *  fptr - pointer to the function to be called
00055      *  t - the time between calls in seconds
00056      */
00057     void attach(void (*fptr)(void), float t) {
00058         attach_us(fptr, t * 1000000.0f);
00059     }
00060     
00061     /* Function: attach
00062      *  Attach a member function to be called by the Ticker, specifiying the interval in seconds
00063      *     
00064      * Variables:
00065      *  tptr - pointer to the object to call the member function on
00066      *  mptr - pointer to the member function to be called
00067      *  t - the time between calls in seconds
00068      */
00069     template<typename T>
00070     void attach(T* tptr, void (T::*mptr)(void), float t) {
00071         attach_us(tptr, mptr, t * 1000000.0f);
00072     }
00073     
00074     /* Function: attach_us
00075      *  Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
00076      *     
00077      * Variables:
00078      *  fptr - pointer to the function to be called
00079      *  t - the time between calls in micro-seconds
00080      */
00081     void attach_us(void (*fptr)(void), unsigned int t) {
00082         _function.attach(fptr);
00083         setup(t);
00084     }
00085 
00086     /* Function: attach_us
00087      *  Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
00088      *     
00089      * Variables:
00090      *  tptr - pointer to the object to call the member function on
00091      *  mptr - pointer to the member function to be called
00092      *  t - the time between calls in micro-seconds
00093      */    
00094     template<typename T>
00095     void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
00096         _function.attach(tptr, mptr);
00097         setup(t);
00098     }
00099     
00100     /* Function: detach
00101      *  Detach the function
00102      */        
00103     void detach();
00104 
00105 protected:
00106 
00107     void setup(unsigned int t);
00108     virtual void handler();
00109 
00110     unsigned int _delay;
00111     FunctionPointer _function;
00112 
00113 };
00114 
00115 } // namespace mbed
00116 
00117 #endif