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.
Dependents: Blynk_Example_WIZwiki-W7500
Fork of Blynk by
Diff: Blynk/BlynkTimer.h
- Revision:
- 13:ed6276c0afb7
- Child:
- 14:76d8fd871a4d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Blynk/BlynkTimer.h Sun Apr 09 14:50:30 2017 +0300
@@ -0,0 +1,125 @@
+/*
+ * SimpleTimer.h
+ *
+ * SimpleTimer - A timer library for Arduino.
+ * Author: mromani@ottotecnica.com
+ * Copyright (c) 2010 OTTOTECNICA Italy
+ *
+ * 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
+ * Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will
+ * be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser
+ * General Public License along with this library; if not,
+ * write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#ifndef BLYNKTIMER_H
+#define BLYNKTIMER_H
+
+#include <Blynk/BlynkDebug.h>
+
+// Replace SimpleTimer
+#define SIMPLETIMER_H
+#define SimpleTimer BlynkTimer
+
+typedef void (*timer_callback)(void);
+
+class SimpleTimer {
+
+public:
+ // maximum number of timers
+ const static int MAX_TIMERS = 16;
+
+ // setTimer() constants
+ const static int RUN_FOREVER = 0;
+ const static int RUN_ONCE = 1;
+
+ // constructor
+ SimpleTimer();
+
+ void init();
+
+ // this function must be called inside loop()
+ void run();
+
+ // call function f every d milliseconds
+ int setInterval(long d, timer_callback f);
+
+ // call function f once after d milliseconds
+ int setTimeout(long d, timer_callback f);
+
+ // call function f every d milliseconds for n times
+ int setTimer(long d, timer_callback f, int n);
+
+ // destroy the specified timer
+ void deleteTimer(int numTimer);
+
+ // restart the specified timer
+ void restartTimer(int numTimer);
+
+ // returns true if the specified timer is enabled
+ bool isEnabled(int numTimer);
+
+ // enables the specified timer
+ void enable(int numTimer);
+
+ // disables the specified timer
+ void disable(int numTimer);
+
+ // enables the specified timer if it's currently disabled,
+ // and vice-versa
+ void toggle(int numTimer);
+
+ // returns the number of used timers
+ int getNumTimers();
+
+ // returns the number of available timers
+ int 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
+
+ // 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];
+
+ // number of runs to be executed for each timer
+ int maxNumRuns[MAX_TIMERS];
+
+ // number of executed runs for each timer
+ int numRuns[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
+ int numTimers;
+};
+
+#endif
