Maxim mbed development library
Dependents: MAX34417_demo MAXREFDES1265 MAXREFDES1265
Fork of mbed-dev by
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/toolchain.h" 00022 00023 namespace mbed { 00024 /** \addtogroup drivers */ 00025 /** @{*/ 00026 00027 /** A Ticker is used to call a function at a recurring interval 00028 * 00029 * You can use as many seperate Ticker objects as you require. 00030 * 00031 * @Note Synchronization level: Interrupt safe 00032 * 00033 * Example: 00034 * @code 00035 * // Toggle the blinking led after 5 seconds 00036 * 00037 * #include "mbed.h" 00038 * 00039 * Ticker timer; 00040 * DigitalOut led1(LED1); 00041 * DigitalOut led2(LED2); 00042 * 00043 * int flip = 0; 00044 * 00045 * void attime() { 00046 * flip = !flip; 00047 * } 00048 * 00049 * int main() { 00050 * timer.attach(&attime, 5); 00051 * while(1) { 00052 * if(flip == 0) { 00053 * led1 = !led1; 00054 * } else { 00055 * led2 = !led2; 00056 * } 00057 * wait(0.2); 00058 * } 00059 * } 00060 * @endcode 00061 */ 00062 class Ticker : public TimerEvent { 00063 00064 public: 00065 Ticker() : TimerEvent() { 00066 } 00067 00068 Ticker(const ticker_data_t *data) : TimerEvent(data) { 00069 data->interface->init(); 00070 } 00071 00072 /** Attach a function to be called by the Ticker, specifiying the interval in seconds 00073 * 00074 * @param func pointer to the function to be called 00075 * @param t the time between calls in seconds 00076 */ 00077 void attach(Callback<void()> func, float t) { 00078 attach_us(func, t * 1000000.0f); 00079 } 00080 00081 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds 00082 * 00083 * @param obj pointer to the object to call the member function on 00084 * @param method pointer to the member function to be called 00085 * @param t the time between calls in seconds 00086 * @deprecated 00087 * The attach function does not support cv-qualifiers. Replaced by 00088 * attach(callback(obj, method), t). 00089 */ 00090 template<typename T, typename M> 00091 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00092 "The attach function does not support cv-qualifiers. Replaced by " 00093 "attach(callback(obj, method), t).") 00094 void attach(T *obj, M method, float t) { 00095 attach(callback(obj, method), t); 00096 } 00097 00098 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds 00099 * 00100 * @param fptr pointer to the function to be called 00101 * @param t the time between calls in micro-seconds 00102 */ 00103 void attach_us(Callback<void()> func, timestamp_t t) { 00104 _function.attach(func); 00105 setup(t); 00106 } 00107 00108 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds 00109 * 00110 * @param tptr pointer to the object to call the member function on 00111 * @param mptr pointer to the member function to be called 00112 * @param t the time between calls in micro-seconds 00113 * @deprecated 00114 * The attach_us function does not support cv-qualifiers. Replaced by 00115 * attach_us(callback(obj, method), t). 00116 */ 00117 template<typename T, typename M> 00118 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00119 "The attach_us function does not support cv-qualifiers. Replaced by " 00120 "attach_us(callback(obj, method), t).") 00121 void attach_us(T *obj, M method, timestamp_t t) { 00122 attach_us(Callback<void()>(obj, method), t); 00123 } 00124 00125 virtual ~Ticker() { 00126 detach(); 00127 } 00128 00129 /** Detach the function 00130 */ 00131 void detach(); 00132 00133 protected: 00134 void setup(timestamp_t t); 00135 virtual void handler(); 00136 00137 protected: 00138 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ 00139 Callback<void()> _function; /**< Callback. */ 00140 }; 00141 00142 } // namespace mbed 00143 00144 #endif 00145 00146 /** @}*/
Generated on Tue Jul 12 2022 20:41:23 by 1.7.2