Skip to content

Commit 4e58181

Browse files
committed
Cleanup
1 parent 77e258f commit 4e58181

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

examples/SchedulerTaskCompletionQueue/SchedulerTaskCompletionQueue.ino

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*
1919
* @section Description
2020
* This Arduino sketch shows how to use the Scheduler library;
21-
* Queue events between tasks.
21+
* Worker Design Pattern; Functions (tasks) are queued and executed by
22+
* work threads.
2223
*/
2324

2425
#include <Scheduler.h>
@@ -29,48 +30,47 @@
2930
#define Serial SerialUSB
3031
#endif
3132

32-
Queue<SchedulerClass::func_t, 4> taskq;
33+
// Queue of tasks (function pointers)
34+
typedef SchedulerClass::func_t task_t;
35+
const unsigned int TASKQ_MAX = 8;
36+
Queue<task_t, TASKQ_MAX> taskq;
3337

38+
// Example task: Calculate sum(i): i=0..100
3439
volatile unsigned int sum100;
3540

36-
volatile unsigned int fact10;
37-
3841
void sumTo100()
3942
{
4043
const unsigned int N = 100;
4144
sum100 = 0;
42-
for (unsigned int i = 0; i != N+1; ++i)
43-
{
44-
sum100 += i;
45-
delay(random(10));
45+
for (unsigned int i = 0; i <= N; i++) {
46+
sum100 += i;
47+
delay(random(200));
4648
}
4749
}
4850

51+
// Example task2: Calculate fac(10)
52+
volatile unsigned long int fact10;
53+
4954
void factTo10()
5055
{
5156
const unsigned int N = 10;
5257
fact10 = 1;
53-
for (unsigned int i = 1; i != N+1 ; ++i)
54-
{
55-
fact10 *= i;
56-
delay(random(10));
58+
for (unsigned int i = 1; i <= N; i++) {
59+
fact10 *= i;
60+
delay(random(2000));
5761
}
5862
}
5963

64+
// Example task3: simple background task (push back example)
6065
void printAlive()
6166
{
6267
Serial.print(millis());
63-
Serial.print(F(":printAlive::"));
64-
Serial.println(F("alive..."));
65-
yield();
66-
}
68+
Serial.println(F(":printAlive::alive..."));
69+
Serial.flush();
70+
delay(1000);
6771

68-
void printAliveLoop()
69-
{
70-
Serial.print(millis());
71-
Serial.print(F(":printLoop::"));
72-
Serial.println(F("alive loop..."));
73-
delay(500);
72+
task_t task = printAlive;
73+
taskq.push(&task);
7474
}
7575

7676
void setup()
@@ -80,49 +80,47 @@ void setup()
8080
Serial.println(F("SchedulerTaskCompletionQueue: started"));
8181
Serial.flush();
8282

83-
SchedulerClass::func_t task;
83+
// Start two worker
84+
Scheduler.startLoop(worker);
85+
Scheduler.startLoop(worker);
8486

87+
// Push tasks
88+
task_t task;
8589
task = sumTo100;
8690
Serial.print(millis());
87-
Serial.print(F(":setup::push task sumto100="));
91+
Serial.print(F(":setup::push task @sumTo100 = "));
8892
Serial.println(int(&sumTo100));
8993
taskq.push(&task);
90-
94+
9195
task = factTo10;
9296
Serial.print(millis());
93-
Serial.print(F(":setup::push task factTo10="));
97+
Serial.print(F(":setup::push task @factTo10 = "));
9498
Serial.println(int(&factTo10));
9599
taskq.push(&task);
96-
100+
97101
task = printAlive;
98102
Serial.print(millis());
99-
Serial.print(F(":setup::push task printAlive="));
103+
Serial.print(F(":setup::push task @printAlive = "));
100104
Serial.println(int(&printAlive));
101105
taskq.push(&task);
102106

103-
// Start two event handlers
104-
Scheduler.startLoop(taskRunner);
105-
Scheduler.startLoop(taskRunner);
106-
Scheduler.startLoop(printAliveLoop);
107+
// Note: The printAlive task will start when either sumTo100 or
108+
// factTo10 are completed as there are only two workers.
107109
}
108110

109111
void loop()
110112
{
111-
delay(1000);
112-
113-
Serial.print(millis());
114-
Serial.print(F(":loop sum100="));
115-
Serial.println(sum100);
116-
117113
Serial.print(millis());
118-
Serial.print(F(":loop fact10="));
114+
Serial.print(F(":loop::sum100 = "));
115+
Serial.print(sum100);
116+
Serial.print(F(", fact10 = "));
119117
Serial.println(fact10);
118+
delay(1000);
120119
}
121120

122-
void taskRunner()
121+
void worker()
123122
{
124-
// Pull events
125-
SchedulerClass::func_t* task;
126-
taskq.pull(task);
127-
(*task)();
123+
task_t task;
124+
taskq.pull(&task);
125+
task();
128126
}

0 commit comments

Comments
 (0)