Christian Weiß / Mbed 2 deprecated Diplomarbeit_MW_CW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ticker.h Source File

Ticker.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_TICKER_H
00023 #define MBED_TICKER_H
00024 
00025 #include "TimerEvent.h"
00026 #include "FunctionPointer.h"
00027 
00028 namespace mbed {
00029 
00030 /** A Ticker is used to call a function at a recurring interval
00031  *
00032  *  You can use as many seperate Ticker objects as you require.
00033  *
00034  * Example:
00035  * @code
00036  * // Toggle the blinking led after 5 seconds
00037  *
00038  * #include "mbed.h"
00039  *
00040  * Ticker timer;
00041  * DigitalOut led1(LED1);
00042  * DigitalOut led2(LED2);
00043  *
00044  * int flip = 0;
00045  *
00046  * void attime() {
00047  *     flip = !flip;
00048  * }
00049  *
00050  * int main() {
00051  *     timer.attach(&attime, 5);
00052  *     while(1) {
00053  *         if(flip == 0) {
00054  *             led1 = !led1;
00055  *         } else {
00056  *             led2 = !led2;
00057  *         }
00058  *         wait(0.2);
00059  *     }
00060  * }
00061  * @endcode
00062  */
00063 class Ticker : public TimerEvent {
00064 
00065 public:
00066 
00067     /** Attach a function to be called by the Ticker, specifiying the interval in seconds
00068      *
00069      *  @param fptr pointer to the function to be called
00070      *  @param t the time between calls in seconds
00071      */
00072     void attach(void (*fptr)(void), float t) {
00073         attach_us(fptr, t * 1000000.0f);
00074     }
00075 
00076     /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
00077      *
00078      *  @param tptr pointer to the object to call the member function on
00079      *  @param mptr pointer to the member function to be called
00080      *  @param t the time between calls in seconds
00081      */
00082     template<typename T>
00083     void attach(T* tptr, void (T::*mptr)(void), float t) {
00084         attach_us(tptr, mptr, t * 1000000.0f);
00085     }
00086 
00087     /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
00088      *
00089      *  @param fptr pointer to the function to be called
00090      *  @param t the time between calls in micro-seconds
00091      */
00092     void attach_us(void (*fptr)(void), unsigned int t) {
00093         _function.attach(fptr);
00094         setup(t);
00095     }
00096 
00097     /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
00098      *
00099      *  @param tptr pointer to the object to call the member function on
00100      *  @param mptr pointer to the member function to be called
00101      *  @param t the time between calls in micro-seconds
00102      */
00103     template<typename T>
00104     void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
00105         _function.attach(tptr, mptr);
00106         setup(t);
00107     }
00108 
00109     /** Detach the function
00110      */
00111     void detach();
00112 
00113 protected:
00114     void setup(unsigned int t);
00115     virtual void handler();
00116 
00117     unsigned int _delay;
00118     FunctionPointer _function;
00119 };
00120 
00121 } // namespace mbed
00122 
00123 #endif
00124