mbed library sources

Dependents:   RPC_Serial_V_mac

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  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_TICKER_H
00017 #define MBED_TICKER_H
00018 
00019 #include "TimerEvent.h"
00020 #include "FunctionPointer.h"
00021 
00022 namespace mbed {
00023 
00024 /** A Ticker is used to call a function at a recurring interval
00025  *
00026  *  You can use as many seperate Ticker objects as you require.
00027  *
00028  * Example:
00029  * @code
00030  * // Toggle the blinking led after 5 seconds
00031  *
00032  * #include "mbed.h"
00033  *
00034  * Ticker timer;
00035  * DigitalOut led1(LED1);
00036  * DigitalOut led2(LED2);
00037  *
00038  * int flip = 0;
00039  *
00040  * void attime() {
00041  *     flip = !flip;
00042  * }
00043  *
00044  * int main() {
00045  *     timer.attach(&attime, 5);
00046  *     while(1) {
00047  *         if(flip == 0) {
00048  *             led1 = !led1;
00049  *         } else {
00050  *             led2 = !led2;
00051  *         }
00052  *         wait(0.2);
00053  *     }
00054  * }
00055  * @endcode
00056  */
00057 class Ticker : public TimerEvent {
00058 
00059 public:
00060     Ticker() : TimerEvent() {
00061     }
00062 
00063     Ticker(const ticker_data_t *data) : TimerEvent(data) {
00064     }
00065 
00066     /** Attach a function to be called by the Ticker, specifiying the interval in seconds
00067      *
00068      *  @param fptr pointer to the function to be called
00069      *  @param t the time between calls in seconds
00070      */
00071     void attach(void (*fptr)(void), float t) {
00072         attach_us(fptr, t * 1000000.0f);
00073     }
00074 
00075     /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
00076      *
00077      *  @param tptr pointer to the object to call the member function on
00078      *  @param mptr pointer to the member function to be called
00079      *  @param t the time between calls in seconds
00080      */
00081     template<typename T>
00082     void attach(T* tptr, void (T::*mptr)(void), float t) {
00083         attach_us(tptr, mptr, t * 1000000.0f);
00084     }
00085 
00086     /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
00087      *
00088      *  @param fptr pointer to the function to be called
00089      *  @param t the time between calls in micro-seconds
00090      */
00091     void attach_us(void (*fptr)(void), timestamp_t t) {
00092         _function.attach(fptr);
00093         setup(t);
00094     }
00095 
00096     /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
00097      *
00098      *  @param tptr pointer to the object to call the member function on
00099      *  @param mptr pointer to the member function to be called
00100      *  @param t the time between calls in micro-seconds
00101      */
00102     template<typename T>
00103     void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
00104         _function.attach(tptr, mptr);
00105         setup(t);
00106     }
00107 
00108     virtual ~Ticker() {
00109         detach();
00110     }
00111 
00112     /** Detach the function
00113      */
00114     void detach();
00115 
00116 protected:
00117     void setup(timestamp_t t);
00118     virtual void handler();
00119 
00120 protected:
00121     timestamp_t     _delay;     /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
00122     FunctionPointer  _function;  /**< Callback. */
00123 };
00124 
00125 } // namespace mbed
00126 
00127 #endif