test test test
snake-test/mbed/drivers/Ticker.h@0:e4c5e6ec922e, 2020-05-25 (annotated)
- Committer:
- mohamedmoawya
- Date:
- Mon May 25 19:06:11 2020 +0000
- Revision:
- 0:e4c5e6ec922e
snake game tteest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mohamedmoawya | 0:e4c5e6ec922e | 1 | /* mbed Microcontroller Library |
mohamedmoawya | 0:e4c5e6ec922e | 2 | * Copyright (c) 2006-2013 ARM Limited |
mohamedmoawya | 0:e4c5e6ec922e | 3 | * |
mohamedmoawya | 0:e4c5e6ec922e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mohamedmoawya | 0:e4c5e6ec922e | 5 | * you may not use this file except in compliance with the License. |
mohamedmoawya | 0:e4c5e6ec922e | 6 | * You may obtain a copy of the License at |
mohamedmoawya | 0:e4c5e6ec922e | 7 | * |
mohamedmoawya | 0:e4c5e6ec922e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mohamedmoawya | 0:e4c5e6ec922e | 9 | * |
mohamedmoawya | 0:e4c5e6ec922e | 10 | * Unless required by applicable law or agreed to in writing, software |
mohamedmoawya | 0:e4c5e6ec922e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mohamedmoawya | 0:e4c5e6ec922e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mohamedmoawya | 0:e4c5e6ec922e | 13 | * See the License for the specific language governing permissions and |
mohamedmoawya | 0:e4c5e6ec922e | 14 | * limitations under the License. |
mohamedmoawya | 0:e4c5e6ec922e | 15 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 16 | #ifndef MBED_TICKER_H |
mohamedmoawya | 0:e4c5e6ec922e | 17 | #define MBED_TICKER_H |
mohamedmoawya | 0:e4c5e6ec922e | 18 | |
mohamedmoawya | 0:e4c5e6ec922e | 19 | #include "drivers/TimerEvent.h" |
mohamedmoawya | 0:e4c5e6ec922e | 20 | #include "platform/Callback.h" |
mohamedmoawya | 0:e4c5e6ec922e | 21 | #include "platform/mbed_toolchain.h" |
mohamedmoawya | 0:e4c5e6ec922e | 22 | #include "platform/NonCopyable.h" |
mohamedmoawya | 0:e4c5e6ec922e | 23 | #include "platform/mbed_power_mgmt.h" |
mohamedmoawya | 0:e4c5e6ec922e | 24 | #include "hal/lp_ticker_api.h" |
mohamedmoawya | 0:e4c5e6ec922e | 25 | #include "platform/mbed_critical.h" |
mohamedmoawya | 0:e4c5e6ec922e | 26 | |
mohamedmoawya | 0:e4c5e6ec922e | 27 | namespace mbed { |
mohamedmoawya | 0:e4c5e6ec922e | 28 | /** \addtogroup drivers */ |
mohamedmoawya | 0:e4c5e6ec922e | 29 | |
mohamedmoawya | 0:e4c5e6ec922e | 30 | /** A Ticker is used to call a function at a recurring interval |
mohamedmoawya | 0:e4c5e6ec922e | 31 | * |
mohamedmoawya | 0:e4c5e6ec922e | 32 | * You can use as many separate Ticker objects as you require. |
mohamedmoawya | 0:e4c5e6ec922e | 33 | * |
mohamedmoawya | 0:e4c5e6ec922e | 34 | * @note Synchronization level: Interrupt safe |
mohamedmoawya | 0:e4c5e6ec922e | 35 | * |
mohamedmoawya | 0:e4c5e6ec922e | 36 | * Example: |
mohamedmoawya | 0:e4c5e6ec922e | 37 | * @code |
mohamedmoawya | 0:e4c5e6ec922e | 38 | * // Toggle the blinking LED after 5 seconds |
mohamedmoawya | 0:e4c5e6ec922e | 39 | * |
mohamedmoawya | 0:e4c5e6ec922e | 40 | * #include "mbed.h" |
mohamedmoawya | 0:e4c5e6ec922e | 41 | * |
mohamedmoawya | 0:e4c5e6ec922e | 42 | * Ticker timer; |
mohamedmoawya | 0:e4c5e6ec922e | 43 | * DigitalOut led1(LED1); |
mohamedmoawya | 0:e4c5e6ec922e | 44 | * DigitalOut led2(LED2); |
mohamedmoawya | 0:e4c5e6ec922e | 45 | * |
mohamedmoawya | 0:e4c5e6ec922e | 46 | * int flip = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 47 | * |
mohamedmoawya | 0:e4c5e6ec922e | 48 | * void attime() { |
mohamedmoawya | 0:e4c5e6ec922e | 49 | * flip = !flip; |
mohamedmoawya | 0:e4c5e6ec922e | 50 | * } |
mohamedmoawya | 0:e4c5e6ec922e | 51 | * |
mohamedmoawya | 0:e4c5e6ec922e | 52 | * int main() { |
mohamedmoawya | 0:e4c5e6ec922e | 53 | * timer.attach(&attime, 5); |
mohamedmoawya | 0:e4c5e6ec922e | 54 | * while(1) { |
mohamedmoawya | 0:e4c5e6ec922e | 55 | * if(flip == 0) { |
mohamedmoawya | 0:e4c5e6ec922e | 56 | * led1 = !led1; |
mohamedmoawya | 0:e4c5e6ec922e | 57 | * } else { |
mohamedmoawya | 0:e4c5e6ec922e | 58 | * led2 = !led2; |
mohamedmoawya | 0:e4c5e6ec922e | 59 | * } |
mohamedmoawya | 0:e4c5e6ec922e | 60 | * wait(0.2); |
mohamedmoawya | 0:e4c5e6ec922e | 61 | * } |
mohamedmoawya | 0:e4c5e6ec922e | 62 | * } |
mohamedmoawya | 0:e4c5e6ec922e | 63 | * @endcode |
mohamedmoawya | 0:e4c5e6ec922e | 64 | * @ingroup drivers |
mohamedmoawya | 0:e4c5e6ec922e | 65 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 66 | class Ticker : public TimerEvent, private NonCopyable<Ticker> { |
mohamedmoawya | 0:e4c5e6ec922e | 67 | |
mohamedmoawya | 0:e4c5e6ec922e | 68 | public: |
mohamedmoawya | 0:e4c5e6ec922e | 69 | Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) |
mohamedmoawya | 0:e4c5e6ec922e | 70 | { |
mohamedmoawya | 0:e4c5e6ec922e | 71 | } |
mohamedmoawya | 0:e4c5e6ec922e | 72 | |
mohamedmoawya | 0:e4c5e6ec922e | 73 | // When low power ticker is in use, then do not disable deep sleep. |
mohamedmoawya | 0:e4c5e6ec922e | 74 | Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) |
mohamedmoawya | 0:e4c5e6ec922e | 75 | { |
mohamedmoawya | 0:e4c5e6ec922e | 76 | #if DEVICE_LPTICKER |
mohamedmoawya | 0:e4c5e6ec922e | 77 | _lock_deepsleep = (data != get_lp_ticker_data()); |
mohamedmoawya | 0:e4c5e6ec922e | 78 | #endif |
mohamedmoawya | 0:e4c5e6ec922e | 79 | } |
mohamedmoawya | 0:e4c5e6ec922e | 80 | |
mohamedmoawya | 0:e4c5e6ec922e | 81 | /** Attach a function to be called by the Ticker, specifying the interval in seconds |
mohamedmoawya | 0:e4c5e6ec922e | 82 | * |
mohamedmoawya | 0:e4c5e6ec922e | 83 | * @param func pointer to the function to be called |
mohamedmoawya | 0:e4c5e6ec922e | 84 | * @param t the time between calls in seconds |
mohamedmoawya | 0:e4c5e6ec922e | 85 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 86 | void attach(Callback<void()> func, float t) |
mohamedmoawya | 0:e4c5e6ec922e | 87 | { |
mohamedmoawya | 0:e4c5e6ec922e | 88 | attach_us(func, t * 1000000.0f); |
mohamedmoawya | 0:e4c5e6ec922e | 89 | } |
mohamedmoawya | 0:e4c5e6ec922e | 90 | |
mohamedmoawya | 0:e4c5e6ec922e | 91 | /** Attach a member function to be called by the Ticker, specifying the interval in seconds |
mohamedmoawya | 0:e4c5e6ec922e | 92 | * |
mohamedmoawya | 0:e4c5e6ec922e | 93 | * @param obj pointer to the object to call the member function on |
mohamedmoawya | 0:e4c5e6ec922e | 94 | * @param method pointer to the member function to be called |
mohamedmoawya | 0:e4c5e6ec922e | 95 | * @param t the time between calls in seconds |
mohamedmoawya | 0:e4c5e6ec922e | 96 | * @deprecated |
mohamedmoawya | 0:e4c5e6ec922e | 97 | * The attach function does not support cv-qualifiers. Replaced by |
mohamedmoawya | 0:e4c5e6ec922e | 98 | * attach(callback(obj, method), t). |
mohamedmoawya | 0:e4c5e6ec922e | 99 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 100 | template<typename T, typename M> |
mohamedmoawya | 0:e4c5e6ec922e | 101 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
mohamedmoawya | 0:e4c5e6ec922e | 102 | "The attach function does not support cv-qualifiers. Replaced by " |
mohamedmoawya | 0:e4c5e6ec922e | 103 | "attach(callback(obj, method), t).") |
mohamedmoawya | 0:e4c5e6ec922e | 104 | void attach(T *obj, M method, float t) |
mohamedmoawya | 0:e4c5e6ec922e | 105 | { |
mohamedmoawya | 0:e4c5e6ec922e | 106 | attach(callback(obj, method), t); |
mohamedmoawya | 0:e4c5e6ec922e | 107 | } |
mohamedmoawya | 0:e4c5e6ec922e | 108 | |
mohamedmoawya | 0:e4c5e6ec922e | 109 | /** Attach a function to be called by the Ticker, specifying the interval in microseconds |
mohamedmoawya | 0:e4c5e6ec922e | 110 | * |
mohamedmoawya | 0:e4c5e6ec922e | 111 | * @param func pointer to the function to be called |
mohamedmoawya | 0:e4c5e6ec922e | 112 | * @param t the time between calls in micro-seconds |
mohamedmoawya | 0:e4c5e6ec922e | 113 | * |
mohamedmoawya | 0:e4c5e6ec922e | 114 | * @note setting @a t to a value shorter than it takes to process the ticker callback |
mohamedmoawya | 0:e4c5e6ec922e | 115 | * causes the system to hang. Ticker callback is called constantly with no time |
mohamedmoawya | 0:e4c5e6ec922e | 116 | * for threads scheduling. |
mohamedmoawya | 0:e4c5e6ec922e | 117 | * |
mohamedmoawya | 0:e4c5e6ec922e | 118 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 119 | void attach_us(Callback<void()> func, us_timestamp_t t) |
mohamedmoawya | 0:e4c5e6ec922e | 120 | { |
mohamedmoawya | 0:e4c5e6ec922e | 121 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 122 | // lock only for the initial callback setup and this is not low power ticker |
mohamedmoawya | 0:e4c5e6ec922e | 123 | if (!_function && _lock_deepsleep) { |
mohamedmoawya | 0:e4c5e6ec922e | 124 | sleep_manager_lock_deep_sleep(); |
mohamedmoawya | 0:e4c5e6ec922e | 125 | } |
mohamedmoawya | 0:e4c5e6ec922e | 126 | _function = func; |
mohamedmoawya | 0:e4c5e6ec922e | 127 | setup(t); |
mohamedmoawya | 0:e4c5e6ec922e | 128 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 129 | } |
mohamedmoawya | 0:e4c5e6ec922e | 130 | |
mohamedmoawya | 0:e4c5e6ec922e | 131 | /** Attach a member function to be called by the Ticker, specifying the interval in microseconds |
mohamedmoawya | 0:e4c5e6ec922e | 132 | * |
mohamedmoawya | 0:e4c5e6ec922e | 133 | * @param obj pointer to the object to call the member function on |
mohamedmoawya | 0:e4c5e6ec922e | 134 | * @param method pointer to the member function to be called |
mohamedmoawya | 0:e4c5e6ec922e | 135 | * @param t the time between calls in microseconds |
mohamedmoawya | 0:e4c5e6ec922e | 136 | * @deprecated |
mohamedmoawya | 0:e4c5e6ec922e | 137 | * The attach_us function does not support cv-qualifiers. Replaced by |
mohamedmoawya | 0:e4c5e6ec922e | 138 | * attach_us(callback(obj, method), t). |
mohamedmoawya | 0:e4c5e6ec922e | 139 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 140 | template<typename T, typename M> |
mohamedmoawya | 0:e4c5e6ec922e | 141 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
mohamedmoawya | 0:e4c5e6ec922e | 142 | "The attach_us function does not support cv-qualifiers. Replaced by " |
mohamedmoawya | 0:e4c5e6ec922e | 143 | "attach_us(callback(obj, method), t).") |
mohamedmoawya | 0:e4c5e6ec922e | 144 | void attach_us(T *obj, M method, us_timestamp_t t) |
mohamedmoawya | 0:e4c5e6ec922e | 145 | { |
mohamedmoawya | 0:e4c5e6ec922e | 146 | attach_us(Callback<void()>(obj, method), t); |
mohamedmoawya | 0:e4c5e6ec922e | 147 | } |
mohamedmoawya | 0:e4c5e6ec922e | 148 | |
mohamedmoawya | 0:e4c5e6ec922e | 149 | virtual ~Ticker() |
mohamedmoawya | 0:e4c5e6ec922e | 150 | { |
mohamedmoawya | 0:e4c5e6ec922e | 151 | detach(); |
mohamedmoawya | 0:e4c5e6ec922e | 152 | } |
mohamedmoawya | 0:e4c5e6ec922e | 153 | |
mohamedmoawya | 0:e4c5e6ec922e | 154 | /** Detach the function |
mohamedmoawya | 0:e4c5e6ec922e | 155 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 156 | void detach(); |
mohamedmoawya | 0:e4c5e6ec922e | 157 | |
mohamedmoawya | 0:e4c5e6ec922e | 158 | #if !defined(DOXYGEN_ONLY) |
mohamedmoawya | 0:e4c5e6ec922e | 159 | protected: |
mohamedmoawya | 0:e4c5e6ec922e | 160 | void setup(us_timestamp_t t); |
mohamedmoawya | 0:e4c5e6ec922e | 161 | virtual void handler(); |
mohamedmoawya | 0:e4c5e6ec922e | 162 | |
mohamedmoawya | 0:e4c5e6ec922e | 163 | protected: |
mohamedmoawya | 0:e4c5e6ec922e | 164 | us_timestamp_t _delay; /**< Time delay (in microseconds) for resetting the multishot callback. */ |
mohamedmoawya | 0:e4c5e6ec922e | 165 | Callback<void()> _function; /**< Callback. */ |
mohamedmoawya | 0:e4c5e6ec922e | 166 | bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */ |
mohamedmoawya | 0:e4c5e6ec922e | 167 | #endif |
mohamedmoawya | 0:e4c5e6ec922e | 168 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 169 | |
mohamedmoawya | 0:e4c5e6ec922e | 170 | } // namespace mbed |
mohamedmoawya | 0:e4c5e6ec922e | 171 | |
mohamedmoawya | 0:e4c5e6ec922e | 172 | #endif |