Skip to content

Commit ccd1047

Browse files
committed
Support for taask IDs and Control Points
* Automatically and manually assigned task IDs * Manually assigned Control Points within task's callback function * Updated existing examples * New example sketches
1 parent bb20df9 commit ccd1047

11 files changed

Lines changed: 481 additions & 116 deletions

File tree

README

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Task Scheduler – cooperative multitasking for Arduino microcontrollers
2-
Version 1.8.0: 2015-10-15
2+
Version 1.8.1: 2015-10-22
33

44

55
OVERVIEW:
@@ -10,6 +10,7 @@ A lightweight implementation of cooperative multitasking (task scheduling) suppo
1010
4. Dynamic change of task execution parameters (frequency, number of iterations, callback function)
1111
5. Power saving via entering IDLE sleep mode between tasks are scheduled to run
1212
6. Support for task invocation via Status Request object
13+
7. Support for task IDs and Control Points for error handling and watchdog timer
1314

1415

1516
Changelog:
@@ -49,6 +50,9 @@ v1.7.0:
4950
2015-10-12 - new Task method: forceNextIteration() - makes next iteration happen immediately during the next pass regardless how much time is left
5051

5152
v1.8.0:
52-
2015-10-13 - support for status request objects allowing tasks waiting on requests
53-
2015-10-13 - moved to a single header file to allow compilation control via #defines from the main sketch
54-
53+
2015-10-13 - support for status request objects allowing tasks waiting on requests
54+
2015-10-13 - moved to a single header file to allow compilation control via #defines from the main sketch
55+
56+
v1.8.1:
57+
2015-10-22 - implement Task id and control points to support identification of failure points for watchdog timer logging
58+

examples/Scheduler_example/Scheduler_example.ino

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
/**
2+
* TaskScheduler Test
3+
*
4+
* Initially only tasks 1 and 2 are enabled
5+
* Task1 runs every 2 seconds 10 times and then stops
6+
* Task2 runs every 3 seconds indefinitely
7+
* Task1 enables Task3 at its first run
8+
* Task3 run every 5 seconds
9+
* Task1 disables Task3 on its last iteration and changed Task2 to run every 1/2 seconds
10+
* At the end Task2 is the only task running every 1/2 seconds
11+
*/
12+
13+
114
#include <TaskScheduler.h>
215

316
Task t4();
417
Task t1(2000, 10, &t1Callback);
518
Task t2(3000, -1, &t2Callback);
619
Task t3(5000, -1, &t3Callback);
720

8-
// Test
9-
// Initially only tasks 1 and 2 are enabled
10-
// Task1 runs every 2 seconds 10 times and then stops
11-
// Task2 runs every 3 seconds indefinitely
12-
// Task1 enables Task3 at its first run
13-
// Task3 run every 5 seconds
14-
// loop() runs every 1 second (a default scheduler delay, if no shorter tasks' interval is detected)
15-
// Task1 disables Task3 on its last iteration and changed Task2 to run every 1/2 seconds
16-
// Because Task2 interval is shorter than Scheduler default tick, loop() executes ecery 1/2 seconds now
17-
// At the end Task2 is the only task running every 1/2 seconds
18-
19-
2021
Scheduler runner;
2122

2223

