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.
Fork of mbed-src 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 "TimerEvent.h" 00020 #include "FunctionPointer.h" 00021 00022 namespace mbed { 00023 00024 /** A Ticker is used to call a function at a recurring interval 00025 * 00026 * You can use as many seperate Ticker objects as you require. 00027 * 00028 * Example: 00029 * @code 00030 * // Toggle the blinking led after 5 seconds 00031 * 00032 * #include "mbed.h" 00033 * 00034 * Ticker timer; 00035 * DigitalOut led1(LED1); 00036 * DigitalOut led2(LED2); 00037 * 00038 * int flip = 0; 00039 * 00040 * void attime() { 00041 * flip = !flip; 00042 * } 00043 * 00044 * int main() { 00045 * timer.attach(&attime, 5); 00046 * while(1) { 00047 * if(flip == 0) { 00048 * led1 = !led1; 00049 * } else { 00050 * led2 = !led2; 00051 * } 00052 * wait(0.2); 00053 * } 00054 * } 00055 * @endcode 00056 */ 00057 class Ticker : public TimerEvent { 00058 00059 public: 00060 00061 /** Attach a function to be called by the Ticker, specifiying the interval in seconds 00062 * 00063 * @param fptr pointer to the function to be called 00064 * @param t the time between calls in seconds 00065 */ 00066 void attach(void (*fptr)(void), float t) { 00067 attach_us(fptr, t * 1000000.0f); 00068 } 00069 00070 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds 00071 * 00072 * @param tptr pointer to the object to call the member function on 00073 * @param mptr pointer to the member function to be called 00074 * @param t the time between calls in seconds 00075 */ 00076 template<typename T> 00077 void attach(T* tptr, void (T::*mptr)(void), float t) { 00078 attach_us(tptr, mptr, t * 1000000.0f); 00079 } 00080 00081 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds 00082 * 00083 * @param fptr pointer to the function to be called 00084 * @param t the time between calls in micro-seconds 00085 */ 00086 void attach_us(void (*fptr)(void), unsigned int t) { 00087 _function.attach(fptr); 00088 setup(t); 00089 } 00090 00091 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds 00092 * 00093 * @param tptr pointer to the object to call the member function on 00094 * @param mptr pointer to the member function to be called 00095 * @param t the time between calls in micro-seconds 00096 */ 00097 template<typename T> 00098 void attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) { 00099 _function.attach(tptr, mptr); 00100 setup(t); 00101 } 00102 00103 /** Detach the function 00104 */ 00105 void detach(); 00106 00107 protected: 00108 void setup(unsigned int t); 00109 virtual void handler(); 00110 00111 unsigned int _delay; 00112 FunctionPointer _function; 00113 }; 00114 00115 } // namespace mbed 00116 00117 #endif
Generated on Tue Jul 12 2022 21:21:33 by
