Skip to content

Commit

Permalink
update README.md and example
Browse files Browse the repository at this point in the history
  • Loading branch information
CAHEK7 committed Sep 4, 2016
1 parent b271e7d commit 1c59599
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ It should be noted that these are not “threads” in the real computer-science
There are many examples showing many ways to use it. Here, we will explain Class itself,
what it does and "how" it does.

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

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

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

- `StaticThreadController class`: Slighly faster and smaller version of `ThreadController`.
It works similar to `ThreadController`, but once constructed it can't add or remove threads to run.

* The instantiation of a Thread class is very simple:

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

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

* Check the full example `CustomTimedThread` for a cool application of Threads that runs
Expand Down
56 changes: 56 additions & 0 deletions examples/StaticThreadController/StaticThreadController.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <Thread.h>
#include <StaticThreadController.h>

//My Thread (as a pointer)
Thread* myThread = new Thread();
//His Thread (not pointer)
Thread hisThread = Thread();

// callback for myThread
void niceCallback(){
Serial.print("COOL! I'm running on: ");
Serial.println(millis());
}

// callback for hisThread
void boringCallback(){
Serial.println("BORING...");
}

// callback for theThread
void justCallback(){
Serial.println("executing...");
}

//The Thread (as a pointer) with justCallback initialized
Thread* theThread = new Thread(justCallback);

// StaticThreadController that will controll all threads
// All non-pointers go with '&', but pointers go without '&',
StaticThreadController<3> controll (myThread, &hisThread, theThread);

void setup(){
Serial.begin(9600);

// Configure myThread
myThread->onRun(niceCallback);
myThread->setInterval(500);

// Configure hisThread
hisThread.onRun(boringCallback);
hisThread.setInterval(250);

// Set interval for theThread using StaticThreadController interface
controll[3].setInterval(375);
}

void loop(){
// run StaticThreadController
// this will check every thread inside ThreadController,
// if it should run. If yes, he will run it;
controll.run();

// Rest of code
float h = 3.1415;
h/=2;
}

0 comments on commit 1c59599

Please sign in to comment.