Skip to content

Commit 5aef01c

Browse files
committed
changes the default priority value if ctor is called with no parameter
adds two functions for querying and setting the priority value at runtime
1 parent af814b4 commit 5aef01c

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

README.md

+22-19
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ illustrates the whole API and its best usage.
3030

3131
ChangeLog
3232
---------
33-
* version 1.1 adds clear example of DS18B20 handling
33+
* version 1.4 changes the default priority value when <code>Tasker</code> is instantiated without the optional parameter. In previous versions the priority was enabled, now it is disabled. I (and also other users of **Tasker**) found the prioritized handling of tasks rather counter-intuitive because it could happen almost randomly that some tasks were sometimes not executed at all (when a higher priority task ran for too long). Whoever wants to keep the original behaviour please instantiate Tasker like this: <code>Tasker tasker(TRUE);</code>. There are also two new functions that help to query or set the priority value: <code>isPrioritized()</code> and <code>setPrioritized(bool)</code>.
34+
35+
* version 1.3 removes the <code>run()</code> function - please call <code>tasker.loop()</code> in your Arduino <code>loop()</code> function instead. This makes **Tasker** much more Arduino friendly and compatible with far more platforms where the Arduino 'kernel' does some housekeeping behind the scenes and needs the <code>loop()</code> to be running for it. It also allowed me to remove the <code>yield()</code> call that didn't really bring anything but issues in compiling on some platforms.
3436

3537
* version 1.2 adds optional priorities when defining tasks
3638

37-
* version 1.3 removes the <code>run()</code> function - please call <code>tasker.loop()</code> in your Arduino <code>loop()</code> function instead. This makes **Tasker** much more Arduino friendly and compatible with far more platforms where the Arduino 'kernel' does some housekeeping behind the scenes and needs the <code>loop()</code> to be running for it. It also allowed me to remove the <code>yield()</code> call that didn't really bring anything but issues in compiling on some platforms.
39+
* version 1.1 adds clear example of DS18B20 handling
3840

3941

4042
How to use
@@ -49,13 +51,13 @@ Tasker API
4951
----------
5052

5153
* <code>Tasker([bool prioritized])</code>. The class constructor takes
52-
an optional bool flag (that is set to true if omitted). If this flag
54+
an optional bool flag (that is set to false if omitted). If this flag
5355
is TRUE then the Tasker prioritizes the scheduled tasks. If the flag
5456
is FALSE then the Tasker considers all scheduled tasks equal. More about priorities later.
5557

5658
```cpp
57-
Tasker tasker; // creates prioritizing tasker
58-
Tasker tasker(FALSE); // creates non-prioritizing tasker
59+
Tasker tasker; // creates non-prioritizing tasker
60+
Tasker tasker(TRUE); // creates prioritizing tasker
5961
```
6062
6163
* <code>setTimeout(function_name, time_in_milliseconds [, optional_int [, optional_priority]])</code>
@@ -83,32 +85,31 @@ Tasker API
8385
}
8486
```
8587

86-
Task priorities
87-
---------------
88-
If the Tasker constructor was not called with a FALSE flag then the internal
88+
Task priorities (optional)
89+
--------------------------
90+
If the Tasker constructor was called with a parameter (TRUE) then the internal
8991
scheduler will prioritize the tasks in its queue. Tasks added later have lower
9092
priority than those added earlier, unless you specify their priority with
9193
optional parameter: the lower its value the higher priority, 0 = highest priority.
9294

9395
```cpp
94-
Tasker tasker;
95-
tasker.setInterval(most_important_fn, ..);
96-
tasker.setInterval(less_important_fn, ..);
97-
tasker.setInterval(highest_priority_fn, .., .., 0);
96+
Tasker tasker(TRUE);
97+
tasker.setInterval(most_important_fn, ..); // formerly added calls have automatically higher priority
98+
tasker.setInterval(less_important_fn, ..); // the later added calls the lower priority they have
99+
tasker.setInterval(highest_priority_fn, .., .., 0); // unless you specify the priority explicitly by the last parameter
98100
```
99101
100102
Normally, when there is enough time for calling each of the scheduled task
101103
at the right time the priorities don't play any role but when a previous task takes
102104
longer time and the scheduler detects that certain tasks are delayed
103-
(are behind their schedule) it needs to decide which task will get run of those
105+
(are behind their schedule) it needs to decide which task will be run of those
104106
that should have been run already. And that's where the tasks' priorities step
105107
in: the task added earlier or with a higher priority will be chosen.
106-
If the priorities were disabled then the scheduler would simply run the next task
107-
in its queue. If all your tasks are equally important you might want to disable
108-
the priorities by passing FALSE into the constructor:
108+
If the priorities were disabled (by default the are) then the scheduler would simply run the next task
109+
in its queue. If all your tasks are equally important (they most probably are) you might simply ignore the whole idea of priorities and their implementation.
109110
110111
```cpp
111-
Tasker tasker(false);
112+
Tasker tasker;
112113
tasker.setInterval(fn, ..);
113114
tasker.setInterval(equally_important_fn, ..);
114115
tasker.setInterval(order_doesnt_matter_fn, ..);
@@ -152,8 +153,10 @@ Petr Stehlík
152153

153154
154155

156+
Web: https://www.pstehlik.cz/
157+
155158
Daily active on Google+: https://plus.google.com/+PetrStehl%C3%ADk
156159

157-
Longer articles published at blog: http://joysfera.blogspot.com/
160+
Longer articles are published at blog: http://joysfera.blogspot.com/
158161

159-
Sometimes tweets as @joysfera
162+
Sometimes tweets as https://twitter.com/joysfera

Tasker.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Tasker for Arduino - cooperative task scheduler with Javascript like API
3-
* Copyleft (c) 2015-2017 Petr Stehlik [email protected]
3+
* Copyleft (c) 2015-2018 Petr Stehlik [email protected]
44
* Distributed under the GNU LGPL http://www.gnu.org/licenses/lgpl.txt
55
*/
66

@@ -18,11 +18,14 @@ typedef void (*TaskCallback)(int);
1818
class Tasker
1919
{
2020
public:
21-
Tasker(bool prioritized = true);
21+
Tasker(bool prioritized = false);
2222
bool setTimeout(TaskCallback func, unsigned long interval, int param = 0, byte prio = TASKER_MAX_TASKS);
2323
bool setInterval(TaskCallback func, unsigned long interval, int param = 0, byte prio = TASKER_MAX_TASKS);
2424
bool setRepeated(TaskCallback func, unsigned long interval, unsigned int repeat, int param = 0, byte prio = TASKER_MAX_TASKS);
2525
void loop(void);
26+
bool isPrioritized() { return t_prioritized; }
27+
void setPrioritized(bool prioritized) { t_prioritized = prioritized; }
28+
2629
private:
2730
struct TASK {
2831
TaskCallback call;

keywords.txt

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ setTimeout KEYWORD2
44
setInterval KEYWORD2
55
setRepeated KEYWORD2
66
loop KEYWORD2
7+
isPrioritized KEYWORD2
8+
setPrioritized KEYWORD2
9+
710
TASKER_MAX_TASKS LITERAL1

library.json

+1-1
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.2",
10+
"version": "1.4",
1111
"frameworks": "arduino",
1212
"platforms": "*"
1313
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Tasker
2-
version=1.2
2+
version=1.4
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)