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