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