Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc. http://www.blynk.cc/

Dependents:   Blynk_RBL_BLE_Nano Blynk_MicroBit Blynk_Serial Blynk_RBL_BLE_Nano

Revision:
14:76d8fd871a4d
Parent:
13:ed6276c0afb7
--- a/Blynk/BlynkTimer.h	Sun Apr 09 14:50:30 2017 +0300
+++ b/Blynk/BlynkTimer.h	Tue Jun 20 00:20:01 2017 +0300
@@ -5,6 +5,8 @@
  * Author: mromani@ottotecnica.com
  * Copyright (c) 2010 OTTOTECNICA Italy
  *
+ * Modifications by Bill Knight <billk@rosw.com> 18March2017
+ *
  * This library is free software; you can redistribute it
  * and/or modify it under the terms of the GNU Lesser
  * General Public License as published by the Free Software
@@ -34,6 +36,7 @@
 #define SimpleTimer BlynkTimer
 
 typedef void (*timer_callback)(void);
+typedef void (*timer_callback_p)(void *);
 
 class SimpleTimer {
 
@@ -53,72 +56,99 @@
     // this function must be called inside loop()
     void run();
 
-    // call function f every d milliseconds
-    int setInterval(long d, timer_callback f);
+    // Timer will call function 'f' every 'd' milliseconds forever
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setInterval(unsigned long d, timer_callback f);
+
+    // Timer will call function 'f' with parameter 'p' every 'd' milliseconds forever
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setInterval(unsigned long d, timer_callback_p f, void* p);
+
+    // Timer will call function 'f' after 'd' milliseconds one time
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setTimeout(unsigned long d, timer_callback f);
 
-    // call function f once after d milliseconds
-    int setTimeout(long d, timer_callback f);
+    // Timer will call function 'f' with parameter 'p' after 'd' milliseconds one time
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setTimeout(unsigned long d, timer_callback_p f, void* p);
 
-    // call function f every d milliseconds for n times
-    int setTimer(long d, timer_callback f, int n);
+    // Timer will call function 'f' every 'd' milliseconds 'n' times
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setTimer(unsigned long d, timer_callback f, unsigned n);
+
+    // Timer will call function 'f' with parameter 'p' every 'd' milliseconds 'n' times
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n);
+
+    // updates interval of the specified timer
+    bool changeInterval(unsigned numTimer, unsigned long d);
 
     // destroy the specified timer
-    void deleteTimer(int numTimer);
+    void deleteTimer(unsigned numTimer);
 
     // restart the specified timer
-    void restartTimer(int numTimer);
+    void restartTimer(unsigned numTimer);
 
     // returns true if the specified timer is enabled
-    bool isEnabled(int numTimer);
+    bool isEnabled(unsigned numTimer);
 
     // enables the specified timer
-    void enable(int numTimer);
+    void enable(unsigned numTimer);
 
     // disables the specified timer
-    void disable(int numTimer);
+    void disable(unsigned numTimer);
+
+    // enables all timers
+    void enableAll();
+
+    // disables all timers
+    void disableAll();
 
     // enables the specified timer if it's currently disabled,
     // and vice-versa
-    void toggle(int numTimer);
+    void toggle(unsigned numTimer);
 
     // returns the number of used timers
-    int getNumTimers();
+    unsigned getNumTimers();
 
     // returns the number of available timers
-    int getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
+    unsigned getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
 
 private:
     // deferred call constants
     const static int DEFCALL_DONTRUN = 0;       // don't call the callback function
     const static int DEFCALL_RUNONLY = 1;       // call the callback function but don't delete the timer
-    const static int DEFCALL_RUNANDDEL = 2;      // call the callback function and delete the timer
+    const static int DEFCALL_RUNANDDEL = 2;     // call the callback function and delete the timer
+
+    // low level function to initialize and enable a new timer
+    // returns the timer number (numTimer) on success or
+    // -1 on failure (f == NULL) or no free timers
+    int setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n);
 
     // find the first available slot
     int findFirstFreeSlot();
 
-    // value returned by the millis() function
-    // in the previous run() call
-    unsigned long prev_millis[MAX_TIMERS];
-
-    // pointers to the callback functions
-    timer_callback callbacks[MAX_TIMERS];
-
-    // delay values
-    unsigned long delays[MAX_TIMERS];
+    typedef struct {
+      unsigned long prev_millis;        // value returned by the millis() function in the previous run() call
+      void* callback;                   // pointer to the callback function
+      void* param;                      // function parameter
+      bool hasParam;                 // true if callback takes a parameter
+      unsigned long delay;              // delay value
+      unsigned maxNumRuns;              // number of runs to be executed
+      unsigned numRuns;                 // number of executed runs
+      bool enabled;                  // true if enabled
+      unsigned toBeCalled;              // deferred function call (sort of) - N.B.: only used in run()
+    } timer_t;
 
-    // number of runs to be executed for each timer
-    int maxNumRuns[MAX_TIMERS];
-
-    // number of executed runs for each timer
-    int numRuns[MAX_TIMERS];
+    timer_t timer[MAX_TIMERS];
 
-    // which timers are enabled
-    bool enabled[MAX_TIMERS];
-
-    // deferred function call (sort of) - N.B.: this array is only used in run()
-    int toBeCalled[MAX_TIMERS];
-
-    // actual number of timers in use
+    // actual number of timers in use (-1 means uninitialized)
     int numTimers;
 };