initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #ifndef MBED_TICKER_H
yihui 0:638edba3adf6 17 #define MBED_TICKER_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "TimerEvent.h"
yihui 0:638edba3adf6 20 #include "FunctionPointer.h"
yihui 0:638edba3adf6 21
yihui 0:638edba3adf6 22 namespace mbed {
yihui 0:638edba3adf6 23
yihui 0:638edba3adf6 24 /** A Ticker is used to call a function at a recurring interval
yihui 0:638edba3adf6 25 *
yihui 0:638edba3adf6 26 * You can use as many seperate Ticker objects as you require.
yihui 0:638edba3adf6 27 *
yihui 0:638edba3adf6 28 * Example:
yihui 0:638edba3adf6 29 * @code
yihui 0:638edba3adf6 30 * // Toggle the blinking led after 5 seconds
yihui 0:638edba3adf6 31 *
yihui 0:638edba3adf6 32 * #include "mbed.h"
yihui 0:638edba3adf6 33 *
yihui 0:638edba3adf6 34 * Ticker timer;
yihui 0:638edba3adf6 35 * DigitalOut led1(LED1);
yihui 0:638edba3adf6 36 * DigitalOut led2(LED2);
yihui 0:638edba3adf6 37 *
yihui 0:638edba3adf6 38 * int flip = 0;
yihui 0:638edba3adf6 39 *
yihui 0:638edba3adf6 40 * void attime() {
yihui 0:638edba3adf6 41 * flip = !flip;
yihui 0:638edba3adf6 42 * }
yihui 0:638edba3adf6 43 *
yihui 0:638edba3adf6 44 * int main() {
yihui 0:638edba3adf6 45 * timer.attach(&attime, 5);
yihui 0:638edba3adf6 46 * while(1) {
yihui 0:638edba3adf6 47 * if(flip == 0) {
yihui 0:638edba3adf6 48 * led1 = !led1;
yihui 0:638edba3adf6 49 * } else {
yihui 0:638edba3adf6 50 * led2 = !led2;
yihui 0:638edba3adf6 51 * }
yihui 0:638edba3adf6 52 * wait(0.2);
yihui 0:638edba3adf6 53 * }
yihui 0:638edba3adf6 54 * }
yihui 0:638edba3adf6 55 * @endcode
yihui 0:638edba3adf6 56 */
yihui 0:638edba3adf6 57 class Ticker : public TimerEvent {
yihui 0:638edba3adf6 58
yihui 0:638edba3adf6 59 public:
yihui 0:638edba3adf6 60 Ticker() : TimerEvent() {
yihui 0:638edba3adf6 61 }
yihui 0:638edba3adf6 62
yihui 0:638edba3adf6 63 Ticker(const ticker_data_t *data) : TimerEvent(data) {
yihui 0:638edba3adf6 64 }
yihui 0:638edba3adf6 65
yihui 0:638edba3adf6 66 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
yihui 0:638edba3adf6 67 *
yihui 0:638edba3adf6 68 * @param fptr pointer to the function to be called
yihui 0:638edba3adf6 69 * @param t the time between calls in seconds
yihui 0:638edba3adf6 70 */
yihui 0:638edba3adf6 71 void attach(void (*fptr)(void), float t) {
yihui 0:638edba3adf6 72 attach_us(fptr, t * 1000000.0f);
yihui 0:638edba3adf6 73 }
yihui 0:638edba3adf6 74
yihui 0:638edba3adf6 75 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
yihui 0:638edba3adf6 76 *
yihui 0:638edba3adf6 77 * @param tptr pointer to the object to call the member function on
yihui 0:638edba3adf6 78 * @param mptr pointer to the member function to be called
yihui 0:638edba3adf6 79 * @param t the time between calls in seconds
yihui 0:638edba3adf6 80 */
yihui 0:638edba3adf6 81 template<typename T>
yihui 0:638edba3adf6 82 void attach(T* tptr, void (T::*mptr)(void), float t) {
yihui 0:638edba3adf6 83 attach_us(tptr, mptr, t * 1000000.0f);
yihui 0:638edba3adf6 84 }
yihui 0:638edba3adf6 85
yihui 0:638edba3adf6 86 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
yihui 0:638edba3adf6 87 *
yihui 0:638edba3adf6 88 * @param fptr pointer to the function to be called
yihui 0:638edba3adf6 89 * @param t the time between calls in micro-seconds
yihui 0:638edba3adf6 90 */
yihui 0:638edba3adf6 91 void attach_us(void (*fptr)(void), timestamp_t t) {
yihui 0:638edba3adf6 92 _function.attach(fptr);
yihui 0:638edba3adf6 93 setup(t);
yihui 0:638edba3adf6 94 }
yihui 0:638edba3adf6 95
yihui 0:638edba3adf6 96 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
yihui 0:638edba3adf6 97 *
yihui 0:638edba3adf6 98 * @param tptr pointer to the object to call the member function on
yihui 0:638edba3adf6 99 * @param mptr pointer to the member function to be called
yihui 0:638edba3adf6 100 * @param t the time between calls in micro-seconds
yihui 0:638edba3adf6 101 */
yihui 0:638edba3adf6 102 template<typename T>
yihui 0:638edba3adf6 103 void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
yihui 0:638edba3adf6 104 _function.attach(tptr, mptr);
yihui 0:638edba3adf6 105 setup(t);
yihui 0:638edba3adf6 106 }
yihui 0:638edba3adf6 107
yihui 0:638edba3adf6 108 virtual ~Ticker() {
yihui 0:638edba3adf6 109 detach();
yihui 0:638edba3adf6 110 }
yihui 0:638edba3adf6 111
yihui 0:638edba3adf6 112 /** Detach the function
yihui 0:638edba3adf6 113 */
yihui 0:638edba3adf6 114 void detach();
yihui 0:638edba3adf6 115
yihui 0:638edba3adf6 116 protected:
yihui 0:638edba3adf6 117 void setup(timestamp_t t);
yihui 0:638edba3adf6 118 virtual void handler();
yihui 0:638edba3adf6 119
yihui 0:638edba3adf6 120 protected:
yihui 0:638edba3adf6 121 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
yihui 0:638edba3adf6 122 FunctionPointer _function; /**< Callback. */
yihui 0:638edba3adf6 123 };
yihui 0:638edba3adf6 124
yihui 0:638edba3adf6 125 } // namespace mbed
yihui 0:638edba3adf6 126
yihui 0:638edba3adf6 127 #endif