aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAnatoli Arkhipenko <arkhipenko@hotmail.com>2015-11-14 17:46:16 -0500
committerAnatoli Arkhipenko <arkhipenko@hotmail.com>2015-11-14 17:46:16 -0500
commitdc152db48e325a5d03be0d48a07c119d97d11e59 (patch)
tree72fdcd4c9feee8da743e9540ce58580e7392a7d9 /examples
parentef2329bdeade723f560cd0f7e933c9f3119677f3 (diff)
Version 1.8.3 number of addtions and improvementsv1.8.3
* support for task activation on a status request with arbitrary interval and number of iterations (0 and 1 are still default values) * implement waitForDelayed() method to allow task activation on the status request completion delayed for one current interval * added callback methods prototypes to all examples for Arduino IDE 1.6.6 compatibility * added several constants to be used as task parameters for readability (e.g, TASK_FOREVER, TASK_SECOND, etc.) * significant optimization of the scheduler's execute loop, including millis() rollover fix option
Diffstat (limited to 'examples')
-rw-r--r--examples/Scheduler_example/Scheduler_example.ino29
-rw-r--r--examples/Scheduler_example2/Scheduler_example2.ino21
-rw-r--r--examples/Scheduler_example3/Scheduler_example3.ino17
-rw-r--r--examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino12
-rw-r--r--examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino27
-rw-r--r--examples/Scheduler_example6_IDLE/Scheduler_example6_IDLE.ino11
-rw-r--r--examples/Scheduler_example7_WDT/Scheduler_example7_WDT.ino14
-rw-r--r--examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino15
8 files changed, 98 insertions, 48 deletions
diff --git a/examples/Scheduler_example/Scheduler_example.ino b/examples/Scheduler_example/Scheduler_example.ino
index 74b60f9..2175e57 100644
--- a/examples/Scheduler_example/Scheduler_example.ino
+++ b/examples/Scheduler_example/Scheduler_example.ino
@@ -13,10 +13,16 @@
#include <TaskScheduler.h>
+// Callback methods prototypes
+void t1Callback();
+void t2Callback();
+void t3Callback();
+
+//Tasks
Task t4();
Task t1(2000, 10, &t1Callback);
-Task t2(3000, -1, &t2Callback);
-Task t3(5000, -1, &t3Callback);
+Task t2(3000, TASK_FOREVER, &t2Callback);
+Task t3(5000, TASK_FOREVER, &t3Callback);
Scheduler runner;
@@ -26,8 +32,8 @@ void t1Callback() {
Serial.println(millis());
if (t1.isFirstIteration()) {
- t3.enable();
- runner.addTask(t3);
+ runner.addTask(t3);
+ t3.enable();
Serial.println("t1: enabled t3 and added to the chain");
}
@@ -55,12 +61,6 @@ void setup () {
Serial.begin(115200);
Serial.println("Scheduler TEST");
-
- t1.enable();
- Serial.println("Enabled t1");
- t2.enable();
- Serial.println("Enabled t2");
-
runner.init();
Serial.println("Initialized scheduler");
@@ -69,11 +69,16 @@ void setup () {
runner.addTask(t2);
Serial.println("added t2");
-
+
delay(5000);
+
+ t1.enable();
+ Serial.println("Enabled t1");
+ t2.enable();
+ Serial.println("Enabled t2");
}
void loop () {
runner.execute();
-}
+} \ No newline at end of file
diff --git a/examples/Scheduler_example2/Scheduler_example2.ino b/examples/Scheduler_example2/Scheduler_example2.ino
index 0e7f9a9..c76551a 100644
--- a/examples/Scheduler_example2/Scheduler_example2.ino
+++ b/examples/Scheduler_example2/Scheduler_example2.ino
@@ -2,11 +2,16 @@
#include <TaskScheduler.h>
Scheduler runner;
+// Callback methods prototypes
+void t1Callback();
+void t2Callback();
+void t3Callback();
+// Tasks
Task t4();
Task t1(2000, 10, &t1Callback, &runner, true); //adding task to the chain on creation
-Task t2(3000, -1, &t2Callback, &runner, true); //adding task to the chain on creation
-Task t3(5000, -1, &t3Callback);
+Task t2(3000, TASK_FOREVER, &t2Callback, &runner, true); //adding task to the chain on creation
+Task t3(5000, TASK_FOREVER, &t3Callback);
// Test
// Initially only tasks 1 and 2 are enabled
@@ -18,6 +23,8 @@ Task t3(5000, -1, &t3Callback);
// Task1 disables Task3 on its last iteration and changed Task2 to run every 1/2 seconds
// Because Task2 interval is shorter than Scheduler default tick, loop() executes ecery 1/2 seconds now
// At the end Task2 is the only task running every 1/2 seconds
+//
+// NOTE that t1 and t2 are affected by the delay() function in the setup() method and are scheduled immediately twice to "catch up" with millis().
@@ -28,8 +35,8 @@ void t1Callback() {
Serial.println(millis());
if (t1.isFirstIteration()) {
- t3.enable();
- runner.addTask(t3);
+ runner.addTask(t3);
+ t3.enable();
Serial.println("t1: enabled t3 and added to the chain");
}
@@ -65,6 +72,6 @@ void loop () {
runner.execute();
- Serial.println("Loop ticks at: ");
- Serial.println(millis());
-}
+// Serial.println("Loop ticks at: ");
+// Serial.println(millis());
+} \ No newline at end of file
diff --git a/examples/Scheduler_example3/Scheduler_example3.ino b/examples/Scheduler_example3/Scheduler_example3.ino
index 7111ac3..115f7bc 100644
--- a/examples/Scheduler_example3/Scheduler_example3.ino
+++ b/examples/Scheduler_example3/Scheduler_example3.ino
@@ -1,7 +1,7 @@
/**
* TaskScheduler Test of OnEnable and OnDisable methods and illustration of using wrapper tasks for timout purposes
*
- * A wrapper task runs every 30 seconds and initiates the test case
+ * A wrapper task runs every 10 seconds and initiates the test case
* Another task is run once for 5 seconds, and serves as a LED blinking timeout - 5 seconds
* Finally, a dedicated task which controls LED is running periodically until stopped, and makes the LED blink with 0.5 to 1 second interval.
*
@@ -14,9 +14,17 @@
Scheduler ts;
-Task tWrapper(30000L, -1, &WrapperCallback, &ts, true);
-Task tBlink(5000, 1, NULL, &ts, false, &BlinkOnEnable, &BlinkOnDisable);
-Task tLED(0, -1, NULL, &ts, false, NULL, &LEDOff);
+// Callback methods prototypes
+void WrapperCallback();
+bool BlinkOnEnable();
+void BlinkOnDisable();
+void LEDOn();
+void LEDOff();
+
+// Tasks
+Task tWrapper(10000L, TASK_FOREVER, &WrapperCallback, &ts, true);
+Task tBlink(5000, TASK_ONCE, NULL, &ts, false, &BlinkOnEnable, &BlinkOnDisable);
+Task tLED(0, TASK_FOREVER, NULL, &ts, false, NULL, &LEDOff);
void WrapperCallback() {
tBlink.restartDelayed(); // LED blinking is initiated
@@ -61,6 +69,7 @@ void LEDOff () {
void setup() {
// put your setup code here, to run once:
+ pinMode(LEDPIN, OUTPUT);
}
void loop() {
diff --git a/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino b/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino
index 080de05..22f1dbc 100644
--- a/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino
+++ b/examples/Scheduler_example4_StatusRequest/Scheduler_example4_StatusRequest.ino
@@ -13,7 +13,15 @@ StatusRequest st;
Scheduler ts;
-Task t1(5000, 1, &Callback1, &ts, true, NULL, &Disable1);
+// Callback methods prototypes
+void Callback1();
+void Disable1();
+void Callback2();
+void Callback3();
+void PrepareStatus();
+
+// Tasks
+Task t1(5000, TASK_ONCE, &Callback1, &ts, true, NULL, &Disable1);
Task t2(&Callback2, &ts);
Task t3(&Callback3, &ts);
@@ -77,4 +85,4 @@ void loop() {
ts.execute();
-}
+} \ No newline at end of file
diff --git a/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino b/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino
index d1f2e5c..ff2d850 100644
--- a/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino
+++ b/examples/Scheduler_example5_StatusRequest/Scheduler_example5_StatusRequest.ino
@@ -7,22 +7,29 @@
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STATUS_REQUEST
-
#include <TaskScheduler.h>
-
-
-
StatusRequest measure;
Scheduler ts;
-Task tCycle(10000, -1, &CycleCallback, &ts, true);
-Task tMeasure(1000, 1, &MeasureCallback, &ts, false, &MeasureEnable, &MeasureDisable);
+// Callback methods prototypes
+void CycleCallback();
+void MeasureCallback();
+bool MeasureEnable();
+void MeasureDisable();
+void CalcCallback();
+void S1Callback(); bool S1Enable();
+void S2Callback(); bool S2Enable();
+void S3Callback(); bool S3Enable();
+
+// Tasks
+Task tCycle(10000, TASK_FOREVER, &CycleCallback, &ts, true);
+Task tMeasure(1000, TASK_ONCE, &MeasureCallback, &ts, false, &MeasureEnable, &MeasureDisable);
Task tCalculate(&CalcCallback, &ts);
-Task tSensor1(0, 1, &S1Callback, &ts, false, &S1Enable);
-Task tSensor2(0, 1, &S2Callback, &ts, false, &S2Enable);
-Task tSensor3(0, 1, &S3Callback, &ts, false, &S3Enable);
+Task tSensor1(0, TASK_ONCE, &S1Callback, &ts, false, &S1Enable);
+Task tSensor2(0, TASK_ONCE, &S2Callback, &ts, false, &S2Enable);
+Task tSensor3(0, TASK_ONCE, &S3Callback, &ts, false, &S3Enable);
long distance, d1, d2, d3;
@@ -162,4 +169,4 @@ void loop() {
ts.execute();
-}
+} \ No newline at end of file
diff --git a/examples/Scheduler_example6_IDLE/Scheduler_example6_IDLE.ino b/examples/Scheduler_example6_IDLE/Scheduler_example6_IDLE.ino
index f4df5d8..52f14c0 100644
--- a/examples/Scheduler_example6_IDLE/Scheduler_example6_IDLE.ino
+++ b/examples/Scheduler_example6_IDLE/Scheduler_example6_IDLE.ino
@@ -38,8 +38,13 @@ C1 is scenario 2) is much higher than in scenario 1) because processor is put to
Scheduler ts;
-Task c(10, -1, &Count, &ts);
-Task t(10000, 1, NULL, &ts, true, &tOn, &tOff);
+// Callback methods prototypes
+void Count();
+bool tOn(); void tOff();
+
+// Tasks
+Task c(10, TASK_FOREVER, &Count, &ts);
+Task t(10000, TASK_ONCE, NULL, &ts, true, &tOn, &tOff);
volatile unsigned long c1, c2;
@@ -74,4 +79,4 @@ void loop() {
// put your main code here, to run repeatedly:
ts.execute();
c1++;
-}
+} \ No newline at end of file
diff --git a/examples/Scheduler_example7_WDT/Scheduler_example7_WDT.ino b/examples/Scheduler_example7_WDT/Scheduler_example7_WDT.ino
index ab880d2..260209f 100644
--- a/examples/Scheduler_example7_WDT/Scheduler_example7_WDT.ino
+++ b/examples/Scheduler_example7_WDT/Scheduler_example7_WDT.ino
@@ -18,14 +18,18 @@
Scheduler ts;
+// Callback methods prototypes
+void TaskCB();
+void HB(); bool HBOn(); void HBOff();
+
// Three tasks emulating accidental infinite loop
-Task tTask1(1000, -1, &TaskCB, &ts, true);
-Task tTask2(1000, -1, &TaskCB, &ts, true);
-Task tTask3(1000, -1, &TaskCB, &ts, true);
+Task tTask1(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
+Task tTask2(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
+Task tTask3(TASK_SECOND, TASK_FOREVER, &TaskCB, &ts, true);
// Heartbeat task - resetting the watchdog timer periodically
// Initiates WDT on enable, and deactivates it on disable
-Task tHB(500, -1, &HB, &ts, false, &HBOn, &HBOff);
+Task tHB(500, TASK_FOREVER, &HB, &ts, false, &HBOn, &HBOff);
/**
* Emulating task callback function
@@ -125,4 +129,4 @@ void setup() {
*/
void loop() {
ts.execute();
-}
+} \ No newline at end of file
diff --git a/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino b/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino
index 238617e..8d8df06 100644
--- a/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino
+++ b/examples/Scheduler_example8_LTS/Scheduler_example8_LTS.ino
@@ -23,16 +23,21 @@
Scheduler ts;
+// Callback methods prototypes
+void Calculate(); bool CalcOn();
+bool WrapperOn(); void WrapperOff();
+
+// Tasks
// Calculator tasks.
// Note that all three tasks use the same callback methods
// They will be updating specific variables based on the
// Locat Task Storage pointers
-Task t1(1000, -1, &Calculate, &ts, false, &CalcOn);
-Task t2(1000, -1, &Calculate, &ts, false, &CalcOn);
-Task t3(1000, -1, &Calculate, &ts, false, &CalcOn);
+Task t1(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
+Task t2(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
+Task t3(TASK_SECOND, TASK_FOREVER, &Calculate, &ts, false, &CalcOn);
// add more calc tasks here if necessary
-Task tWrapper(5000, 1, NULL, &ts, false, &WrapperOn, &WrapperOff);
+Task tWrapper(5*TASK_SECOND, TASK_ONCE, NULL, &ts, false, &WrapperOn, &WrapperOff);
// The below structure is an object referenced by LTS pointer
typedef struct {
@@ -139,4 +144,4 @@ void setup() {
void loop() {
ts.execute();
-}
+} \ No newline at end of file