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.
SimpleTimer.h
00001 /* 00002 * SimpleTimer.h 00003 * 00004 * SimpleTimer - A timer library for Arduino. 00005 * Author: mromani@ottotecnica.com 00006 * Copyright (c) 2010 OTTOTECNICA Italy 00007 * 00008 * This library is free software; you can redistribute it 00009 * and/or modify it under the terms of the GNU Lesser 00010 * General Public License as published by the Free Software 00011 * Foundation; either version 2.1 of the License, or (at 00012 * your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will 00015 * be useful, but WITHOUT ANY WARRANTY; without even the 00016 * implied warranty of MERCHANTABILITY or FITNESS FOR A 00017 * PARTICULAR PURPOSE. See the GNU Lesser General Public 00018 * License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser 00021 * General Public License along with this library; if not, 00022 * write to the Free Software Foundation, Inc., 00023 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 * 00025 */ 00026 00027 00028 #ifndef SIMPLETIMER_H 00029 #define SIMPLETIMER_H 00030 00031 //#include <functional> 00032 00033 //typedef std::function<void(void)> timer_callback; 00034 typedef void (*timer_callback)(void); 00035 00036 class SimpleTimer { 00037 00038 public: 00039 // maximum number of timers 00040 const static int MAX_TIMERS = 10; 00041 00042 // setTimer() constants 00043 const static int RUN_FOREVER = 0; 00044 const static int RUN_ONCE = 1; 00045 00046 // constructor 00047 SimpleTimer(Timer &timer); 00048 00049 // this function must be called inside loop() 00050 void run(); 00051 00052 // call function f every d milliseconds 00053 int setInterval(int d, timer_callback f); 00054 00055 // call function f once after d milliseconds 00056 int setTimeout(int d, timer_callback f); 00057 00058 // call function f every d milliseconds for n times 00059 int setTimer(int d, timer_callback f, int n); 00060 00061 // destroy the specified timer 00062 void deleteTimer(int numTimer); 00063 00064 // restart the specified timer 00065 void restartTimer(int numTimer); 00066 00067 // returns true if the specified timer is enabled 00068 bool isEnabled(int numTimer); 00069 00070 // enables the specified timer 00071 void enable(int numTimer); 00072 00073 // disables the specified timer 00074 void disable(int numTimer); 00075 00076 // enables the specified timer if it's currently disabled, 00077 // and vice-versa 00078 void toggle(int numTimer); 00079 00080 // returns the number of used timers 00081 int getNumTimers(); 00082 00083 // returns the number of available timers 00084 int getNumAvailableTimers() { return MAX_TIMERS - numTimers; }; 00085 00086 private: 00087 int elapsed(); 00088 00089 // deferred call constants 00090 const static int DEFCALL_DONTRUN = 0; // don't call the callback function 00091 const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer 00092 const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer 00093 00094 // find the first available slot 00095 int findFirstFreeSlot(); 00096 00097 // value returned by the millis() function 00098 // in the previous run() call 00099 int prev_millis[MAX_TIMERS]; 00100 00101 // pointers to the callback functions 00102 timer_callback callbacks[MAX_TIMERS]; 00103 00104 // delay values 00105 int delays[MAX_TIMERS]; 00106 00107 // number of runs to be executed for each timer 00108 int maxNumRuns[MAX_TIMERS]; 00109 00110 // number of executed runs for each timer 00111 int numRuns[MAX_TIMERS]; 00112 00113 // which timers are enabled 00114 bool enabled[MAX_TIMERS]; 00115 00116 // deferred function call (sort of) - N.B.: this array is only used in run() 00117 int toBeCalled[MAX_TIMERS]; 00118 00119 // actual number of timers in use 00120 int numTimers; 00121 00122 Timer &_timer; 00123 }; 00124 00125 #endif 00126
Generated on Tue Jul 12 2022 23:11:54 by
