-
Notifications
You must be signed in to change notification settings - Fork 0
/
CounterTask.h
52 lines (45 loc) · 1.42 KB
/
CounterTask.h
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
#ifndef __COUNTERTASK_H__
#define __COUNTERTASK_H__
// Our task that counts...
class CounterTask
{
public:
// constructs an object with the max-count.
CounterTask(long maxCount) : _counter(0), _maxCount(maxCount)
{}
/*
* Between the Task_Begin and Task_End macros is your method that defines the process of your task.
* bool GlobalTask() - return value indicates if we yielded (true) or naturally exited the function (false).
*/
Task_Begin(Execute)
{
// no - this will not hang the program ...
while(true)
{
// ... because we yield from this method (that is the magic!)
Task_YieldUntil(IncrementCount());
/* Execution will exit (yield) and enter (next call to Execute) exactly here (Task_YieldUntil).
* So make sure you perform the action required by the task (for this part) in one method.
*/
// execution falls through when IncrementCount returned true.
_counter = 0;
Serial.print("Counted to ");
Serial.println(_maxCount);
}
}
Task_End
private:
// returns true if the count has been reached.
bool IncrementCount()
{
_counter++;
return _counter > _maxCount;
}
// vars used by our task
long _counter;
long _maxCount;
// This var is used by the macro's to maintain the state of the process
// and must always be present for each Task method (only one task method per class).
int _task;
};
#endif //__COUNTERTASK_H__