@@ -74,9 +75,5 @@ void setup () {
7475

7576

7677
void loop () {
77-
7878
runner.execute();
79-
80-
Serial.println("Loop ticks at: ");
81-
Serial.println(millis());
8279
}

examples/Scheduler_example3/Scheduler_example3.ino

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* TaskScheduler Test of OnEnable and OnDisable methods and illustration of using wrapper tasks for timout purposes
3+
*
4+
* A wrapper task runs every 30 seconds and initiates the test case
5+
* Another task is run once for 5 seconds, and serves as a LED blinking timeout - 5 seconds
6+
* Finally, a dedicated task which controls LED is running periodically until stopped, and makes the LED blink with 0.5 to 1 second interval.
7+
*
8+
*/
9+
110
#define _TASK_SLEEP_ON_IDLE_RUN
211
#include <TaskScheduler.h>
312

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,80 @@
1-
/** This test demonstrates interaction between three simple tasks via StatusRequest object.
2-
* Task T1 runs every 5 seconds and signals completion of a status request st.
3-
* Tasks T2 and T3 are waiting on the same request (st)
4-
* Task T3 does not renew its interest in status request st, so it is only invoked once (first iteration)
5-
* Task T2 is invoked every time st completes, because it renews its interest in status of status request object st every iteration of T1
6-
*/
7-
8-
#define _TASK_SLEEP_ON_IDLE_RUN
9-
#define _TASK_STATUS_REQUEST
10-
11-
#include <TaskScheduler.h>
12-
13-
StatusRequest st;
14-
15-
Scheduler ts;
16-
17-
Task t1(5000, 1, &Callback1, &ts, true, NULL, &Disable1);
18-
Task t2(&Callback2, &ts);
19-
Task t3(&Callback3, &ts);
20-
21-
/** T1 callback
22-
* T1 just signals completion of st every 5 seconds
23-
*/
24-
void Callback1() {
25-
Serial.println("T1: Signaling completion of ST");
26-
st.signalComplete();
27-
}
28-
29-
/** T1 On Disable callback
30-
* This callback renews the status request and restarts T1 delayed to run again in 5 seconds
31-
*/
32-
void Disable1() {
33-
PrepareStatus();
34-
t1.restartDelayed();
35-
}
36-
37-
/** T2 callback
38-
* Invoked when status request st completes
39-
*/
40-
void Callback2() {
41-
Serial.println("T2: Invoked due to completion of ST");
42-
}
43-
44-
45-
/** T3 callback
46-
* Invoked when status request st completes.
47-
* This is only run once since T3 does not renew its interest in the status request st after first iteration
48-
*/
49-
void Callback3() {
50-
Serial.println("T3: Invoked due to completion of ST");
51-
52-
}
53-
54-
/** Prepare Status request st for another iteration
55-
*
56-
*/
57-
void PrepareStatus() {
58-
st.setWaiting(); // set the statusrequest object for waiting
59-
t2.waitFor(&st); // request tasks 1 & 2 to wait on the object st
60-
}
61-
62-
63-
/** Main Arduino code
64-
* Not much to do here. Just init Serial and set the initial status request
65-
*/
66-
void setup() {
67-
68-
Serial.begin(115200);
69-
Serial.println("TaskScheduler: Status Request Test 1. Simple Test.");
70-
// put your setup code here, to run once:
71-
PrepareStatus();
72-
t3.waitFor(&st);
73-
74-
t1.delay();
75-
}
76-
77-
void loop() {
78-
// put your main code here, to run repeatedly:
79-
ts.execute();
80-
1+
/** This test demonstrates interaction between three simple tasks via StatusRequest object.
2+
* Task T1 runs every 5 seconds and signals completion of a status request st.
3+
* Tasks T2 and T3 are waiting on the same request (st)
4+
* Task T3 does not renew its interest in status request st, so it is only invoked once (first iteration)
5+
* Task T2 is invoked every time st completes, because it renews its interest in status of status request object st every iteration of T1
6+
*/
7+
8+
#define _TASK_SLEEP_ON_IDLE_RUN
9+
#define _TASK_STATUS_REQUEST
10+
#include <TaskScheduler.h>
11+
12+
StatusRequest st;
13+
14+
Scheduler ts;
15+
16+
Task t1(5000, 1, &Callback1, &ts, true, NULL, &Disable1);
17+
Task t2(&Callback2, &ts);
18+
Task t3(&Callback3, &ts);
19+
20+
/** T1 callback
21+
* T1 just signals completion of st every 5 seconds
22+
*/
23+
void Callback1() {
24+
Serial.println("T1: Signaling completion of ST");
25+
st.signalComplete();
26+
}
27+
28+
/** T1 On Disable callback
29+
* This callback renews the status request and restarts T1 delayed to run again in 5 seconds
30+
*/
31+
void Disable1() {
32+
PrepareStatus();
33+
t1.restartDelayed();
34+
}
35+
36+
/** T2 callback
37+
* Invoked when status request st completes
38+
*/
39+
void Callback2() {
40+
Serial.println("T2: Invoked due to completion of ST");
41+
}
42+
43+
44+
/** T3 callback
45+
* Invoked when status request st completes.
46+
* This is only run once since T3 does not renew its interest in the status request st after first iteration
47+
*/
48+
void Callback3() {
49+
Serial.println("T3: Invoked due to completion of ST");
50+
51+
}
52+
53+
/** Prepare Status request st for another iteration
54+
*
55+
*/
56+
void PrepareStatus() {
57+
st.setWaiting(); // set the statusrequest object for waiting
58+
t2.waitFor(&st); // request tasks 1 & 2 to wait on the object st
59+
}
60+
61+
62+
/** Main Arduino code
63+
* Not much to do here. Just init Serial and set the initial status request
64+
*/
65+
void setup() {
66+
67+
Serial.begin(115200);
68+
Serial.println("TaskScheduler: Status Request Test 1. Simple Test.");
69+
70+
PrepareStatus();
71+
t3.waitFor(&st);
72+
73+
t1.delay();
74+
}
75+
76+
void loop() {
77+
78+
ts.execute();
79+
8180
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/**
3+
* This is a test to prove that processor really goes into IDLE sleep.
4+
* For this setup:
5+
*
6+
*
7+
8+
Task c(10, -1, &Count, &ts);
9+
Task t(10000, 1, NULL, &ts, true, &tOn, &tOff);
10+
11+
The result are:
12+
13+
1): With #define _TASK_SLEEP_ON_IDLE_RUN enabled
14+
Start
15+
c1=10771
16+
c2=1001
17+
18+
19+
and
20+
21+
2): With #define _TASK_SLEEP_ON_IDLE_RUN disabled (commented out)
22+
Start
23+
c1=529783
24+
c2=1001
25+
26+
C1 is scenario 2) is much higher than in scenario 1) because processor is put to sleep for 1), but not for 2)
27+
28+
*/
29+
30+
31+
/**
32+
* Compile and run once with _TASK_SLEEP_ON_IDLE_RUN enabled, then with _TASK_SLEEP_ON_IDLE_RUN disabled.
33+
* Compare the results.
34+
*/
35+
36+
//#define _TASK_SLEEP_ON_IDLE_RUN
37+
#include <TaskScheduler.h>
38+
39+
Scheduler ts;
40+
41+
Task c(10, -1, &Count, &ts);
42+
Task t(10000, 1, NULL, &ts, true, &tOn, &tOff);
43+
44+
45+
volatile unsigned long c1, c2;
46+
bool tOn() {
47+
c1 = 0;
48+
c2 = 0;
49+
c.enable();
50+
51+
return true;
52+
}
53+
54+
void tOff() {
55+
c.disable();
56+
Serial.print("c1=");Serial.println(c1);
57+
Serial.print("c2=");Serial.println(c2);
58+
}
59+
60+
void setup() {
61+
// put your setup code here, to run once:
62+
Serial.begin(115200);
63+
Serial.println("Start");
64+
t.delay();
65+
// ts.allowSleep(false);
66+
}
67+
68+
void Count() {
69+
c2++;
70+
}
71+
72+
73+
void loop() {
74+
// put your main code here, to run repeatedly:
75+
ts.execute();
76+
c1++;
77+
}

0 commit comments

Comments
 (0)