1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/**
* TaskScheduler Test sketch - use of task's Local Task Storage pointer
* Test case:
* Overall test runs for 5 seconds
* A number of calculator tasks run every one second, and update their respective variables using Local Task Storage pointer
* All calculator tasks use the same callback code, which obtains reference to appropriate variables via LTS pointer
* Calculaotr tasks perform simple calculation (as an example):
* adding task id number to itself
* multiplying task id number by 10
*
* Upon completion of the overall test, all results are printed out.
* Test could be repeated with various number of calculator tasks.
* All that needs to change is data definitions - code is completely agnostic of number of tasks
*/
#define _TASK_SLEEP_ON_IDLE_RUN // Compile with support for entering IDLE SLEEP state for 1 ms if not tasks are scheduled to run
#define _TASK_WDT_IDS // Compile with support for Task IDs and Watchdog timer
#define _TASK_LTS_POINTER // Compile with support for Local Task Storage pointer
#include <TaskScheduler.h>
// Overall number of calculator tasks:
#define NO_TASKS 3
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(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(5*TASK_SECOND, TASK_ONCE, NULL, &ts, false, &WrapperOn, &WrapperOff);
// The below structure is an object referenced by LTS pointer
typedef struct {
unsigned int id;
long sum;
long product;
} task_var;
// These are actual structures which hold tasks specific values
task_var v1;
task_var v2;
task_var v3;
// Arrays below allow indexed access to specific tasks and tasks variables
Task *tasks[] = { &t1, &t2, &t3 };
task_var *vars[] = { &v1, &v2, &v3 };
/**
* This method is called when a wrapper task is enabled
* The purpose is to supply LTS pointers to all the tasks
*/
bool WrapperOn() {
for (int i=0; i < NO_TASKS; i++) {
Task& T = *tasks[i];
T.setLtsPointer( vars[i] );
T.enableDelayed();
}
return true; // Signal that Task could be enabled
}
/**
* This method is called when Wrapper task is disabled (after first and only iteration is executed)
* For each of the calculor tasks the results are printed out.
*/
void WrapperOff() {
Serial.println("Finished processing");
ts.disableAll();
for (int i=0; i < NO_TASKS; i++) {
Serial.print("ID: "); Serial.println(vars[i]->id);
Serial.print("Sum: "); Serial.println(vars[i]->sum);
Serial.print("Product: "); Serial.println(vars[i]->product);
Serial.println();
}
}
/**
* This method is executed when each calculator task is enabled
* The purpose is to initiate all local variables
*/
bool CalcOn() {
Task& T = ts.currentTask();
task_var& var = *((task_var*) T.getLtsPointer());
// Initialize local variables
var.id = T.getId();
var.sum = 0;
var.product = var.id;
return true;
}
/**
* This method performs simple calculations on task's local variables
*/
void Calculate() {
Task& T = ts.currentTask();
// Another way to get to LTS pointer:
task_var& var = *((task_var*) ts.currentLts());
Serial.print("Calculating for task: ");
Serial.print(T.getId());
Serial.print("; Task id per LTS is: ");
Serial.println( var.id );
var.sum += T.getId();
var.product = var.product * 10;
}
/**
* Standard Arduino setup and loo methods
*/
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0)+analogRead(5));
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.println("Local Task Storage pointer test");
tWrapper.enableDelayed();
}
void loop() {
ts.execute();
}
|