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 Io_moon by
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 "mbed.h" 00032 00033 typedef void (*timer_callback)(void); 00034 00035 class SimpleTimer { 00036 00037 public: 00038 // maximum number of timers 00039 const static int MAX_TIMERS = 10; 00040 00041 // setTimer() constants 00042 const static int RUN_FOREVER = 0; 00043 const static int RUN_ONCE = 1; 00044 00045 // constructor 00046 SimpleTimer(); 00047 00048 // this function must be called inside loop() 00049 void run(); 00050 00051 // call function f every d milliseconds 00052 int setInterval(uint32_t d, timer_callback f); 00053 00054 // call function f once after d milliseconds 00055 int setTimeout(uint32_t d, timer_callback f); 00056 00057 // call function f every d milliseconds for n times 00058 int setTimer(uint32_t d, timer_callback f, int n); 00059 00060 // destroy the specified timer 00061 void deleteTimer(int numTimer); 00062 00063 // restart the specified timer 00064 void restartTimer(int numTimer); 00065 00066 // returns true if the specified timer is enabled 00067 bool isEnabled(int numTimer); 00068 00069 // enables the specified timer 00070 void enable(int numTimer); 00071 00072 // disables the specified timer 00073 void disable(int numTimer); 00074 00075 // enables the specified timer if it's currently disabled, 00076 // and vice-versa 00077 void toggle(int numTimer); 00078 00079 // returns the number of used timers 00080 int getNumTimers(); 00081 00082 // returns the number of available timers 00083 int getNumAvailableTimers() { return MAX_TIMERS - numTimers; }; 00084 00085 private: 00086 // deferred call constants 00087 const static int DEFCALL_DONTRUN = 0; // don't call the callback function 00088 const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer 00089 const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer 00090 00091 // find the first available slot 00092 int findFirstFreeSlot(); 00093 00094 // value returned by the millis() function 00095 // in the previous run() call 00096 uint32_t prev_millis[MAX_TIMERS]; 00097 00098 // pointers to the callback functions 00099 timer_callback callbacks[MAX_TIMERS]; 00100 00101 // delay values 00102 uint32_t delays[MAX_TIMERS]; 00103 00104 // number of runs to be executed for each timer 00105 int maxNumRuns[MAX_TIMERS]; 00106 00107 // number of executed runs for each timer 00108 int numRuns[MAX_TIMERS]; 00109 00110 // which timers are enabled 00111 bool enabled[MAX_TIMERS]; 00112 00113 // deferred function call (sort of) - N.B.: this array is only used in run() 00114 int toBeCalled[MAX_TIMERS]; 00115 00116 // actual number of timers in use 00117 int numTimers; 00118 }; 00119 00120 #endif
Generated on Tue Jul 19 2022 01:01:49 by
1.7.2
