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

Committer:
Volodymyr Shymanskyy
Date:
Tue Jun 20 00:20:01 2017 +0300
Revision:
14:76d8fd871a4d
Parent:
13:ed6276c0afb7
Update version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Volodymyr Shymanskyy 13:ed6276c0afb7 1 /*
Volodymyr Shymanskyy 13:ed6276c0afb7 2 * SimpleTimer.h
Volodymyr Shymanskyy 13:ed6276c0afb7 3 *
Volodymyr Shymanskyy 13:ed6276c0afb7 4 * SimpleTimer - A timer library for Arduino.
Volodymyr Shymanskyy 13:ed6276c0afb7 5 * Author: mromani@ottotecnica.com
Volodymyr Shymanskyy 13:ed6276c0afb7 6 * Copyright (c) 2010 OTTOTECNICA Italy
Volodymyr Shymanskyy 13:ed6276c0afb7 7 *
Volodymyr Shymanskyy 14:76d8fd871a4d 8 * Modifications by Bill Knight <billk@rosw.com> 18March2017
Volodymyr Shymanskyy 14:76d8fd871a4d 9 *
Volodymyr Shymanskyy 13:ed6276c0afb7 10 * This library is free software; you can redistribute it
Volodymyr Shymanskyy 13:ed6276c0afb7 11 * and/or modify it under the terms of the GNU Lesser
Volodymyr Shymanskyy 13:ed6276c0afb7 12 * General Public License as published by the Free Software
Volodymyr Shymanskyy 13:ed6276c0afb7 13 * Foundation; either version 2.1 of the License, or (at
Volodymyr Shymanskyy 13:ed6276c0afb7 14 * your option) any later version.
Volodymyr Shymanskyy 13:ed6276c0afb7 15 *
Volodymyr Shymanskyy 13:ed6276c0afb7 16 * This library is distributed in the hope that it will
Volodymyr Shymanskyy 13:ed6276c0afb7 17 * be useful, but WITHOUT ANY WARRANTY; without even the
Volodymyr Shymanskyy 13:ed6276c0afb7 18 * implied warranty of MERCHANTABILITY or FITNESS FOR A
Volodymyr Shymanskyy 13:ed6276c0afb7 19 * PARTICULAR PURPOSE. See the GNU Lesser General Public
Volodymyr Shymanskyy 13:ed6276c0afb7 20 * License for more details.
Volodymyr Shymanskyy 13:ed6276c0afb7 21 *
Volodymyr Shymanskyy 13:ed6276c0afb7 22 * You should have received a copy of the GNU Lesser
Volodymyr Shymanskyy 13:ed6276c0afb7 23 * General Public License along with this library; if not,
Volodymyr Shymanskyy 13:ed6276c0afb7 24 * write to the Free Software Foundation, Inc.,
Volodymyr Shymanskyy 13:ed6276c0afb7 25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Volodymyr Shymanskyy 13:ed6276c0afb7 26 *
Volodymyr Shymanskyy 13:ed6276c0afb7 27 */
Volodymyr Shymanskyy 13:ed6276c0afb7 28
Volodymyr Shymanskyy 13:ed6276c0afb7 29 #ifndef BLYNKTIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 30 #define BLYNKTIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 31
Volodymyr Shymanskyy 13:ed6276c0afb7 32 #include <Blynk/BlynkDebug.h>
Volodymyr Shymanskyy 13:ed6276c0afb7 33
Volodymyr Shymanskyy 13:ed6276c0afb7 34 // Replace SimpleTimer
Volodymyr Shymanskyy 13:ed6276c0afb7 35 #define SIMPLETIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 36 #define SimpleTimer BlynkTimer
Volodymyr Shymanskyy 13:ed6276c0afb7 37
Volodymyr Shymanskyy 13:ed6276c0afb7 38 typedef void (*timer_callback)(void);
Volodymyr Shymanskyy 14:76d8fd871a4d 39 typedef void (*timer_callback_p)(void *);
Volodymyr Shymanskyy 13:ed6276c0afb7 40
Volodymyr Shymanskyy 13:ed6276c0afb7 41 class SimpleTimer {
Volodymyr Shymanskyy 13:ed6276c0afb7 42
Volodymyr Shymanskyy 13:ed6276c0afb7 43 public:
Volodymyr Shymanskyy 13:ed6276c0afb7 44 // maximum number of timers
Volodymyr Shymanskyy 13:ed6276c0afb7 45 const static int MAX_TIMERS = 16;
Volodymyr Shymanskyy 13:ed6276c0afb7 46
Volodymyr Shymanskyy 13:ed6276c0afb7 47 // setTimer() constants
Volodymyr Shymanskyy 13:ed6276c0afb7 48 const static int RUN_FOREVER = 0;
Volodymyr Shymanskyy 13:ed6276c0afb7 49 const static int RUN_ONCE = 1;
Volodymyr Shymanskyy 13:ed6276c0afb7 50
Volodymyr Shymanskyy 13:ed6276c0afb7 51 // constructor
Volodymyr Shymanskyy 13:ed6276c0afb7 52 SimpleTimer();
Volodymyr Shymanskyy 13:ed6276c0afb7 53
Volodymyr Shymanskyy 13:ed6276c0afb7 54 void init();
Volodymyr Shymanskyy 13:ed6276c0afb7 55
Volodymyr Shymanskyy 13:ed6276c0afb7 56 // this function must be called inside loop()
Volodymyr Shymanskyy 13:ed6276c0afb7 57 void run();
Volodymyr Shymanskyy 13:ed6276c0afb7 58
Volodymyr Shymanskyy 14:76d8fd871a4d 59 // Timer will call function 'f' every 'd' milliseconds forever
Volodymyr Shymanskyy 14:76d8fd871a4d 60 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 61 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 62 int setInterval(unsigned long d, timer_callback f);
Volodymyr Shymanskyy 14:76d8fd871a4d 63
Volodymyr Shymanskyy 14:76d8fd871a4d 64 // Timer will call function 'f' with parameter 'p' every 'd' milliseconds forever
Volodymyr Shymanskyy 14:76d8fd871a4d 65 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 66 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 67 int setInterval(unsigned long d, timer_callback_p f, void* p);
Volodymyr Shymanskyy 14:76d8fd871a4d 68
Volodymyr Shymanskyy 14:76d8fd871a4d 69 // Timer will call function 'f' after 'd' milliseconds one time
Volodymyr Shymanskyy 14:76d8fd871a4d 70 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 71 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 72 int setTimeout(unsigned long d, timer_callback f);
Volodymyr Shymanskyy 13:ed6276c0afb7 73
Volodymyr Shymanskyy 14:76d8fd871a4d 74 // Timer will call function 'f' with parameter 'p' after 'd' milliseconds one time
Volodymyr Shymanskyy 14:76d8fd871a4d 75 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 76 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 77 int setTimeout(unsigned long d, timer_callback_p f, void* p);
Volodymyr Shymanskyy 13:ed6276c0afb7 78
Volodymyr Shymanskyy 14:76d8fd871a4d 79 // Timer will call function 'f' every 'd' milliseconds 'n' times
Volodymyr Shymanskyy 14:76d8fd871a4d 80 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 81 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 82 int setTimer(unsigned long d, timer_callback f, unsigned n);
Volodymyr Shymanskyy 14:76d8fd871a4d 83
Volodymyr Shymanskyy 14:76d8fd871a4d 84 // Timer will call function 'f' with parameter 'p' every 'd' milliseconds 'n' times
Volodymyr Shymanskyy 14:76d8fd871a4d 85 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 86 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 87 int setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n);
Volodymyr Shymanskyy 14:76d8fd871a4d 88
Volodymyr Shymanskyy 14:76d8fd871a4d 89 // updates interval of the specified timer
Volodymyr Shymanskyy 14:76d8fd871a4d 90 bool changeInterval(unsigned numTimer, unsigned long d);
Volodymyr Shymanskyy 13:ed6276c0afb7 91
Volodymyr Shymanskyy 13:ed6276c0afb7 92 // destroy the specified timer
Volodymyr Shymanskyy 14:76d8fd871a4d 93 void deleteTimer(unsigned numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 94
Volodymyr Shymanskyy 13:ed6276c0afb7 95 // restart the specified timer
Volodymyr Shymanskyy 14:76d8fd871a4d 96 void restartTimer(unsigned numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 97
Volodymyr Shymanskyy 13:ed6276c0afb7 98 // returns true if the specified timer is enabled
Volodymyr Shymanskyy 14:76d8fd871a4d 99 bool isEnabled(unsigned numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 100
Volodymyr Shymanskyy 13:ed6276c0afb7 101 // enables the specified timer
Volodymyr Shymanskyy 14:76d8fd871a4d 102 void enable(unsigned numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 103
Volodymyr Shymanskyy 13:ed6276c0afb7 104 // disables the specified timer
Volodymyr Shymanskyy 14:76d8fd871a4d 105 void disable(unsigned numTimer);
Volodymyr Shymanskyy 14:76d8fd871a4d 106
Volodymyr Shymanskyy 14:76d8fd871a4d 107 // enables all timers
Volodymyr Shymanskyy 14:76d8fd871a4d 108 void enableAll();
Volodymyr Shymanskyy 14:76d8fd871a4d 109
Volodymyr Shymanskyy 14:76d8fd871a4d 110 // disables all timers
Volodymyr Shymanskyy 14:76d8fd871a4d 111 void disableAll();
Volodymyr Shymanskyy 13:ed6276c0afb7 112
Volodymyr Shymanskyy 13:ed6276c0afb7 113 // enables the specified timer if it's currently disabled,
Volodymyr Shymanskyy 13:ed6276c0afb7 114 // and vice-versa
Volodymyr Shymanskyy 14:76d8fd871a4d 115 void toggle(unsigned numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 116
Volodymyr Shymanskyy 13:ed6276c0afb7 117 // returns the number of used timers
Volodymyr Shymanskyy 14:76d8fd871a4d 118 unsigned getNumTimers();
Volodymyr Shymanskyy 13:ed6276c0afb7 119
Volodymyr Shymanskyy 13:ed6276c0afb7 120 // returns the number of available timers
Volodymyr Shymanskyy 14:76d8fd871a4d 121 unsigned getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
Volodymyr Shymanskyy 13:ed6276c0afb7 122
Volodymyr Shymanskyy 13:ed6276c0afb7 123 private:
Volodymyr Shymanskyy 13:ed6276c0afb7 124 // deferred call constants
Volodymyr Shymanskyy 13:ed6276c0afb7 125 const static int DEFCALL_DONTRUN = 0; // don't call the callback function
Volodymyr Shymanskyy 13:ed6276c0afb7 126 const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer
Volodymyr Shymanskyy 14:76d8fd871a4d 127 const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer
Volodymyr Shymanskyy 14:76d8fd871a4d 128
Volodymyr Shymanskyy 14:76d8fd871a4d 129 // low level function to initialize and enable a new timer
Volodymyr Shymanskyy 14:76d8fd871a4d 130 // returns the timer number (numTimer) on success or
Volodymyr Shymanskyy 14:76d8fd871a4d 131 // -1 on failure (f == NULL) or no free timers
Volodymyr Shymanskyy 14:76d8fd871a4d 132 int setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n);
Volodymyr Shymanskyy 13:ed6276c0afb7 133
Volodymyr Shymanskyy 13:ed6276c0afb7 134 // find the first available slot
Volodymyr Shymanskyy 13:ed6276c0afb7 135 int findFirstFreeSlot();
Volodymyr Shymanskyy 13:ed6276c0afb7 136
Volodymyr Shymanskyy 14:76d8fd871a4d 137 typedef struct {
Volodymyr Shymanskyy 14:76d8fd871a4d 138 unsigned long prev_millis; // value returned by the millis() function in the previous run() call
Volodymyr Shymanskyy 14:76d8fd871a4d 139 void* callback; // pointer to the callback function
Volodymyr Shymanskyy 14:76d8fd871a4d 140 void* param; // function parameter
Volodymyr Shymanskyy 14:76d8fd871a4d 141 bool hasParam; // true if callback takes a parameter
Volodymyr Shymanskyy 14:76d8fd871a4d 142 unsigned long delay; // delay value
Volodymyr Shymanskyy 14:76d8fd871a4d 143 unsigned maxNumRuns; // number of runs to be executed
Volodymyr Shymanskyy 14:76d8fd871a4d 144 unsigned numRuns; // number of executed runs
Volodymyr Shymanskyy 14:76d8fd871a4d 145 bool enabled; // true if enabled
Volodymyr Shymanskyy 14:76d8fd871a4d 146 unsigned toBeCalled; // deferred function call (sort of) - N.B.: only used in run()
Volodymyr Shymanskyy 14:76d8fd871a4d 147 } timer_t;
Volodymyr Shymanskyy 13:ed6276c0afb7 148
Volodymyr Shymanskyy 14:76d8fd871a4d 149 timer_t timer[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 150
Volodymyr Shymanskyy 14:76d8fd871a4d 151 // actual number of timers in use (-1 means uninitialized)
Volodymyr Shymanskyy 13:ed6276c0afb7 152 int numTimers;
Volodymyr Shymanskyy 13:ed6276c0afb7 153 };
Volodymyr Shymanskyy 13:ed6276c0afb7 154
Volodymyr Shymanskyy 13:ed6276c0afb7 155 #endif