Skip to content

Commit 77e258f

Browse files
authored
Merge pull request #24 from rstoica/develop
Added example of run to completion tasks
2 parents 89bcd1f + dba6a95 commit 77e258f

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed

Scheduler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ extern SchedulerClass Scheduler;
159159
* variable(s).
160160
* @param[in] cond condition to await.
161161
*/
162-
#define await(cond) while (!(cond)) yield()
162+
#define await(cond) while (!(cond)) Scheduler.yield()
163163

164164
#endif

Scheduler/Semaphore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Semaphore {
4646
void signal(unsigned int count = 1)
4747
{
4848
m_count += count;
49-
yield();
49+
Scheduler.yield();
5050
}
5151

5252
protected:
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* @file SchedulerTaskCompletionQueue.ino
3+
* @version 1.0
4+
* @author Andrei Stoica
5+
*
6+
* @section License
7+
* Copyright (C) 2015-2017, Mikael Patel
8+
*
9+
* This library is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License as published by the Free Software Foundation; either
12+
* version 2.1 of the License, or (at your option) any later version.
13+
*
14+
* This library is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
* Lesser General Public License for more details.
18+
*
19+
* @section Description
20+
* This Arduino sketch shows how to use the Scheduler library;
21+
* Queue events between tasks.
22+
*/
23+
24+
#include <Scheduler.h>
25+
#include <Scheduler/Queue.h>
26+
27+
// Check for SparkFun SAMD21 Breakout
28+
#if defined(ARDUINO_ARCH_SAMD) && (USB_PID == 0x8D21)
29+
#define Serial SerialUSB
30+
#endif
31+
32+
Queue<SchedulerClass::func_t, 4> taskq;
33+
34+
volatile unsigned int sum100;
35+
36+
volatile unsigned int fact10;
37+
38+
void sumTo100()
39+
{
40+
const unsigned int N = 100;
41+
sum100 = 0;
42+
for (unsigned int i = 0; i != N+1; ++i)
43+
{
44+
sum100 += i;
45+
delay(random(10));
46+
}
47+
}
48+
49+
void factTo10()
50+
{
51+
const unsigned int N = 10;
52+
fact10 = 1;
53+
for (unsigned int i = 1; i != N+1 ; ++i)
54+
{
55+
fact10 *= i;
56+
delay(random(10));
57+
}
58+
}
59+
60+
void printAlive()
61+
{
62+
Serial.print(millis());
63+
Serial.print(F(":printAlive::"));
64+
Serial.println(F("alive..."));
65+
yield();
66+
}
67+
68+
void printAliveLoop()
69+
{
70+
Serial.print(millis());
71+
Serial.print(F(":printLoop::"));
72+
Serial.println(F("alive loop..."));
73+
delay(500);
74+
}
75+
76+
void setup()
77+
{
78+
Serial.begin(57600);
79+
while (!Serial);
80+
Serial.println(F("SchedulerTaskCompletionQueue: started"));
81+
Serial.flush();
82+
83+
SchedulerClass::func_t task;
84+
85+
task = sumTo100;
86+
Serial.print(millis());
87+
Serial.print(F(":setup::push task sumto100="));
88+
Serial.println(int(&sumTo100));
89+
taskq.push(&task);
90+
91+
task = factTo10;
92+
Serial.print(millis());
93+
Serial.print(F(":setup::push task factTo10="));
94+
Serial.println(int(&factTo10));
95+
taskq.push(&task);
96+
97+
task = printAlive;
98+
Serial.print(millis());
99+
Serial.print(F(":setup::push task printAlive="));
100+
Serial.println(int(&printAlive));
101+
taskq.push(&task);
102+
103+
// Start two event handlers
104+
Scheduler.startLoop(taskRunner);
105+
Scheduler.startLoop(taskRunner);
106+
Scheduler.startLoop(printAliveLoop);
107+
}
108+
109+
void loop()
110+
{
111+
delay(1000);
112+
113+
Serial.print(millis());
114+
Serial.print(F(":loop sum100="));
115+
Serial.println(sum100);
116+
117+
Serial.print(millis());
118+
Serial.print(F(":loop fact10="));
119+
Serial.println(fact10);
120+
}
121+
122+
void taskRunner()
123+
{
124+
// Pull events
125+
SchedulerClass::func_t* task;
126+
taskq.pull(task);
127+
(*task)();
128+
}

0 commit comments

Comments
 (0)