mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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