Skip to content

Commit 1c59599

Browse files
committed
update README.md and example
1 parent b271e7d commit 1c59599

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ It should be noted that these are not “threads” in the real computer-science
3232
There are many examples showing many ways to use it. Here, we will explain Class itself,
3333
what it does and "how" it does.
3434

35-
There are basicaly, two Classes included in this Library:
36-
`Thread` and `ThreadController` (that inherits from Thread).
35+
There are basicaly, three Classes included in this Library:
36+
`Thread`, `ThreadController` and `StaticThreadController` (both controllers inherit from Thread).
3737

3838
- `Thread class`: This is the basic class, witch contains methods to set and run callbacks,
3939
check if the Thread should be runned, and also creates a unique ThreadID on the instantiation.
4040

4141
- `ThreadController class`: Responsable for "holding" multiple Threads. Can also be called
4242
as "a group of Threads", and is used to perform run in every Thread ONLY when needed.
4343

44+
- `StaticThreadController class`: Slighly faster and smaller version of `ThreadController`.
45+
It works similar to `ThreadController`, but once constructed it can't add or remove threads to run.
46+
4447
* The instantiation of a Thread class is very simple:
4548

4649
```c++
@@ -93,9 +96,9 @@ controller.add(&sensorReadings);
9396
```
9497
or
9598
```c++
96-
// Instantiate a new StaticThreadController
97-
StaticThreadController<2> controller (Thread(my_callback, my_interval), Thread(his_callback, his_interval));
98-
// You don't need to do anything else, the treads will be created and kept inside controller
99+
// Instantiate a new StaticThreadController with the number of threads to be supplied as template parameter
100+
StaticThreadController<3> controller (&myThread, &hisThread, &sensorReadings);
101+
// You don't need to do anything else, controller now contains all the threads.
99102
...
100103
```
101104
@@ -136,7 +139,8 @@ inside another). Check `ControllerInController` example.
136139

137140
* There is a `StaticThreadController` which is better to use when you know exact number of
138141
threads to run. You cannot add or remove threads in runtime, but `StaticThreadController`
139-
doesn't have any memory overhead to keep all the treads together, also the code may be slighly
142+
doesn't have additional memory overhead to keep all the treads together, doesn't have any
143+
limitations how many threads to store (except of available memory) and also the code may be slighly
140144
more optimized because all the threads always exist and no need to do any runtime checks.
141145

142146
* Check the full example `CustomTimedThread` for a cool application of Threads that runs
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <Thread.h>
2+
#include <StaticThreadController.h>
3+
4+
//My Thread (as a pointer)
5+
Thread* myThread = new Thread();
6+
//His Thread (not pointer)
7+
Thread hisThread = Thread();
8+
9+
// callback for myThread
10+
void niceCallback(){
11+
Serial.print("COOL! I'm running on: ");
12+
Serial.println(millis());
13+
}
14+
15+
// callback for hisThread
16+
void boringCallback(){
17+
Serial.println("BORING...");
18+
}
19+
20+
// callback for theThread
21+
void justCallback(){
22+
Serial.println("executing...");
23+
}
24+
25+
//The Thread (as a pointer) with justCallback initialized
26+
Thread* theThread = new Thread(justCallback);
27+
28+
// StaticThreadController that will controll all threads
29+
// All non-pointers go with '&', but pointers go without '&',
30+
StaticThreadController<3> controll (myThread, &hisThread, theThread);
31+
32+
void setup(){
33+
Serial.begin(9600);
34+
35+
// Configure myThread
36+
myThread->onRun(niceCallback);
37+
myThread->setInterval(500);
38+
39+
// Configure hisThread
40+
hisThread.onRun(boringCallback);
41+
hisThread.setInterval(250);
42+
43+
// Set interval for theThread using StaticThreadController interface
44+
controll[3].setInterval(375);
45+
}
46+
47+
void loop(){
48+
// run StaticThreadController
49+
// this will check every thread inside ThreadController,
50+
// if it should run. If yes, he will run it;
51+
controll.run();
52+
53+
// Rest of code
54+
float h = 3.1415;
55+
h/=2;
56+
}

0 commit comments

Comments
 (0)