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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
Ticker.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #ifndef MBED_TICKER_H 00018 #define MBED_TICKER_H 00019 00020 #include <mstd_utility> 00021 #include "drivers/TimerEvent.h" 00022 #include "platform/Callback.h" 00023 #include "platform/mbed_toolchain.h" 00024 #include "platform/NonCopyable.h" 00025 #include "hal/lp_ticker_api.h" 00026 00027 namespace mbed { 00028 /** 00029 * \defgroup drivers_Ticker Ticker class 00030 * \ingroup drivers-public-api-ticker 00031 * @{ 00032 */ 00033 00034 /** A Ticker is used to call a function at a recurring interval 00035 * 00036 * You can use as many separate Ticker objects as you require. 00037 * 00038 * @note Synchronization level: Interrupt safe 00039 * 00040 * Example: 00041 * @code 00042 * // Toggle the blinking LED after 5 seconds 00043 * 00044 * #include "mbed.h" 00045 * 00046 * Ticker timer; 00047 * DigitalOut led1(LED1); 00048 * DigitalOut led2(LED2); 00049 * 00050 * int flip = 0; 00051 * 00052 * void attime() { 00053 * flip = !flip; 00054 * } 00055 * 00056 * int main() { 00057 * timer.attach(&attime, 5); 00058 * while(1) { 00059 * if(flip == 0) { 00060 * led1 = !led1; 00061 * } else { 00062 * led2 = !led2; 00063 * } 00064 * wait(0.2); 00065 * } 00066 * } 00067 * @endcode 00068 */ 00069 class Ticker : public TimerEvent, private NonCopyable<Ticker> { 00070 00071 public: 00072 Ticker(); 00073 00074 // When low power ticker is in use, then do not disable deep sleep. 00075 Ticker(const ticker_data_t *data); 00076 00077 /** Attach a function to be called by the Ticker, specifying the interval in seconds 00078 * 00079 * The method forwards its arguments to attach_us() rather than copying them which 00080 * may not be trivial depending on the callback copied. 00081 * The function is forcibly inlined to not use floating-point operations. This is 00082 * possible given attach_us() expects an integer value for the callback interval. 00083 * @param func pointer to the function to be called 00084 * @param t the time between calls in seconds 00085 */ 00086 #if defined(__ICCARM__) 00087 MBED_FORCEINLINE template <typename F> 00088 #else 00089 template <typename F> MBED_FORCEINLINE 00090 #endif 00091 void attach(F &&func, float t) 00092 { 00093 attach_us(std::forward<F>(func), t * 1000000.0f); 00094 } 00095 00096 /** Attach a member function to be called by the Ticker, specifying the interval in seconds 00097 * 00098 * @param obj pointer to the object to call the member function on 00099 * @param method pointer to the member function to be called 00100 * @param t the time between calls in seconds 00101 * @deprecated 00102 * The attach function does not support cv-qualifiers. Replaced by 00103 * attach(callback(obj, method), t). 00104 */ 00105 template<typename T, typename M> 00106 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00107 "The attach function does not support cv-qualifiers. Replaced by " 00108 "attach(callback(obj, method), t).") 00109 void attach(T *obj, M method, float t) 00110 { 00111 attach(callback(obj, method), t); 00112 } 00113 00114 /** Attach a function to be called by the Ticker, specifying the interval in microseconds 00115 * 00116 * @param func pointer to the function to be called 00117 * @param t the time between calls in micro-seconds 00118 * 00119 * @note setting @a t to a value shorter than it takes to process the ticker callback 00120 * causes the system to hang. Ticker callback is called constantly with no time 00121 * for threads scheduling. 00122 * 00123 */ 00124 void attach_us(Callback<void()> func, us_timestamp_t t); 00125 00126 /** Attach a member function to be called by the Ticker, specifying the interval in microseconds 00127 * 00128 * @param obj pointer to the object to call the member function on 00129 * @param method pointer to the member function to be called 00130 * @param t the time between calls in microseconds 00131 * @deprecated 00132 * The attach_us function does not support cv-qualifiers. Replaced by 00133 * attach_us(callback(obj, method), t). 00134 */ 00135 template<typename T, typename M> 00136 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00137 "The attach_us function does not support cv-qualifiers. Replaced by " 00138 "attach_us(callback(obj, method), t).") 00139 void attach_us(T *obj, M method, us_timestamp_t t) 00140 { 00141 attach_us(Callback<void()>(obj, method), t); 00142 } 00143 00144 virtual ~Ticker() 00145 { 00146 detach(); 00147 } 00148 00149 /** Detach the function 00150 */ 00151 void detach(); 00152 00153 #if !defined(DOXYGEN_ONLY) 00154 protected: 00155 void setup(us_timestamp_t t); 00156 virtual void handler(); 00157 00158 protected: 00159 us_timestamp_t _delay; /**< Time delay (in microseconds) for resetting the multishot callback. */ 00160 Callback<void()> _function; /**< Callback. */ 00161 bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */ 00162 #endif 00163 }; 00164 00165 /** @}*/ 00166 00167 } // namespace mbed 00168 00169 #endif
Generated on Tue Jul 12 2022 13:55:00 by
