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