blynk & neopixelring & w7500

Fork of WIZwiki-7500_Blynk by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkTimer.h Source File

BlynkTimer.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  * Modifications by Bill Knight <billk@rosw.com> 18March2017
00009  *
00010  * This library is free software; you can redistribute it
00011  * and/or modify it under the terms of the GNU Lesser
00012  * General Public License as published by the Free Software
00013  * Foundation; either version 2.1 of the License, or (at
00014  * your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will
00017  * be useful, but WITHOUT ANY WARRANTY; without even the
00018  * implied warranty of MERCHANTABILITY or FITNESS FOR A
00019  * PARTICULAR PURPOSE.  See the GNU Lesser General Public
00020  * License for more details.
00021  *
00022  * You should have received a copy of the GNU Lesser
00023  * General Public License along with this library; if not,
00024  * write to the Free Software Foundation, Inc.,
00025  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00026  *
00027  */
00028 
00029 #ifndef BLYNKTIMER_H
00030 #define BLYNKTIMER_H
00031 
00032 #include <Blynk/BlynkDebug.h>
00033 
00034 // Replace SimpleTimer
00035 #define SIMPLETIMER_H
00036 #define SimpleTimer BlynkTimer
00037 
00038 typedef void (*timer_callback)(void);
00039 typedef void (*timer_callback_p)(void *);
00040 
00041 class SimpleTimer {
00042 
00043 public:
00044     // maximum number of timers
00045     const static int MAX_TIMERS = 16;
00046 
00047     // setTimer() constants
00048     const static int RUN_FOREVER = 0;
00049     const static int RUN_ONCE = 1;
00050 
00051     // constructor
00052     SimpleTimer();
00053 
00054     void init();
00055 
00056     // this function must be called inside loop()
00057     void run();
00058 
00059     // Timer will call function 'f' every 'd' milliseconds forever
00060     // returns the timer number (numTimer) on success or
00061     // -1 on failure (f == NULL) or no free timers
00062     int setInterval(unsigned long d, timer_callback f);
00063 
00064     // Timer will call function 'f' with parameter 'p' every 'd' milliseconds forever
00065     // returns the timer number (numTimer) on success or
00066     // -1 on failure (f == NULL) or no free timers
00067     int setInterval(unsigned long d, timer_callback_p f, void* p);
00068 
00069     // Timer will call function 'f' after 'd' milliseconds one time
00070     // returns the timer number (numTimer) on success or
00071     // -1 on failure (f == NULL) or no free timers
00072     int setTimeout(unsigned long d, timer_callback f);
00073 
00074     // Timer will call function 'f' with parameter 'p' after 'd' milliseconds one time
00075     // returns the timer number (numTimer) on success or
00076     // -1 on failure (f == NULL) or no free timers
00077     int setTimeout(unsigned long d, timer_callback_p f, void* p);
00078 
00079     // Timer will call function 'f' every 'd' milliseconds 'n' times
00080     // returns the timer number (numTimer) on success or
00081     // -1 on failure (f == NULL) or no free timers
00082     int setTimer(unsigned long d, timer_callback f, unsigned n);
00083 
00084     // Timer will call function 'f' with parameter 'p' every 'd' milliseconds 'n' times
00085     // returns the timer number (numTimer) on success or
00086     // -1 on failure (f == NULL) or no free timers
00087     int setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n);
00088 
00089     // updates interval of the specified timer
00090     bool changeInterval(unsigned numTimer, unsigned long d);
00091 
00092     // destroy the specified timer
00093     void deleteTimer(unsigned numTimer);
00094 
00095     // restart the specified timer
00096     void restartTimer(unsigned numTimer);
00097 
00098     // returns true if the specified timer is enabled
00099     bool isEnabled(unsigned numTimer);
00100 
00101     // enables the specified timer
00102     void enable(unsigned numTimer);
00103 
00104     // disables the specified timer
00105     void disable(unsigned numTimer);
00106 
00107     // enables all timers
00108     void enableAll();
00109 
00110     // disables all timers
00111     void disableAll();
00112 
00113     // enables the specified timer if it's currently disabled,
00114     // and vice-versa
00115     void toggle(unsigned numTimer);
00116 
00117     // returns the number of used timers
00118     unsigned getNumTimers();
00119 
00120     // returns the number of available timers
00121     unsigned getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
00122 
00123 private:
00124     // deferred call constants
00125     const static int DEFCALL_DONTRUN = 0;       // don't call the callback function
00126     const static int DEFCALL_RUNONLY = 1;       // call the callback function but don't delete the timer
00127     const static int DEFCALL_RUNANDDEL = 2;     // call the callback function and delete the timer
00128 
00129     // low level function to initialize and enable a new timer
00130     // returns the timer number (numTimer) on success or
00131     // -1 on failure (f == NULL) or no free timers
00132     int setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n);
00133 
00134     // find the first available slot
00135     int findFirstFreeSlot();
00136 
00137     typedef struct {
00138       unsigned long prev_millis;        // value returned by the millis() function in the previous run() call
00139       void* callback;                   // pointer to the callback function
00140       void* param;                      // function parameter
00141       bool hasParam;                 // true if callback takes a parameter
00142       unsigned long delay;              // delay value
00143       unsigned maxNumRuns;              // number of runs to be executed
00144       unsigned numRuns;                 // number of executed runs
00145       bool enabled;                  // true if enabled
00146       unsigned toBeCalled;              // deferred function call (sort of) - N.B.: only used in run()
00147     } timer_t;
00148 
00149     timer_t timer[MAX_TIMERS];
00150 
00151     // actual number of timers in use (-1 means uninitialized)
00152     int numTimers;
00153 };
00154 
00155 #endif