Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ticker.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_TICKER_H 00017 #define MBED_TICKER_H 00018 00019 #include "drivers/TimerEvent.h" 00020 #include "platform/Callback.h" 00021 #include "platform/mbed_toolchain.h" 00022 #include "platform/NonCopyable.h" 00023 #include "platform/mbed_power_mgmt.h" 00024 #include "hal/lp_ticker_api.h" 00025 #include "platform/mbed_critical.h" 00026 00027 namespace mbed { 00028 /** \addtogroup drivers */ 00029 00030 /** A Ticker is used to call a function at a recurring interval 00031 * 00032 * You can use as many separate Ticker objects as you require. 00033 * 00034 * @note Synchronization level: Interrupt safe 00035 * 00036 * Example: 00037 * @code 00038 * // Toggle the blinking led after 5 seconds 00039 * 00040 * #include "mbed.h" 00041 * 00042 * Ticker timer; 00043 * DigitalOut led1(LED1); 00044 * DigitalOut led2(LED2); 00045 * 00046 * int flip = 0; 00047 * 00048 * void attime() { 00049 * flip = !flip; 00050 * } 00051 * 00052 * int main() { 00053 * timer.attach(&attime, 5); 00054 * while(1) { 00055 * if(flip == 0) { 00056 * led1 = !led1; 00057 * } else { 00058 * led2 = !led2; 00059 * } 00060 * wait(0.2); 00061 * } 00062 * } 00063 * @endcode 00064 * @ingroup drivers 00065 */ 00066 class Ticker : public TimerEvent, private NonCopyable<Ticker> { 00067 00068 public: 00069 Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) 00070 { 00071 } 00072 00073 // When low power ticker is in use, then do not disable deep-sleep. 00074 Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) 00075 { 00076 #if DEVICE_LPTICKER 00077 _lock_deepsleep = (data != get_lp_ticker_data()); 00078 #endif 00079 } 00080 00081 /** Attach a function to be called by the Ticker, specifying the interval in seconds 00082 * 00083 * @param func pointer to the function to be called 00084 * @param t the time between calls in seconds 00085 */ 00086 void attach(Callback<void()> func, float t) 00087 { 00088 attach_us(func, t * 1000000.0f); 00089 } 00090 00091 /** Attach a member function to be called by the Ticker, specifying the interval in seconds 00092 * 00093 * @param obj pointer to the object to call the member function on 00094 * @param method pointer to the member function to be called 00095 * @param t the time between calls in seconds 00096 * @deprecated 00097 * The attach function does not support cv-qualifiers. Replaced by 00098 * attach(callback(obj, method), t). 00099 */ 00100 template<typename T, typename M> 00101 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00102 "The attach function does not support cv-qualifiers. Replaced by " 00103 "attach(callback(obj, method), t).") 00104 void attach(T *obj, M method, float t) 00105 { 00106 attach(callback(obj, method), t); 00107 } 00108 00109 /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds 00110 * 00111 * @param func pointer to the function to be called 00112 * @param t the time between calls in micro-seconds 00113 * 00114 * @note setting @a t to a value shorter that it takes to process the ticker callback 00115 * will cause the system to hang. Ticker callback will be called constantly with no time 00116 * for threads scheduling. 00117 * 00118 */ 00119 void attach_us(Callback<void()> func, us_timestamp_t t) 00120 { 00121 core_util_critical_section_enter(); 00122 // lock only for the initial callback setup and this is not low power ticker 00123 if (!_function && _lock_deepsleep) { 00124 sleep_manager_lock_deep_sleep(); 00125 } 00126 _function = func; 00127 setup(t); 00128 core_util_critical_section_exit(); 00129 } 00130 00131 /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds 00132 * 00133 * @param obj pointer to the object to call the member function on 00134 * @param method pointer to the member function to be called 00135 * @param t the time between calls in micro-seconds 00136 * @deprecated 00137 * The attach_us function does not support cv-qualifiers. Replaced by 00138 * attach_us(callback(obj, method), t). 00139 */ 00140 template<typename T, typename M> 00141 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00142 "The attach_us function does not support cv-qualifiers. Replaced by " 00143 "attach_us(callback(obj, method), t).") 00144 void attach_us(T *obj, M method, us_timestamp_t t) 00145 { 00146 attach_us(Callback<void()>(obj, method), t); 00147 } 00148 00149 virtual ~Ticker() 00150 { 00151 detach(); 00152 } 00153 00154 /** Detach the function 00155 */ 00156 void detach(); 00157 00158 protected: 00159 void setup(us_timestamp_t t); 00160 virtual void handler(); 00161 00162 protected: 00163 us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ 00164 Callback<void()> _function; /**< Callback. */ 00165 bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */ 00166 }; 00167 00168 } // namespace mbed 00169 00170 #endif
Generated on Tue Aug 9 2022 00:37:23 by
