Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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