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