You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Arduino does not support "REAL" parallel tasks (aka Threads), but we can make use of this Library to
4
-
improve our code, and easily schedule tasks with fixed (or variable) time between runs.
5
-
6
-
This Library helps to maintain organized and to facilitate the use of multiple tasks. We can
7
-
use Timers Interrupts, and make it really powerfull, running "pseudo-background" tasks under the rug.
8
-
9
-
For example, I personaly use it for all my projects, and put all sensor aquisition and
10
-
filtering inside it, leaving the main loop, just for logic and "cool" part.
11
-
12
-
#### ArduinoThreads is a library for managing the periodic execution of multiple tasks.
13
-
14
-
Blinking an LED is often the very first thing an Arduino user learns. And this demonstrates that periodically performing one single task, like toggling the LED state, is really easy. However, one may quickly discover that managing multiple periodic tasks is not so simple if the tasks have different execution periods.
15
-
16
-
ArduinoThreads is designed to simplify programs that need to perform multiple periodic tasks. The user defines a Thread object for each of those tasks, then lets the library manage their scheduled execution.
17
-
18
-
It should be noted that these are not “threads” in the real computer-science meaning of the term: tasks are implemented as functions that are periodically run to completion. On the one hand, this means that the only way a task can “yield” the CPU is by returning to the caller, and it is thus inadvisable to delay() or do long waits inside a task. On the other hand, this makes ArduinoThreads memory friendly, as no stack needs to be allocated per task.
3
+
## ArduinoThreads Motivation
4
+
Arduino does not support isolated parallel tasks ([Threads](https://en.wikipedia.org/wiki/Thread_(computing))),
5
+
but we can make the main `loop` switch function execution conditionally and
6
+
thus simulate threading with [Protothread](https://en.wikipedia.org/wiki/Protothread) mechanism.
7
+
This library implements it and helps you to:
8
+
9
+
- schedule, manage and simplify parallel, periodic tasks
10
+
- define fixed or variable time between runs
11
+
- organize the code in any type of project
12
+
- put all sensor readings in a thread
13
+
- keep the main loop clean
14
+
- hide the complexity of thread management
15
+
- run "pseudo-background" tasks using Timer interrupts
16
+
17
+
Blinking an LED is often the very first thing an Arduino user learns.
18
+
And this demonstrates that periodically performing one single task, like toggling the LED state, is really easy.
19
+
However, one may quickly discover that managing multiple periodic tasks is not so simple
20
+
if the tasks have different schedule.
21
+
22
+
The user defines a Thread object for each of those tasks, then lets the library manage their scheduled execution.
23
+
24
+
It should be noted that these are not “threads” in the real computer-science meaning of the term:
25
+
tasks are implemented as functions that are run periodically.
26
+
On the one hand, this means that the only way a task can *yield* the CPU is by returning to the caller,
27
+
and it is thus inadvisable to `delay()` or do long waits inside any task.
28
+
On the other hand, this makes ArduinoThreads memory friendly, as no stack need to be allocated per task.
19
29
20
30
## Installation
21
31
22
-
1."Download":https://github.com/ivanseidel/ArduinoThread/archive/master.zip the Master branch from gitHub.
23
-
2. Unzip and modify the Folder name to "ArduinoThread" (Remove the '-master')
32
+
1. Download[the Master branch](https://github.com/ivanseidel/ArduinoThread/archive/master.zip) from gitHub.
33
+
2. Unzip and modify the Folder name to "ArduinoThread" (Remove the '-master' suffix)
24
34
3. Paste the modified folder on your Library folder (On your `Libraries` folder inside Sketchbooks or Arduino software).
25
-
4. Restart Arduino IDE
35
+
4. Restart the Arduino IDE
26
36
27
-
**If you are here, because another Library requires this class, just don't waste time reading bellow. Install and ready.**
37
+
**If you are here just because another library requires a class from ArduinoThread, then you are done now
38
+
.**
28
39
29
40
30
41
## Getting Started
31
42
32
-
There are many examples showing many ways to use it. Here, we will explain Class itself,
33
-
what it does and "how" it does.
43
+
There are many examples showing many ways to use it. We will explain Class itself,
44
+
what it does and how it does.
34
45
35
-
There are basicaly, three Classes included in this Library:
36
-
`Thread`, `ThreadController` and `StaticThreadController` (both controllers inherit from Thread).
46
+
There are three main classes included in the library:
47
+
`Thread`, `ThreadController` and `StaticThreadController` (both controllers inherit from `Thread`).
37
48
38
-
-`Thread class`: This is the basic class, witch contains methods to set and run callbacks,
39
-
check if the Thread should be runned, and also creates a unique ThreadID on the instantiation.
49
+
-`Thread`: Basic class, witch contains methods to set and run callbacks,
50
+
check if the Thread should be run, and also creates a unique ThreadID on the instantiation.
40
51
41
-
-`ThreadController class`: Responsable for "holding" multiple Threads. Can also be called
42
-
as "a group of Threads", and is used to perform run in every Thread ONLY when needed.
52
+
-`ThreadController`: Responsible for managing multiple Threads. Can also be thought of
53
+
as "a group of Threads", and is used to perform `run` in every Thread ONLY when needed.
43
54
44
-
-`StaticThreadController class`: Slighly faster and smaller version of `ThreadController`.
55
+
-`StaticThreadController`: Slightly faster and smaller version of the`ThreadController`.
45
56
It works similar to `ThreadController`, but once constructed it can't add or remove threads to run.
46
57
47
-
* The instantiation of a Thread class is very simple:
58
+
#### Create Thread instance:
48
59
49
60
```c++
50
61
Thread myThread = Thread();
51
62
// or, if initializing a pointer
52
63
Thread* myThread = new Thread();
53
64
```
54
65
55
-
56
-
* Setting up a thread is essential. You can configure many things:
66
+
#### Setup thread behaviour
67
+
You can configure many things:
57
68
58
69
```c++
59
70
myThread.enabled = true; // Default enabled value is true
0 commit comments