From 44c3ad3f063cda66381461f90b22ecb5fb34ffef Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 13:13:41 +0100 Subject: [PATCH 01/25] Commit new version by Bill Knight --- SimpleTimer/SimpleTimer.cpp | 34 +++++--- SimpleTimer/SimpleTimer.h | 49 ++++++----- SimpleTimer/example/SimpleTimerPlus.ino | 109 ++++++++++++++++++++++++ 3 files changed, 159 insertions(+), 33 deletions(-) mode change 100644 => 100755 SimpleTimer/SimpleTimer.cpp mode change 100644 => 100755 SimpleTimer/SimpleTimer.h create mode 100755 SimpleTimer/example/SimpleTimerPlus.ino diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp old mode 100644 new mode 100755 index 1f78234..59b8c21 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -5,6 +5,9 @@ * Author: mromani@ottotecnica.com * Copyright (c) 2010 OTTOTECNICA Italy * + * Callback function parameters added & compiler warnings + * removed by Bill Knight 20March2017 + * * 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 @@ -40,6 +43,7 @@ SimpleTimer::SimpleTimer() { callbacks[i] = 0; // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer prev_millis[i] = current_millis; numRuns[i] = 0; + params[i] = NULL; } numTimers = 0; @@ -97,11 +101,11 @@ void SimpleTimer::run() { break; case DEFCALL_RUNONLY: - (*callbacks[i])(); + (*callbacks[i])(params[i]); break; case DEFCALL_RUNANDDEL: - (*callbacks[i])(); + (*callbacks[i])(params[i]); deleteTimer(i); break; } @@ -131,7 +135,7 @@ int SimpleTimer::findFirstFreeSlot() { } -int SimpleTimer::setTimer(long d, timer_callback f, int n) { +int SimpleTimer::setTimer(unsigned long d, timer_callback f, void* p, unsigned n) { int freeTimer; freeTimer = findFirstFreeSlot(); @@ -145,6 +149,7 @@ int SimpleTimer::setTimer(long d, timer_callback f, int n) { delays[freeTimer] = d; callbacks[freeTimer] = f; + params[freeTimer] = p; maxNumRuns[freeTimer] = n; enabled[freeTimer] = true; prev_millis[freeTimer] = elapsed(); @@ -155,17 +160,17 @@ int SimpleTimer::setTimer(long d, timer_callback f, int n) { } -int SimpleTimer::setInterval(long d, timer_callback f) { - return setTimer(d, f, RUN_FOREVER); +int SimpleTimer::setInterval(unsigned long d, timer_callback f, void* p) { + return setTimer(d, f, p, RUN_FOREVER); } -int SimpleTimer::setTimeout(long d, timer_callback f) { - return setTimer(d, f, RUN_ONCE); +int SimpleTimer::setTimeout(unsigned long d, timer_callback f, void* p) { + return setTimer(d, f, p, RUN_ONCE); } -void SimpleTimer::deleteTimer(int timerId) { +void SimpleTimer::deleteTimer(unsigned timerId) { // ignore invalid argument if (timerId >= MAX_TIMERS) { return; @@ -180,6 +185,7 @@ void SimpleTimer::deleteTimer(int timerId) { // specified slot is already empty if (callbacks[timerId] != NULL) { callbacks[timerId] = 0; + params[timerId] = NULL; enabled[timerId] = false; toBeCalled[timerId] = DEFCALL_DONTRUN; delays[timerId] = 0; @@ -192,7 +198,7 @@ void SimpleTimer::deleteTimer(int timerId) { // function contributed by code@rowansimms.com -void SimpleTimer::restartTimer(int numTimer) { +void SimpleTimer::restartTimer(unsigned numTimer) { // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; @@ -202,7 +208,7 @@ void SimpleTimer::restartTimer(int numTimer) { } -boolean SimpleTimer::isEnabled(int numTimer) { +boolean SimpleTimer::isEnabled(unsigned numTimer) { if (numTimer >= MAX_TIMERS) { return false; } @@ -211,7 +217,7 @@ boolean SimpleTimer::isEnabled(int numTimer) { } -void SimpleTimer::enable(int numTimer) { +void SimpleTimer::enable(unsigned numTimer) { // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; @@ -221,7 +227,7 @@ void SimpleTimer::enable(int numTimer) { } -void SimpleTimer::disable(int numTimer) { +void SimpleTimer::disable(unsigned numTimer) { // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; @@ -231,7 +237,7 @@ void SimpleTimer::disable(int numTimer) { } -void SimpleTimer::toggle(int numTimer) { +void SimpleTimer::toggle(unsigned numTimer) { // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; @@ -241,6 +247,6 @@ void SimpleTimer::toggle(int numTimer) { } -int SimpleTimer::getNumTimers() { +unsigned SimpleTimer::getNumTimers() { return numTimers; } diff --git a/SimpleTimer/SimpleTimer.h b/SimpleTimer/SimpleTimer.h old mode 100644 new mode 100755 index 2cc0b95..d997cc5 --- a/SimpleTimer/SimpleTimer.h +++ b/SimpleTimer/SimpleTimer.h @@ -5,6 +5,8 @@ * Author: mromani@ottotecnica.com * Copyright (c) 2010 OTTOTECNICA Italy * + * Modifications by Bill Knight 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,7 +36,7 @@ #include #endif -typedef void (*timer_callback)(void); +typedef void (*timer_callback)(void *); class SimpleTimer { @@ -52,39 +54,45 @@ class SimpleTimer { // 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' 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 f, void* p); - // 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 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' 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 f, void* p, unsigned n); // 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 - boolean isEnabled(int numTimer); + boolean 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 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 @@ -102,23 +110,26 @@ class SimpleTimer { // pointers to the callback functions timer_callback callbacks[MAX_TIMERS]; + // function parameters + void *params[MAX_TIMERS]; + // delay values unsigned long delays[MAX_TIMERS]; // number of runs to be executed for each timer - int maxNumRuns[MAX_TIMERS]; + unsigned maxNumRuns[MAX_TIMERS]; // number of executed runs for each timer - int numRuns[MAX_TIMERS]; + unsigned numRuns[MAX_TIMERS]; // which timers are enabled boolean enabled[MAX_TIMERS]; // deferred function call (sort of) - N.B.: this array is only used in run() - int toBeCalled[MAX_TIMERS]; + unsigned toBeCalled[MAX_TIMERS]; // actual number of timers in use - int numTimers; + unsigned numTimers; }; #endif diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino new file mode 100755 index 0000000..46d0d9c --- /dev/null +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -0,0 +1,109 @@ +/* + * SimpleTimerPlusAlarmExample.ino + * + * Based on usage example for Time + TimeAlarm + SimpleTimer libraries + * + * A timer is called every 15 seconds + * Another timer is called once only after 10 seconds + * A third timer is called 10 times. + * Toggle an IO pin 100mSec LOW, 900mSec HIGH + * + */ + +#include + +// There must be one global SimpleTimerPlus object. +// More SimpleTimer objects can be created and run, +// although there is little point in doing so. +SimpleTimer timer; + +// function to be called repeatedly +void RepeatTask(void* args) { + Serial.println("15 second timer"); +} + +// function to be called just once +void OnceOnlyTask(void* args) { + Serial.println("This timer only triggers once"); +} + +// function to be called exactly 10 times +void TenTimesTask(void* args) { + static int k = 0; + k++; + Serial.print("called "); + Serial.print(k); + Serial.println(" / 10 times."); +} + +// print current arduino "uptime" on the serial port +void DigitalClockDisplay(void* args) { + int h,m,s; + s = millis() / 1000; + m = s / 60; + h = s / 3600; + s = s - m * 60; + m = m - h * 60; + Serial.print(h); + printDigits(m); + printDigits(s); + Serial.println(); +} + +// +// utility function for digital clock display: +// prints preceding colon and leading 0 +// +void printDigits(int digits) { + Serial.print(":"); + if(digits < 10) + Serial.print('0'); + Serial.print(digits); +} + +void setPin(void *args) +{ + int state = (int)args; + unsigned long duration; + + if (state == LOW) + { + digitalWrite(16, LOW); + state = HIGH; // set the pin high + duration = 100; // after 100 mSec + } + else + { + digitalWrite(16, HIGH); + state = LOW; // set the pin low + duration = 900; // after 900 mSec + } + + timer.setTimeout(duration, setPin, (void *)state); +} + +void setup() { + Serial.begin(9600); + + // welcome message + Serial.println("SimpleTimer Example"); + Serial.println("One timer is triggered every 15 seconds"); + Serial.println("Another timer is set to trigger only once after 10 seconds"); + Serial.println("Another timer is set to trigger 10 times"); + Serial.println(); + + // timed actions setup + timer.setInterval(15000, RepeatTask, NULL); + timer.setTimeout(10000, OnceOnlyTask, NULL); + timer.setInterval(1000, DigitalClockDisplay, NULL); + timer.setTimer(1200, TenTimesTask, NULL, 10); + pinMode(16, OUTPUT); + digitalWrite(16, LOW); + // Set the pin HIGH after 100 mSec + timer.setTimeout(100, setPin, (void *)HIGH); +} + +void loop() { + // this is where the "polling" occurs + timer.run(); +} From 733588aa905cf413bd760063ce0752c260b4b5f7 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 13:29:17 +0100 Subject: [PATCH 02/25] Make new version backwards-compatible (ish); edit example from master to make it compile. --- SimpleTimer/SimpleTimer.cpp | 10 +++++----- SimpleTimer/SimpleTimer.h | 4 ++-- SimpleTimer/example/SimpleTimer.ino | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 59b8c21..3dbd774 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -135,7 +135,7 @@ int SimpleTimer::findFirstFreeSlot() { } -int SimpleTimer::setTimer(unsigned long d, timer_callback f, void* p, unsigned n) { +int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n, void* p) { int freeTimer; freeTimer = findFirstFreeSlot(); @@ -149,7 +149,7 @@ int SimpleTimer::setTimer(unsigned long d, timer_callback f, void* p, unsigned n delays[freeTimer] = d; callbacks[freeTimer] = f; - params[freeTimer] = p; + params[freeTimer] = p; maxNumRuns[freeTimer] = n; enabled[freeTimer] = true; prev_millis[freeTimer] = elapsed(); @@ -160,13 +160,13 @@ int SimpleTimer::setTimer(unsigned long d, timer_callback f, void* p, unsigned n } -int SimpleTimer::setInterval(unsigned long d, timer_callback f, void* p) { - return setTimer(d, f, p, RUN_FOREVER); +int SimpleTimer::setInterval(unsigned long d, timer_callback f, void* p ) { + return setTimer(d, f, RUN_FOREVER, p); } int SimpleTimer::setTimeout(unsigned long d, timer_callback f, void* p) { - return setTimer(d, f, p, RUN_ONCE); + return setTimer(d, f, RUN_ONCE, p); } diff --git a/SimpleTimer/SimpleTimer.h b/SimpleTimer/SimpleTimer.h index d997cc5..db0dcc9 100755 --- a/SimpleTimer/SimpleTimer.h +++ b/SimpleTimer/SimpleTimer.h @@ -57,7 +57,7 @@ class SimpleTimer { // 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 f, void* p); + int setInterval(unsigned long d, timer_callback f, void* p = NULL); // Timer will call function 'f' with parameter 'p' after 'd' milliseconds one time // returns the timer number (numTimer) on success or @@ -67,7 +67,7 @@ class SimpleTimer { // 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 f, void* p, unsigned n); + int setTimer(unsigned long d, timer_callback f, unsigned n, void* p = NULL); // destroy the specified timer void deleteTimer(unsigned numTimer); diff --git a/SimpleTimer/example/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer.ino index df59263..916008a 100644 --- a/SimpleTimer/example/SimpleTimer.ino +++ b/SimpleTimer/example/SimpleTimer.ino @@ -4,7 +4,7 @@ SimpleTimer timer; // a function to be executed periodically -void repeatMe() { +void repeatMe(void *) { Serial.print("Uptime (s): "); Serial.println(millis() / 1000); } From d1f1984359dc3d1a800782b158fd115096a38f71 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 13:31:58 +0100 Subject: [PATCH 03/25] Make 'plus' example compile with backward-compatible(ish) version --- SimpleTimer/example/SimpleTimerPlus.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino index 46d0d9c..7edfff8 100755 --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -96,7 +96,7 @@ void setup() { timer.setInterval(15000, RepeatTask, NULL); timer.setTimeout(10000, OnceOnlyTask, NULL); timer.setInterval(1000, DigitalClockDisplay, NULL); - timer.setTimer(1200, TenTimesTask, NULL, 10); + timer.setTimer(1200, TenTimesTask, 10, NULL); pinMode(16, OUTPUT); digitalWrite(16, LOW); // Set the pin HIGH after 100 mSec From a12f20ad8ce0f0322c625ca664ce8c6ff0285654 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 23:22:19 +0100 Subject: [PATCH 04/25] remove ctrl-m character --- SimpleTimer/example/SimpleTimerPlus.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino index 7edfff8..b9cd313 100755 --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -106,4 +106,4 @@ void setup() { void loop() { // this is where the "polling" occurs timer.run(); -} +} From 72612d20d5d496fbe090a45b15a38d10f54271d0 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 23:26:51 +0100 Subject: [PATCH 05/25] Use Arduino UNO onborad LED --- SimpleTimer/example/SimpleTimerPlus.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) mode change 100755 => 100644 SimpleTimer/example/SimpleTimerPlus.ino diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino old mode 100755 new mode 100644 index b9cd313..0e9c0ea --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -68,13 +68,13 @@ void setPin(void *args) if (state == LOW) { - digitalWrite(16, LOW); + digitalWrite(13, LOW); state = HIGH; // set the pin high duration = 100; // after 100 mSec } else { - digitalWrite(16, HIGH); + digitalWrite(13, HIGH); state = LOW; // set the pin low duration = 900; // after 900 mSec } From b22e86605a242345618d198fdf76fe9a8aae3860 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 13:13:41 +0100 Subject: [PATCH 06/25] Commit new version by Bill Knight --- SimpleTimer/SimpleTimer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 3dbd774..80c5a72 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -149,7 +149,7 @@ int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n, void* p delays[freeTimer] = d; callbacks[freeTimer] = f; - params[freeTimer] = p; + params[freeTimer] = p; maxNumRuns[freeTimer] = n; enabled[freeTimer] = true; prev_millis[freeTimer] = elapsed(); From 03401b09d76ef1f52bc887a2b52724eac537de21 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Sun, 26 Mar 2017 13:13:41 +0100 Subject: [PATCH 07/25] Commit new version by Bill Knight --- SimpleTimer/example/SimpleTimerPlus.ino | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino index 0e9c0ea..854e6ac 100644 --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -68,13 +68,21 @@ void setPin(void *args) if (state == LOW) { +<<<<<<< HEAD digitalWrite(13, LOW); +======= + digitalWrite(16, LOW); +>>>>>>> Commit new version by Bill Knight state = HIGH; // set the pin high duration = 100; // after 100 mSec } else { +<<<<<<< HEAD digitalWrite(13, HIGH); +======= + digitalWrite(16, HIGH); +>>>>>>> Commit new version by Bill Knight state = LOW; // set the pin low duration = 900; // after 900 mSec } @@ -96,7 +104,11 @@ void setup() { timer.setInterval(15000, RepeatTask, NULL); timer.setTimeout(10000, OnceOnlyTask, NULL); timer.setInterval(1000, DigitalClockDisplay, NULL); +<<<<<<< HEAD timer.setTimer(1200, TenTimesTask, 10, NULL); +======= + timer.setTimer(1200, TenTimesTask, NULL, 10); +>>>>>>> Commit new version by Bill Knight pinMode(16, OUTPUT); digitalWrite(16, LOW); // Set the pin HIGH after 100 mSec @@ -106,4 +118,8 @@ void setup() { void loop() { // this is where the "polling" occurs timer.run(); +<<<<<<< HEAD } +======= +} +>>>>>>> Commit new version by Bill Knight From e5d646018c925ba1a94729102e4a70312f2221c5 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 09:44:37 +0100 Subject: [PATCH 08/25] fix merge conflicts --- SimpleTimer/example/SimpleTimerPlus.ino | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino index 854e6ac..0e9c0ea 100644 --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus.ino @@ -68,21 +68,13 @@ void setPin(void *args) if (state == LOW) { -<<<<<<< HEAD digitalWrite(13, LOW); -======= - digitalWrite(16, LOW); ->>>>>>> Commit new version by Bill Knight state = HIGH; // set the pin high duration = 100; // after 100 mSec } else { -<<<<<<< HEAD digitalWrite(13, HIGH); -======= - digitalWrite(16, HIGH); ->>>>>>> Commit new version by Bill Knight state = LOW; // set the pin low duration = 900; // after 900 mSec } @@ -104,11 +96,7 @@ void setup() { timer.setInterval(15000, RepeatTask, NULL); timer.setTimeout(10000, OnceOnlyTask, NULL); timer.setInterval(1000, DigitalClockDisplay, NULL); -<<<<<<< HEAD timer.setTimer(1200, TenTimesTask, 10, NULL); -======= - timer.setTimer(1200, TenTimesTask, NULL, 10); ->>>>>>> Commit new version by Bill Knight pinMode(16, OUTPUT); digitalWrite(16, LOW); // Set the pin HIGH after 100 mSec @@ -118,8 +106,4 @@ void setup() { void loop() { // this is where the "polling" occurs timer.run(); -<<<<<<< HEAD } -======= -} ->>>>>>> Commit new version by Bill Knight From a3608b9700d261f6b4ece4c4b29d27d1c389b883 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 22:12:11 +0100 Subject: [PATCH 09/25] Fix missing default argument --- SimpleTimer/SimpleTimer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/SimpleTimer.h b/SimpleTimer/SimpleTimer.h index db0dcc9..45d2212 100755 --- a/SimpleTimer/SimpleTimer.h +++ b/SimpleTimer/SimpleTimer.h @@ -62,7 +62,7 @@ class SimpleTimer { // 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 f, void* p); + int setTimeout(unsigned long d, timer_callback f, void* p = NULL); // Timer will call function 'f' with parameter 'p' every 'd' milliseconds 'n' times // returns the timer number (numTimer) on success or From 3b7a1bdad6a7776ca264d2a6230d098dd17f1ce1 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 22:12:24 +0100 Subject: [PATCH 10/25] Port unit tests to new version --- SimpleTimer/tests/UnitTests/UnitTests.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SimpleTimer/tests/UnitTests/UnitTests.ino b/SimpleTimer/tests/UnitTests/UnitTests.ino index 292a2e0..45df60b 100644 --- a/SimpleTimer/tests/UnitTests/UnitTests.ino +++ b/SimpleTimer/tests/UnitTests/UnitTests.ino @@ -10,19 +10,19 @@ unsigned int c2 = 0; bool all_done = false; -void dontExecute() { +void dontExecute(void *) { c0++; } -void callMeOnce() { +void callMeOnce(void *) { c1++; } -void callMeTwice() { +void callMeTwice(void *) { c2++; } -void checkResults() { +void checkResults(void *) { assertEqual(c0, 0); assertEqual(c1, 1); assertEqual(c2, 2); From fb727c1f79c272f9fe7200953f1ba39dcaa614bb Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 22:27:11 +0100 Subject: [PATCH 11/25] simple timer plus v3 --- SimpleTimer/SimpleTimer.cpp | 111 +++++++++++++++++++----------------- SimpleTimer/SimpleTimer.h | 78 ++++++++++++++----------- 2 files changed, 104 insertions(+), 85 deletions(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 80c5a72..208a22f 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -39,11 +39,8 @@ SimpleTimer::SimpleTimer() { unsigned long current_millis = elapsed(); for (int i = 0; i < MAX_TIMERS; i++) { - enabled[i] = false; - callbacks[i] = 0; // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer - prev_millis[i] = current_millis; - numRuns[i] = 0; - params[i] = NULL; + memset(&timer[i], 0, sizeof (timer_t)); + timer[i].prev_millis = current_millis; } numTimers = 0; @@ -59,35 +56,34 @@ void SimpleTimer::run() { for (i = 0; i < MAX_TIMERS; i++) { - toBeCalled[i] = DEFCALL_DONTRUN; + timer[i].toBeCalled = DEFCALL_DONTRUN; // no callback == no timer, i.e. jump over empty slots - if (callbacks[i]) { + if (timer[i].callback != NULL) { // is it time to process this timer ? // see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592 - if (current_millis - prev_millis[i] >= delays[i]) { + if ((current_millis - timer[i].prev_millis) >= timer[i].delay) { // update time - //prev_millis[i] = current_millis; - prev_millis[i] += delays[i]; + timer[i].prev_millis += timer[i].delay; // check if the timer callback has to be executed - if (enabled[i]) { + if (timer[i].enabled) { // "run forever" timers must always be executed - if (maxNumRuns[i] == RUN_FOREVER) { - toBeCalled[i] = DEFCALL_RUNONLY; + if (timer[i].maxNumRuns == RUN_FOREVER) { + timer[i].toBeCalled = DEFCALL_RUNONLY; } // other timers get executed the specified number of times - else if (numRuns[i] < maxNumRuns[i]) { - toBeCalled[i] = DEFCALL_RUNONLY; - numRuns[i]++; + else if (timer[i].numRuns < timer[i].maxNumRuns) { + timer[i].toBeCalled = DEFCALL_RUNONLY; + timer[i].numRuns++; // after the last run, delete the timer - if (numRuns[i] >= maxNumRuns[i]) { - toBeCalled[i] = DEFCALL_RUNANDDEL; + if (timer[i].numRuns >= timer[i].maxNumRuns) { + timer[i].toBeCalled = DEFCALL_RUNANDDEL; } } } @@ -96,16 +92,22 @@ void SimpleTimer::run() { } for (i = 0; i < MAX_TIMERS; i++) { - switch(toBeCalled[i]) { + switch(timer[i].toBeCalled) { case DEFCALL_DONTRUN: break; case DEFCALL_RUNONLY: - (*callbacks[i])(params[i]); + if (timer[i].hasParam) + (*(timer_callback_p)timer[i].callback)(timer[i].param); + else + (*(timer_callback)timer[i].callback)(); break; case DEFCALL_RUNANDDEL: - (*callbacks[i])(params[i]); + if (timer[i].hasParam) + (*(timer_callback_p)timer[i].callback)(timer[i].param); + else + (*(timer_callback)timer[i].callback)(); deleteTimer(i); break; } @@ -125,7 +127,7 @@ int SimpleTimer::findFirstFreeSlot() { // return the first slot with no callback (i.e. free) for (i = 0; i < MAX_TIMERS; i++) { - if (callbacks[i] == 0) { + if (timer[i].callback == NULL) { return i; } } @@ -135,7 +137,7 @@ int SimpleTimer::findFirstFreeSlot() { } -int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n, void* p) { +int SimpleTimer::setupTimer(unsigned long d, void* f, void* p, boolean h, unsigned n) { int freeTimer; freeTimer = findFirstFreeSlot(); @@ -143,16 +145,17 @@ int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n, void* p return -1; } - if (f == NULL || n == 0) { + if (f == NULL) { return -1; } - delays[freeTimer] = d; - callbacks[freeTimer] = f; - params[freeTimer] = p; - maxNumRuns[freeTimer] = n; - enabled[freeTimer] = true; - prev_millis[freeTimer] = elapsed(); + timer[freeTimer].delay = d; + timer[freeTimer].callback = f; + timer[freeTimer].param = p; + timer[freeTimer].hasParam = h; + timer[freeTimer].maxNumRuns = n; + timer[freeTimer].enabled = true; + timer[freeTimer].prev_millis = elapsed(); numTimers++; @@ -160,18 +163,32 @@ int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n, void* p } -int SimpleTimer::setInterval(unsigned long d, timer_callback f, void* p ) { - return setTimer(d, f, RUN_FOREVER, p); +int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n) { + return setupTimer(d, (void *)f, NULL, false, n); } +int SimpleTimer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n) { + return setupTimer(d, (void *)f, p, true, n); +} + +int SimpleTimer::setInterval(unsigned long d, timer_callback f) { + return setupTimer(d, (void *)f, NULL, false, RUN_FOREVER); +} + +int SimpleTimer::setInterval(unsigned long d, timer_callback_p f, void* p) { + return setupTimer(d, (void *)f, p, true, RUN_FOREVER); +} + +int SimpleTimer::setTimeout(unsigned long d, timer_callback f) { + return setupTimer(d, (void *)f, NULL, false, RUN_ONCE); +} -int SimpleTimer::setTimeout(unsigned long d, timer_callback f, void* p) { - return setTimer(d, f, RUN_ONCE, p); +int SimpleTimer::setTimeout(unsigned long d, timer_callback_p f, void* p) { + return setupTimer(d, (void *)f, p, true, RUN_ONCE); } void SimpleTimer::deleteTimer(unsigned timerId) { - // ignore invalid argument if (timerId >= MAX_TIMERS) { return; } @@ -183,13 +200,9 @@ void SimpleTimer::deleteTimer(unsigned timerId) { // don't decrease the number of timers if the // specified slot is already empty - if (callbacks[timerId] != NULL) { - callbacks[timerId] = 0; - params[timerId] = NULL; - enabled[timerId] = false; - toBeCalled[timerId] = DEFCALL_DONTRUN; - delays[timerId] = 0; - numRuns[timerId] = 0; + if (timer[timerId].callback != NULL) { + memset(&timer[timerId], 0, sizeof (timer_t)); + timer[timerId].prev_millis = elapsed(); // update number of timers numTimers--; @@ -199,12 +212,11 @@ void SimpleTimer::deleteTimer(unsigned timerId) { // function contributed by code@rowansimms.com void SimpleTimer::restartTimer(unsigned numTimer) { - // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; } - prev_millis[numTimer] = elapsed(); + timer[numTimer].prev_millis = elapsed(); } @@ -213,37 +225,34 @@ boolean SimpleTimer::isEnabled(unsigned numTimer) { return false; } - return enabled[numTimer]; + return timer[numTimer].enabled; } void SimpleTimer::enable(unsigned numTimer) { - // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; } - enabled[numTimer] = true; + timer[numTimer].enabled = true; } void SimpleTimer::disable(unsigned numTimer) { - // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; } - enabled[numTimer] = false; + timer[numTimer].enabled = false; } void SimpleTimer::toggle(unsigned numTimer) { - // ignore invalid argument if (numTimer >= MAX_TIMERS) { return; } - enabled[numTimer] = !enabled[numTimer]; + timer[numTimer].enabled = !timer[numTimer].enabled; } diff --git a/SimpleTimer/SimpleTimer.h b/SimpleTimer/SimpleTimer.h index 45d2212..8eb4b29 100755 --- a/SimpleTimer/SimpleTimer.h +++ b/SimpleTimer/SimpleTimer.h @@ -36,7 +36,8 @@ #include #endif -typedef void (*timer_callback)(void *); +typedef void (*timer_callback)(void); +typedef void (*timer_callback_p)(void *); class SimpleTimer { @@ -54,20 +55,35 @@ class SimpleTimer { // this function must be called inside loop() void run(); + // 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 f, void* p = NULL); + // 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); // 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 f, void* p = NULL); + // 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); + + // 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 f, unsigned n, void* p = NULL); + // 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); // destroy the specified timer void deleteTimer(unsigned numTimer); @@ -100,33 +116,27 @@ class SimpleTimer { 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 + // 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, boolean 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]; - - // function parameters - void *params[MAX_TIMERS]; - - // delay values - unsigned long delays[MAX_TIMERS]; - - // number of runs to be executed for each timer - unsigned maxNumRuns[MAX_TIMERS]; - - // number of executed runs for each timer - unsigned numRuns[MAX_TIMERS]; - - // which timers are enabled - boolean enabled[MAX_TIMERS]; - - // deferred function call (sort of) - N.B.: this array is only used in run() - unsigned toBeCalled[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 functions + void* param; // function parameter + boolean hasParam; // timer callback has parameter + unsigned long delay; // delay value + unsigned maxNumRuns; // number of runs to be executed + unsigned numRuns; // number of executed runs + boolean enabled; // enabled or not + unsigned toBeCalled; // deferred function call (sort of) - N.B.: only used in run() + } timer_t; + + timer_t timer[MAX_TIMERS]; // actual number of timers in use unsigned numTimers; From c34bc14b155b51ff8542b68629a588323011fae5 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 22:31:12 +0100 Subject: [PATCH 12/25] Add fix in b52641d for issue #9 --- SimpleTimer/SimpleTimer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 208a22f..58e5eac 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -145,7 +145,7 @@ int SimpleTimer::setupTimer(unsigned long d, void* f, void* p, boolean h, unsign return -1; } - if (f == NULL) { + if (f == NULL || n == 0) { return -1; } From 7fcdb2e1cffbabe02920fb70730bbbdccde1a6d8 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 23:07:44 +0100 Subject: [PATCH 13/25] new lib is now backwards compatible --- SimpleTimer/tests/UnitTests/UnitTests.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SimpleTimer/tests/UnitTests/UnitTests.ino b/SimpleTimer/tests/UnitTests/UnitTests.ino index 45df60b..292a2e0 100644 --- a/SimpleTimer/tests/UnitTests/UnitTests.ino +++ b/SimpleTimer/tests/UnitTests/UnitTests.ino @@ -10,19 +10,19 @@ unsigned int c2 = 0; bool all_done = false; -void dontExecute(void *) { +void dontExecute() { c0++; } -void callMeOnce(void *) { +void callMeOnce() { c1++; } -void callMeTwice(void *) { +void callMeTwice() { c2++; } -void checkResults(void *) { +void checkResults() { assertEqual(c0, 0); assertEqual(c1, 1); assertEqual(c2, 2); From 5d975c8acb4d93413a28e01bef482b71bf7870e9 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 23:27:29 +0100 Subject: [PATCH 14/25] Make the test more verbose --- SimpleTimer/tests/UnitTests/UnitTests.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SimpleTimer/tests/UnitTests/UnitTests.ino b/SimpleTimer/tests/UnitTests/UnitTests.ino index 292a2e0..6256e25 100644 --- a/SimpleTimer/tests/UnitTests/UnitTests.ino +++ b/SimpleTimer/tests/UnitTests/UnitTests.ino @@ -11,18 +11,22 @@ unsigned int c2 = 0; bool all_done = false; void dontExecute() { + Serial.println("dontExecute"); c0++; } void callMeOnce() { + Serial.println("callMeOnce"); c1++; } void callMeTwice() { + Serial.println("callMeTwice"); c2++; } void checkResults() { + Serial.println("checkResults"); assertEqual(c0, 0); assertEqual(c1, 1); assertEqual(c2, 2); From e8881077241d3c16f4ad2de032a7b76eb6ea0c2b Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Mon, 27 Mar 2017 23:54:39 +0100 Subject: [PATCH 15/25] Revert "Add fix in b52641d for issue #9" This reverts commit c34bc14b155b51ff8542b68629a588323011fae5. --- SimpleTimer/SimpleTimer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 58e5eac..208a22f 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -145,7 +145,7 @@ int SimpleTimer::setupTimer(unsigned long d, void* f, void* p, boolean h, unsign return -1; } - if (f == NULL || n == 0) { + if (f == NULL) { return -1; } From d3ca742f538a0553f71770f810c5b288cffd74be Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 00:08:17 +0100 Subject: [PATCH 16/25] Add test for setInterval --- SimpleTimer/tests/SetInterval/SetInterval.ino | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 SimpleTimer/tests/SetInterval/SetInterval.ino diff --git a/SimpleTimer/tests/SetInterval/SetInterval.ino b/SimpleTimer/tests/SetInterval/SetInterval.ino new file mode 100644 index 0000000..ec08e23 --- /dev/null +++ b/SimpleTimer/tests/SetInterval/SetInterval.ino @@ -0,0 +1,56 @@ +#include + +// the timer object +SimpleTimer timer; + +unsigned int counter = 0; + +boolean pass = true; + +// a function to be executed periodically +void repeatMe() { + counter++; + Serial.print("Uptime (s): "); + Serial.println(millis() / 1000); +} + +void checkResult(void* param) { + unsigned int expected = (unsigned int)param; + + Serial.print("Checking param = "); + Serial.println(expected); + Serial.print("counter = "); + Serial.println(counter); + + if (expected == counter) { + Serial.println("OK"); + } + else { + Serial.println("ERROR"); + pass = false; + } +} + +void start() { + Serial.println("Test start"); +} + +void stopSketch() { + Serial.println("Test stop"); + Serial.println(pass ? "PASS" : "FAIL"); + while(true); +} + +void setup() { + Serial.begin(9600); + timer.setTimeout(100, start); + timer.setInterval(500, repeatMe); + timer.setTimeout(550, checkResult, 1); + timer.setTimeout(1050, checkResult, 2); + timer.setTimeout(1550, checkResult, 3); + timer.setTimeout(2000, stopSketch); +} + +void loop() { + timer.run(); +} From 070da2a24ad7c4050f802908d9af003fe69bf270 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 00:08:34 +0100 Subject: [PATCH 17/25] Added examples --- .../example/SimpleTimer/SimpleTimer.ino | 21 ++++ .../SimpleTimerPlus/SimpleTimerPlus.ino | 109 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 SimpleTimer/example/SimpleTimer/SimpleTimer.ino create mode 100644 SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino diff --git a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino new file mode 100644 index 0000000..c8e458f --- /dev/null +++ b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino @@ -0,0 +1,21 @@ +#include + +// the timer object +SimpleTimer timer; + +// a function to be executed periodically +void repeatMe() { + Serial.print("Uptime (s): "); + Serial.println(millis() / 1000); +} + +void setup() { + Serial.begin(9600); + Serial.println("Starting"); + timer.setTimer(500, repeatMe, 0); + //timer.setInterval(1000, repeatMe, 1); +} + +void loop() { + timer.run(); +} diff --git a/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino new file mode 100644 index 0000000..55fb438 --- /dev/null +++ b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino @@ -0,0 +1,109 @@ +/* + * SimpleTimerAlarmExample.ino + * + * Based on usage example for Time + TimeAlarm + SimpleTimer libraries + * + * A timer is called every 15 seconds + * Another timer is called once only after 10 seconds + * A third timer is called 10 times. + * Toggle an IO pin 100mSec LOW, 900mSec HIGH + * + */ + +#include + +// There must be one global SimpleTimer object. +// More SimpleTimer objects can be created and run, +// although there is little point in doing so. +SimpleTimer timer; + +// function to be called repeatedly +void RepeatTask(void) { + Serial.println("15 second timer"); +} + +// function to be called just once +void OnceOnlyTask(void) { + Serial.println("This timer only triggers once"); +} + +// function to be called exactly 10 times +void TenTimesTask(void) { + static int k = 0; + k++; + Serial.print("called "); + Serial.print(k); + Serial.println(" / 10 times."); +} + +// print current arduino "uptime" on the serial port +void DigitalClockDisplay(void) { + int h,m,s; + s = millis() / 1000; + m = s / 60; + h = s / 3600; + s = s - m * 60; + m = m - h * 60; + Serial.print(h); + printDigits(m); + printDigits(s); + Serial.println(); +} + +// +// utility function for digital clock display: +// prints preceding colon and leading 0 +// +void printDigits(int digits) { + Serial.print(":"); + if(digits < 10) + Serial.print('0'); + Serial.print(digits); +} + +void setPin(void *args) +{ + int state = (int)args; + unsigned long duration; + + if (state == LOW) + { + digitalWrite(13, LOW); + state = HIGH; // set the pin high + duration = 100; // after 100 mSec + } + else + { + digitalWrite(13, HIGH); + state = LOW; // set the pin low + duration = 900; // after 900 mSec + } + + timer.setTimeout(duration, setPin, (void *)state); +} + +void setup() { + Serial.begin(9600); + + // welcome message + Serial.println("SimpleTimer Example"); + Serial.println("One timer is triggered every 15 seconds"); + Serial.println("Another timer is set to trigger only once after 10 seconds"); + Serial.println("Another timer is set to trigger 10 times"); + Serial.println(); + + // timed actions setup + timer.setInterval(15000, RepeatTask); + timer.setTimeout(10000, OnceOnlyTask); + timer.setInterval(1000, DigitalClockDisplay); + timer.setTimer(1200, TenTimesTask, 10); + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + // Set the pin HIGH after 100 mSec + timer.setTimeout(100, setPin, (void *)HIGH); +} + +void loop() { + // this is where the "polling" occurs + timer.run(); +} From cae6992404b173f90a2930da8b7f24cb01755aa3 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 23:07:47 +0100 Subject: [PATCH 18/25] remove files as the arduino IDE wants them in their own directory --- SimpleTimer/example/SimpleTimer.ino | 19 ----- SimpleTimer/example/SimpleTimerPlus.ino | 109 ------------------------ 2 files changed, 128 deletions(-) delete mode 100644 SimpleTimer/example/SimpleTimer.ino delete mode 100644 SimpleTimer/example/SimpleTimerPlus.ino diff --git a/SimpleTimer/example/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer.ino deleted file mode 100644 index 916008a..0000000 --- a/SimpleTimer/example/SimpleTimer.ino +++ /dev/null @@ -1,19 +0,0 @@ -#include - -// the timer object -SimpleTimer timer; - -// a function to be executed periodically -void repeatMe(void *) { - Serial.print("Uptime (s): "); - Serial.println(millis() / 1000); -} - -void setup() { - Serial.begin(9600); - timer.setInterval(1000, repeatMe); -} - -void loop() { - timer.run(); -} diff --git a/SimpleTimer/example/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus.ino deleted file mode 100644 index 0e9c0ea..0000000 --- a/SimpleTimer/example/SimpleTimerPlus.ino +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SimpleTimerPlusAlarmExample.ino - * - * Based on usage example for Time + TimeAlarm + SimpleTimer libraries - * - * A timer is called every 15 seconds - * Another timer is called once only after 10 seconds - * A third timer is called 10 times. - * Toggle an IO pin 100mSec LOW, 900mSec HIGH - * - */ - -#include - -// There must be one global SimpleTimerPlus object. -// More SimpleTimer objects can be created and run, -// although there is little point in doing so. -SimpleTimer timer; - -// function to be called repeatedly -void RepeatTask(void* args) { - Serial.println("15 second timer"); -} - -// function to be called just once -void OnceOnlyTask(void* args) { - Serial.println("This timer only triggers once"); -} - -// function to be called exactly 10 times -void TenTimesTask(void* args) { - static int k = 0; - k++; - Serial.print("called "); - Serial.print(k); - Serial.println(" / 10 times."); -} - -// print current arduino "uptime" on the serial port -void DigitalClockDisplay(void* args) { - int h,m,s; - s = millis() / 1000; - m = s / 60; - h = s / 3600; - s = s - m * 60; - m = m - h * 60; - Serial.print(h); - printDigits(m); - printDigits(s); - Serial.println(); -} - -// -// utility function for digital clock display: -// prints preceding colon and leading 0 -// -void printDigits(int digits) { - Serial.print(":"); - if(digits < 10) - Serial.print('0'); - Serial.print(digits); -} - -void setPin(void *args) -{ - int state = (int)args; - unsigned long duration; - - if (state == LOW) - { - digitalWrite(13, LOW); - state = HIGH; // set the pin high - duration = 100; // after 100 mSec - } - else - { - digitalWrite(13, HIGH); - state = LOW; // set the pin low - duration = 900; // after 900 mSec - } - - timer.setTimeout(duration, setPin, (void *)state); -} - -void setup() { - Serial.begin(9600); - - // welcome message - Serial.println("SimpleTimer Example"); - Serial.println("One timer is triggered every 15 seconds"); - Serial.println("Another timer is set to trigger only once after 10 seconds"); - Serial.println("Another timer is set to trigger 10 times"); - Serial.println(); - - // timed actions setup - timer.setInterval(15000, RepeatTask, NULL); - timer.setTimeout(10000, OnceOnlyTask, NULL); - timer.setInterval(1000, DigitalClockDisplay, NULL); - timer.setTimer(1200, TenTimesTask, 10, NULL); - pinMode(16, OUTPUT); - digitalWrite(16, LOW); - // Set the pin HIGH after 100 mSec - timer.setTimeout(100, setPin, (void *)HIGH); -} - -void loop() { - // this is where the "polling" occurs - timer.run(); -} From d23044735598f92903d8d733ced2ceabb645443d Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 23:08:10 +0100 Subject: [PATCH 19/25] add comment --- SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) mode change 100644 => 100755 SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino diff --git a/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino old mode 100644 new mode 100755 index 55fb438..797a40a --- a/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino @@ -61,7 +61,13 @@ void printDigits(int digits) { Serial.print(digits); } -void setPin(void *args) +// This callback function takes an arg. The arg can be cast to a value +// as in this example or cast to a pointer to a structure or even a +// function. If a structure, it must be present in memory when the +// callback fuction is called. In other words it must be a global or +// declared 'static' and not a structure local to the function that +// created the timer such as setup() or loop(). +void setPin(void* args) { int state = (int)args; unsigned long duration; From 3b8f33ff305420965b3fdc8f63edefe3a305e38f Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 23:09:46 +0100 Subject: [PATCH 20/25] reset simple example --- SimpleTimer/example/SimpleTimer/SimpleTimer.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino index c8e458f..35bb721 100644 --- a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino +++ b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino @@ -12,8 +12,7 @@ void repeatMe() { void setup() { Serial.begin(9600); Serial.println("Starting"); - timer.setTimer(500, repeatMe, 0); - //timer.setInterval(1000, repeatMe, 1); + timer.setTimer(1000, repeatMe); } void loop() { From ad8ffc8ab94573cd830bff759053e8d931551cb6 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 23:11:39 +0100 Subject: [PATCH 21/25] Replace hard coded pin number with symbolic constant --- .../example/SimpleTimerPlus/SimpleTimerPlus.ino | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) mode change 100755 => 100644 SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino diff --git a/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino old mode 100755 new mode 100644 index 797a40a..2a42991 --- a/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino +++ b/SimpleTimer/example/SimpleTimerPlus/SimpleTimerPlus.ino @@ -17,6 +17,9 @@ // although there is little point in doing so. SimpleTimer timer; +// Pin to toggle +const unsigned int PIN = 13; + // function to be called repeatedly void RepeatTask(void) { Serial.println("15 second timer"); @@ -74,13 +77,13 @@ void setPin(void* args) if (state == LOW) { - digitalWrite(13, LOW); + digitalWrite(PIN, LOW); state = HIGH; // set the pin high duration = 100; // after 100 mSec } else { - digitalWrite(13, HIGH); + digitalWrite(PIN, HIGH); state = LOW; // set the pin low duration = 900; // after 900 mSec } @@ -103,8 +106,8 @@ void setup() { timer.setTimeout(10000, OnceOnlyTask); timer.setInterval(1000, DigitalClockDisplay); timer.setTimer(1200, TenTimesTask, 10); - pinMode(13, OUTPUT); - digitalWrite(13, LOW); + pinMode(PIN, OUTPUT); + digitalWrite(PIN, LOW); // Set the pin HIGH after 100 mSec timer.setTimeout(100, setPin, (void *)HIGH); } From 395ff1965093954a059d97b60f9ff4de091d1ed7 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 00:16:30 +0100 Subject: [PATCH 22/25] Fix tests wrt number of executions = 0 --- SimpleTimer/tests/UnitTests/UnitTests.ino | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/SimpleTimer/tests/UnitTests/UnitTests.ino b/SimpleTimer/tests/UnitTests/UnitTests.ino index 6256e25..590569a 100644 --- a/SimpleTimer/tests/UnitTests/UnitTests.ino +++ b/SimpleTimer/tests/UnitTests/UnitTests.ino @@ -4,17 +4,11 @@ // the timer object SimpleTimer timer; -unsigned int c0 = 0; unsigned int c1 = 0; unsigned int c2 = 0; bool all_done = false; -void dontExecute() { - Serial.println("dontExecute"); - c0++; -} - void callMeOnce() { Serial.println("callMeOnce"); c1++; @@ -27,7 +21,6 @@ void callMeTwice() { void checkResults() { Serial.println("checkResults"); - assertEqual(c0, 0); assertEqual(c1, 1); assertEqual(c2, 2); @@ -39,9 +32,6 @@ void setup() { assertEqual(timer.getNumTimers(), 0); - timer.setTimer(10, dontExecute, 0); - assertEqual(timer.getNumTimers(), 0); - timer.setTimer(10, callMeOnce, 1); timer.setTimer(10, callMeTwice, 2); timer.setTimeout(50, checkResults); From fc71110f25152d02d8e5b2a158a96446f634fa6e Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 00:16:30 +0100 Subject: [PATCH 23/25] Commit new version by Bill Knight --- SimpleTimer/SimpleTimer.cpp | 33 +++++++++++---------------------- SimpleTimer/SimpleTimer.h | 6 +++--- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/SimpleTimer/SimpleTimer.cpp b/SimpleTimer/SimpleTimer.cpp index 208a22f..1e92a19 100755 --- a/SimpleTimer/SimpleTimer.cpp +++ b/SimpleTimer/SimpleTimer.cpp @@ -92,25 +92,16 @@ void SimpleTimer::run() { } for (i = 0; i < MAX_TIMERS; i++) { - switch(timer[i].toBeCalled) { - case DEFCALL_DONTRUN: - break; - - case DEFCALL_RUNONLY: - if (timer[i].hasParam) - (*(timer_callback_p)timer[i].callback)(timer[i].param); - else - (*(timer_callback)timer[i].callback)(); - break; - - case DEFCALL_RUNANDDEL: - if (timer[i].hasParam) - (*(timer_callback_p)timer[i].callback)(timer[i].param); - else - (*(timer_callback)timer[i].callback)(); - deleteTimer(i); - break; - } + if (timer[i].toBeCalled == DEFCALL_DONTRUN) + continue; + + if (timer[i].hasParam) + (*(timer_callback_p)timer[i].callback)(timer[i].param); + else + (*(timer_callback)timer[i].callback)(); + + if (timer[i].toBeCalled == DEFCALL_RUNANDDEL) + deleteTimer(i); } } @@ -118,15 +109,13 @@ void SimpleTimer::run() { // find the first available slot // return -1 if none found int SimpleTimer::findFirstFreeSlot() { - int i; - // all slots are used if (numTimers >= MAX_TIMERS) { return -1; } // return the first slot with no callback (i.e. free) - for (i = 0; i < MAX_TIMERS; i++) { + for (int i = 0; i < MAX_TIMERS; i++) { if (timer[i].callback == NULL) { return i; } diff --git a/SimpleTimer/SimpleTimer.h b/SimpleTimer/SimpleTimer.h index 8eb4b29..4d02379 100755 --- a/SimpleTimer/SimpleTimer.h +++ b/SimpleTimer/SimpleTimer.h @@ -126,13 +126,13 @@ class SimpleTimer { typedef struct { unsigned long prev_millis; // value returned by the millis() function in the previous run() call - void* callback; // pointer to the callback functions + void* callback; // pointer to the callback function void* param; // function parameter - boolean hasParam; // timer callback has parameter + boolean 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 - boolean enabled; // enabled or not + boolean enabled; // true if enabled unsigned toBeCalled; // deferred function call (sort of) - N.B.: only used in run() } timer_t; From dba663039b90d5e951845e4e3a8730b563f47d80 Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 00:16:30 +0100 Subject: [PATCH 24/25] Fix example --- SimpleTimer/example/SimpleTimer/SimpleTimer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino index 35bb721..64f63bb 100644 --- a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino +++ b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino @@ -12,7 +12,7 @@ void repeatMe() { void setup() { Serial.begin(9600); Serial.println("Starting"); - timer.setTimer(1000, repeatMe); + timer.setInterval(1000, repeatMe); } void loop() { From 70712d548ec451cb3169d470cb4d7a9655fcd8df Mon Sep 17 00:00:00 2001 From: Marcello Romani Date: Tue, 28 Mar 2017 23:48:24 +0100 Subject: [PATCH 25/25] remove conflicting line --- SimpleTimer/example/SimpleTimer/SimpleTimer.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino index 64f63bb..df59263 100644 --- a/SimpleTimer/example/SimpleTimer/SimpleTimer.ino +++ b/SimpleTimer/example/SimpleTimer/SimpleTimer.ino @@ -11,7 +11,6 @@ void repeatMe() { void setup() { Serial.begin(9600); - Serial.println("Starting"); timer.setInterval(1000, repeatMe); }