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:
Sun Apr 09 14:50:30 2017 +0300
Revision:
13:ed6276c0afb7
Child:
14:76d8fd871a4d
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 13:ed6276c0afb7 8 * This library is free software; you can redistribute it
Volodymyr Shymanskyy 13:ed6276c0afb7 9 * and/or modify it under the terms of the GNU Lesser
Volodymyr Shymanskyy 13:ed6276c0afb7 10 * General Public License as published by the Free Software
Volodymyr Shymanskyy 13:ed6276c0afb7 11 * Foundation; either version 2.1 of the License, or (at
Volodymyr Shymanskyy 13:ed6276c0afb7 12 * your option) any later version.
Volodymyr Shymanskyy 13:ed6276c0afb7 13 *
Volodymyr Shymanskyy 13:ed6276c0afb7 14 * This library is distributed in the hope that it will
Volodymyr Shymanskyy 13:ed6276c0afb7 15 * be useful, but WITHOUT ANY WARRANTY; without even the
Volodymyr Shymanskyy 13:ed6276c0afb7 16 * implied warranty of MERCHANTABILITY or FITNESS FOR A
Volodymyr Shymanskyy 13:ed6276c0afb7 17 * PARTICULAR PURPOSE. See the GNU Lesser General Public
Volodymyr Shymanskyy 13:ed6276c0afb7 18 * License for more details.
Volodymyr Shymanskyy 13:ed6276c0afb7 19 *
Volodymyr Shymanskyy 13:ed6276c0afb7 20 * You should have received a copy of the GNU Lesser
Volodymyr Shymanskyy 13:ed6276c0afb7 21 * General Public License along with this library; if not,
Volodymyr Shymanskyy 13:ed6276c0afb7 22 * write to the Free Software Foundation, Inc.,
Volodymyr Shymanskyy 13:ed6276c0afb7 23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Volodymyr Shymanskyy 13:ed6276c0afb7 24 *
Volodymyr Shymanskyy 13:ed6276c0afb7 25 */
Volodymyr Shymanskyy 13:ed6276c0afb7 26
Volodymyr Shymanskyy 13:ed6276c0afb7 27 #ifndef BLYNKTIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 28 #define BLYNKTIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 29
Volodymyr Shymanskyy 13:ed6276c0afb7 30 #include <Blynk/BlynkDebug.h>
Volodymyr Shymanskyy 13:ed6276c0afb7 31
Volodymyr Shymanskyy 13:ed6276c0afb7 32 // Replace SimpleTimer
Volodymyr Shymanskyy 13:ed6276c0afb7 33 #define SIMPLETIMER_H
Volodymyr Shymanskyy 13:ed6276c0afb7 34 #define SimpleTimer BlynkTimer
Volodymyr Shymanskyy 13:ed6276c0afb7 35
Volodymyr Shymanskyy 13:ed6276c0afb7 36 typedef void (*timer_callback)(void);
Volodymyr Shymanskyy 13:ed6276c0afb7 37
Volodymyr Shymanskyy 13:ed6276c0afb7 38 class SimpleTimer {
Volodymyr Shymanskyy 13:ed6276c0afb7 39
Volodymyr Shymanskyy 13:ed6276c0afb7 40 public:
Volodymyr Shymanskyy 13:ed6276c0afb7 41 // maximum number of timers
Volodymyr Shymanskyy 13:ed6276c0afb7 42 const static int MAX_TIMERS = 16;
Volodymyr Shymanskyy 13:ed6276c0afb7 43
Volodymyr Shymanskyy 13:ed6276c0afb7 44 // setTimer() constants
Volodymyr Shymanskyy 13:ed6276c0afb7 45 const static int RUN_FOREVER = 0;
Volodymyr Shymanskyy 13:ed6276c0afb7 46 const static int RUN_ONCE = 1;
Volodymyr Shymanskyy 13:ed6276c0afb7 47
Volodymyr Shymanskyy 13:ed6276c0afb7 48 // constructor
Volodymyr Shymanskyy 13:ed6276c0afb7 49 SimpleTimer();
Volodymyr Shymanskyy 13:ed6276c0afb7 50
Volodymyr Shymanskyy 13:ed6276c0afb7 51 void init();
Volodymyr Shymanskyy 13:ed6276c0afb7 52
Volodymyr Shymanskyy 13:ed6276c0afb7 53 // this function must be called inside loop()
Volodymyr Shymanskyy 13:ed6276c0afb7 54 void run();
Volodymyr Shymanskyy 13:ed6276c0afb7 55
Volodymyr Shymanskyy 13:ed6276c0afb7 56 // call function f every d milliseconds
Volodymyr Shymanskyy 13:ed6276c0afb7 57 int setInterval(long d, timer_callback f);
Volodymyr Shymanskyy 13:ed6276c0afb7 58
Volodymyr Shymanskyy 13:ed6276c0afb7 59 // call function f once after d milliseconds
Volodymyr Shymanskyy 13:ed6276c0afb7 60 int setTimeout(long d, timer_callback f);
Volodymyr Shymanskyy 13:ed6276c0afb7 61
Volodymyr Shymanskyy 13:ed6276c0afb7 62 // call function f every d milliseconds for n times
Volodymyr Shymanskyy 13:ed6276c0afb7 63 int setTimer(long d, timer_callback f, int n);
Volodymyr Shymanskyy 13:ed6276c0afb7 64
Volodymyr Shymanskyy 13:ed6276c0afb7 65 // destroy the specified timer
Volodymyr Shymanskyy 13:ed6276c0afb7 66 void deleteTimer(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 67
Volodymyr Shymanskyy 13:ed6276c0afb7 68 // restart the specified timer
Volodymyr Shymanskyy 13:ed6276c0afb7 69 void restartTimer(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 70
Volodymyr Shymanskyy 13:ed6276c0afb7 71 // returns true if the specified timer is enabled
Volodymyr Shymanskyy 13:ed6276c0afb7 72 bool isEnabled(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 73
Volodymyr Shymanskyy 13:ed6276c0afb7 74 // enables the specified timer
Volodymyr Shymanskyy 13:ed6276c0afb7 75 void enable(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 76
Volodymyr Shymanskyy 13:ed6276c0afb7 77 // disables the specified timer
Volodymyr Shymanskyy 13:ed6276c0afb7 78 void disable(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 79
Volodymyr Shymanskyy 13:ed6276c0afb7 80 // enables the specified timer if it's currently disabled,
Volodymyr Shymanskyy 13:ed6276c0afb7 81 // and vice-versa
Volodymyr Shymanskyy 13:ed6276c0afb7 82 void toggle(int numTimer);
Volodymyr Shymanskyy 13:ed6276c0afb7 83
Volodymyr Shymanskyy 13:ed6276c0afb7 84 // returns the number of used timers
Volodymyr Shymanskyy 13:ed6276c0afb7 85 int getNumTimers();
Volodymyr Shymanskyy 13:ed6276c0afb7 86
Volodymyr Shymanskyy 13:ed6276c0afb7 87 // returns the number of available timers
Volodymyr Shymanskyy 13:ed6276c0afb7 88 int getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
Volodymyr Shymanskyy 13:ed6276c0afb7 89
Volodymyr Shymanskyy 13:ed6276c0afb7 90 private:
Volodymyr Shymanskyy 13:ed6276c0afb7 91 // deferred call constants
Volodymyr Shymanskyy 13:ed6276c0afb7 92 const static int DEFCALL_DONTRUN = 0; // don't call the callback function
Volodymyr Shymanskyy 13:ed6276c0afb7 93 const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer
Volodymyr Shymanskyy 13:ed6276c0afb7 94 const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer
Volodymyr Shymanskyy 13:ed6276c0afb7 95
Volodymyr Shymanskyy 13:ed6276c0afb7 96 // find the first available slot
Volodymyr Shymanskyy 13:ed6276c0afb7 97 int findFirstFreeSlot();
Volodymyr Shymanskyy 13:ed6276c0afb7 98
Volodymyr Shymanskyy 13:ed6276c0afb7 99 // value returned by the millis() function
Volodymyr Shymanskyy 13:ed6276c0afb7 100 // in the previous run() call
Volodymyr Shymanskyy 13:ed6276c0afb7 101 unsigned long prev_millis[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 102
Volodymyr Shymanskyy 13:ed6276c0afb7 103 // pointers to the callback functions
Volodymyr Shymanskyy 13:ed6276c0afb7 104 timer_callback callbacks[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 105
Volodymyr Shymanskyy 13:ed6276c0afb7 106 // delay values
Volodymyr Shymanskyy 13:ed6276c0afb7 107 unsigned long delays[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 108
Volodymyr Shymanskyy 13:ed6276c0afb7 109 // number of runs to be executed for each timer
Volodymyr Shymanskyy 13:ed6276c0afb7 110 int maxNumRuns[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 111
Volodymyr Shymanskyy 13:ed6276c0afb7 112 // number of executed runs for each timer
Volodymyr Shymanskyy 13:ed6276c0afb7 113 int numRuns[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 114
Volodymyr Shymanskyy 13:ed6276c0afb7 115 // which timers are enabled
Volodymyr Shymanskyy 13:ed6276c0afb7 116 bool enabled[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 117
Volodymyr Shymanskyy 13:ed6276c0afb7 118 // deferred function call (sort of) - N.B.: this array is only used in run()
Volodymyr Shymanskyy 13:ed6276c0afb7 119 int toBeCalled[MAX_TIMERS];
Volodymyr Shymanskyy 13:ed6276c0afb7 120
Volodymyr Shymanskyy 13:ed6276c0afb7 121 // actual number of timers in use
Volodymyr Shymanskyy 13:ed6276c0afb7 122 int numTimers;
Volodymyr Shymanskyy 13:ed6276c0afb7 123 };
Volodymyr Shymanskyy 13:ed6276c0afb7 124
Volodymyr Shymanskyy 13:ed6276c0afb7 125 #endif