Skip to content

Commit ac03f7d

Browse files
committed
optional priorities added
1 parent eba0451 commit ac03f7d

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ functions so HTML/JavaScript programmers will feel right at home.
2828
For the unfamiliar with JavaScript there's a short example code included that
2929
illustrates the whole API and its best usage.
3030

31+
ChangeLog
32+
---------
33+
* version 1.1 adds clear example of DS18B20 handling
34+
* version 1.2 adds optional priorities when defining tasks
35+
3136
How to use
3237
----------
3338

@@ -49,17 +54,17 @@ Tasker API
4954
Tasker tasker(FALSE); // creates non-prioritizing tasker
5055
```
5156
52-
* <code>setTimeout(function_name, time_in_milliseconds [, optional_int])</code>
57+
* <code>setTimeout(function_name, time_in_milliseconds [, optional_int [, optional_priority]])</code>
5358
Tasker will call the *function_name* in *time_in_milliseconds* from now.
5459
It will run the function only once. May pass the *optional_int* parameter into the called function.
5560
When the task finishes its Tasker slot is made available for new tasks (more about slots later).
5661
57-
* <code>setInterval(function_name, time_in_milliseconds [, optional_int])</code>
62+
* <code>setInterval(function_name, time_in_milliseconds [, optional_int [, optional_priority]])</code>
5863
Tasker will call the *function_name* repeatedly and forever, every
5964
*time_in_milliseconds* from now on.
6065
May pass the *optional_int* parameter into the called function.
6166
62-
* <code>setRepeated(function_name, time, number_of_repeats [, optional_int])</code>
67+
* <code>setRepeated(function_name, time, number_of_repeats [, optional_int [, optional_priority]])</code>
6368
Tasker will call the *function_name* repeatedly for *number_of_repeats*,
6469
every *time* (in_milliseconds) from now on.
6570
May pass the <code>optional_int</code> parameter into the called function.
@@ -99,22 +104,22 @@ Task priorities
99104
---------------
100105
If the Tasker constructor was not called with a FALSE flag then the internal
101106
scheduler will prioritize the tasks in its queue. Tasks added later have lower
102-
priority than those added earlier. Thus you'll want to add your more
103-
important tasks first and the less important ones add afterwards.
107+
priority than those added earlier, unless you specify their priority with
108+
optional parameter: the lower its value the higher priority, 0 = highest priority.
104109
105110
```cpp
106111
Tasker tasker;
107112
tasker.setInterval(most_important_fn, ..);
108113
tasker.setInterval(less_important_fn, ..);
109-
tasker.setInterval(least_important_fn, ..);
114+
tasker.setInterval(highest_priority_fn, .., .., 0);
110115
```
111116

112117
Normally, when there is enough time for calling each of the scheduled task
113118
at the right time the priorities don't play any role but when a previous task takes
114119
longer time and the scheduler detects that certain tasks are delayed
115120
(are behind their schedule) it needs to decide which task will get run of those
116121
that should have been run already. And that's where the tasks' priorities step
117-
in: the task added earlier (= with a higher priority) will be chosen.
122+
in: the task added earlier or with a higher priority will be chosen.
118123
If the priorities were disabled then the scheduler would simply run the next task
119124
in its queue. If all your tasks are equally important you might want to disable
120125
the priorities by passing FALSE into the constructor:
@@ -158,8 +163,10 @@ as the first instruction in your task function:
158163
I consider this library finished and stable for everyday use. Adding more features
159164
is not expected, the library will stay short, simple and fast.
160165
161-
Enjoy
162-
166+
Author
167+
------
163168
Petr Stehlík
164169
165-
http://joysfera.blogspot.com/ with link to my G+ profile and contact information.
170+
171+
http://joysfera.blogspot.com/
172+
https://plus.google.com/+PetrStehl%C3%ADk

Tasker.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Tasker
1919
{
2020
public:
2121
Tasker(bool prioritized = true);
22-
bool setTimeout(TaskCallback func, unsigned long interval, int param = 0);
23-
bool setInterval(TaskCallback func, unsigned long interval, int param = 0);
24-
bool setRepeated(TaskCallback func, unsigned long interval, unsigned int repeat, int param = 0);
22+
bool setTimeout(TaskCallback func, unsigned long interval, int param = 0, byte prio = TASKER_MAX_TASKS);
23+
bool setInterval(TaskCallback func, unsigned long interval, int param = 0, byte prio = TASKER_MAX_TASKS);
24+
bool setRepeated(TaskCallback func, unsigned long interval, unsigned int repeat, int param = 0, byte prio = TASKER_MAX_TASKS);
2525
void loop(void);
2626
void run(void) { while(true) { loop(); yield(); } }
2727
private:
@@ -45,21 +45,24 @@ Tasker::Tasker(bool prioritized)
4545
t_prioritized = prioritized;
4646
}
4747

48-
bool Tasker::setTimeout(TaskCallback func, unsigned long interval, int param)
48+
bool Tasker::setTimeout(TaskCallback func, unsigned long interval, int param, byte prio)
4949
{
50-
return setRepeated(func, interval, 1, param);
50+
return setRepeated(func, interval, 1, param, prio);
5151
}
5252

53-
bool Tasker::setInterval(TaskCallback func, unsigned long interval, int param)
53+
bool Tasker::setInterval(TaskCallback func, unsigned long interval, int param, byte prio)
5454
{
55-
return setRepeated(func, interval, 0, param);
55+
return setRepeated(func, interval, 0, param, prio);
5656
}
5757

58-
bool Tasker::setRepeated(TaskCallback func, unsigned long interval, unsigned int repeat, int param)
58+
bool Tasker::setRepeated(TaskCallback func, unsigned long interval, unsigned int repeat, int param, byte prio)
5959
{
6060
if (t_count >= TASKER_MAX_TASKS || interval == 0)
6161
return false;
62-
TASK &t = tasks[t_count];
62+
byte pos = (prio < t_count) ? prio : t_count;
63+
if (pos < t_count)
64+
memmove(tasks+pos+1, tasks+pos, sizeof(TASK)*(t_count-pos));
65+
TASK &t = tasks[pos];
6366
t.call = func;
6467
t.interval = interval;
6568
t.param = param;

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "git",
88
"url": "https://github.com/joysfera/arduino-tasker"
99
},
10-
"version": "1.1",
10+
"version": "1.2",
1111
"frameworks": "arduino",
1212
"platforms": "*"
1313
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Tasker
2-
version=1.1
2+
version=1.2
33
author=Petr Stehlík
44
maintainer=Petr Stehlík
55
sentence=Get rid of delay() calls, schedule tasks instead.

0 commit comments

Comments
 (0)