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