Add Library

This commit is contained in:
Siwat Sirichai 2019-08-09 09:01:56 +07:00
parent e365b9dbd9
commit 3c47103b39
318 changed files with 56465 additions and 0 deletions

View file

@ -0,0 +1,78 @@
#include <Thread.h>
#include <ThreadController.h>
int ledPin = 13;
// ThreadController that will controll all threads
ThreadController controll = ThreadController();
//My Thread
Thread myThread = Thread();
//His Thread
Thread hisThread = Thread();
//Blink Led Thread
Thread blinkLedThread = Thread();
//ThreadController, that will be added to controll
ThreadController groupOfThreads = ThreadController();
// 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 blinkLedThread
void blinkLed(){
static bool ledStatus = false;
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
Serial.print("blinking: ");
Serial.println(ledStatus);
}
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Configure myThread
myThread.onRun(niceCallback);
myThread.setInterval(500);
// Configure hisThread
hisThread.onRun(boringCallback);
hisThread.setInterval(250);
// Configure blinkLedThread
blinkLedThread.onRun(blinkLed);
blinkLedThread.setInterval(100);
// Adds myThread to the controll
controll.add(&myThread);
// Adds hisThread and blinkLedThread to groupOfThreads
groupOfThreads.add(&hisThread);
groupOfThreads.add(&blinkLedThread);
// Add groupOfThreads to controll
controll.add(&groupOfThreads);
}
void loop(){
// run ThreadController
// 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;
}

View file

@ -0,0 +1,100 @@
#include <Thread.h>
#include <ThreadController.h>
/*
This example, requires a Timer Interrupt Library.
If you are using Arduino NANO, UNO... (with ATmega168/328)
Please go to: http://playground.arduino.cc/code/timer1
If you are using Arduino DUE,
Please go to: https://github.com/ivanseidel/DueTimer
Include the library corresponding to your Arduino.
*/
// #include <DueTimer.h>
// #include <TimerOne.h>
// ThreadController that will controll all threads
ThreadController controll = ThreadController();
//My Thread
Thread myThread = Thread();
//His Thread
Thread hisThread = Thread();
// callback for myThread
void myThreadCallback(){
Serial.println("myThread\t\tcallback");
}
// callback for hisThread
void hisThreadCallback(){
Serial.println("\thisThread\tcallback");
}
// This is the callback for the Timer
void timerCallback(){
controll.run();
}
void setup(){
Serial.begin(9600);
// Configure myThread
myThread.onRun(myThreadCallback);
myThread.setInterval(500);
// Configure myThread
hisThread.onRun(hisThreadCallback);
hisThread.setInterval(200);
// Adds both threads to the controller
controll.add(&myThread); // & to pass the pointer to it
controll.add(&hisThread);
/*
If using DueTimer...
*/
// Timer1.attachInterrupt(timerCallback).start(20000);
/*
If using TimerOne...
*/
// Timer1.initialize(20000);
// Timer1.attachInterrupt(timerCallback);
// Timer1.start();
}
void waitSerial(){
while (!Serial.available());
delay(10);
while (Serial.available() && Serial.read());
}
void loop(){
while(1){
noInterrupts(); // Call to disable interrupts
Serial.println("Type anyting to stop myThread!");
interrupts(); // Call to enable interrupts
waitSerial();
myThread.enabled = false;
noInterrupts();
Serial.println("Type anyting to stop hisThread!");
interrupts();
waitSerial();
hisThread.enabled = false;
noInterrupts();
Serial.println("Type anyting to enable myThread!");
interrupts();
waitSerial();
myThread.enabled = true;
noInterrupts();
Serial.println("Type anyting to enable hisThread!");
interrupts();
waitSerial();
hisThread.enabled = true;
}
}

View file

@ -0,0 +1,257 @@
/*
This is an example from ArduinoThread. You can find more information
in https://github.com/ivanseidel/ArduinoThread.
Coded by Ivan Seidel, Jun/2014 - ivanseidel@gmail.com
Dont be afraid. 90% is commented lines. READ them, they will teach you.
*/
#include <Thread.h>
#include <ThreadController.h>
/*
This example provides an object-oriented approach to
develop a custom Thread that overrides the 'shouldRun'
method, to only run the thread after a button was pushed.
After the push, it should 'keep' running for a desired time.
It should also provide us, a way to easily implement this
controll multiple times, without trouble.
We are giving this Custom Thread the name 'ButtonThread'.
Exemplifying what it does:
+ ButtonThread added to our mainController ThreadList
=> Instantiated with a custom Pin #,
=> and a time duration (in miliseconds)
+ ButtonThread is not running.
+ When the button is pressed:
+ Thread will start and keep running.
+ If the thread runned for our defined period,
we stop it.
================ HOW TO SETUP HARDWARE ==================
In order to make this example work with any arduino, hook up
the pins on the board to 3 buttons. You can change the inputs
if you need below here.
The Buttons are being SOFTWARE pulled UP (to VCC), and when
pushed, should go LOW. Connect like this:
(Arduino Input) <----> (Btn) <----> GND (-)
We are using digital pins 9, 10 and 11 as input.
It also uses a LED, but we are using the default one in the board.
=============== WHAT YO LEARN WITH THIS =================
1) Threads are actually running in 'parallel'.
Since each thread process time is very tiny, they appear
as being runned in parallel.
Because of that, clicking multiple buttons at any time,
will looks like there is a program for each one of them.
2) If you keep the button 'pressed', it will continue to run.
Since we are 'enabling' the thread, and reseting the timer
flag (_lastButtonPushed) every time the button is pressed,
we should notice that in btn1Callback, where we print this
flag, it will never go beyond 0 if we keep pressing it.
3) The LED turns off, only because the Thread runs a last time
with the flag 'enabled' as false. This way, we can turn the
LED off and remain OFF until we press it egain.
I hope you enjoy, and learn some advanced-cool stuf with this tutorial.
Any feedback is apreciated!
*/
#define BTN1 9
#define BTN2 10
#define BTN3 11
#define LED 13
// ThreadController that will controll all button threads
ThreadController controll = ThreadController();
// Here we implement our custom ButtonThread, that Inherits from Thread
class ButtonThread: public Thread{
public:
// Our custom thread attributes
int pin;
long duration;
long _lastButtonPushed;
/*
Our Constructor. This will initialize the thread
with it's corresponding pin and duration after clicked.
*/
ButtonThread(int _pin, long _duration): Thread(){
// Set our attributes on construct
pin = _pin;
duration = _duration;
_lastButtonPushed = 0;
// Thread will start disabled
enabled = false;
// Configure the pin as INPUT and enable pull-up
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
/*
Override the method responsible for
checking if the thread should run.
It will first check if the button is pressed.
If so, we enable the thread, and then let the
"Old" default Thread method 'shouldRun' return if
it should run.
*/
bool shouldRun(unsigned long time){
// Override enabled on thread when pin goes LOW.
if(digitalRead(pin) == LOW){
enabled = true;
/*
Here, we save the current time in this object,
to compare it later.
the 'time' parameter in this method, is an override for the
'millis()' method. It allows who is checking the thread, to
pass a custom time.
This is sintax for writing an 'inline' if is very usefull,
it's the same as:
if(time > 0){
_lastButtonPushed = time;
}else{
_lastButtonPushed = millis();
}
*/
_lastButtonPushed = (time ? time : millis());
}
// Let default method check for it.
return Thread::shouldRun(time);
}
/*
We 'disable' the thread after the duration on the
'run' method.
What we should do here, is check if the time saved
in the _lastButtonPushed variable plus the duration,
is greater than our current time. If that's true, it
means we exceeded the thread time, and that we must
disable it and prevent from running.
*/
void run(){
// Check if time elapsed since last button push
if(millis() > _lastButtonPushed + duration){
// It exceeded time. We should disable it.
enabled = false;
}
/*
Run the thread.
Note that this method will only get called
from the ThreadList, IF the 'shouldRun' returns true.
If the thread is not enabled anymore, it will run a 'last'
time with the flag 'enabled' as false, meaning it's the last
run in the period. You can use it for doing something only
before it stops running.
*/
Thread::run();
}
};
/*
ButtonThreads objects instantiation
(we are instantiating 2 as a member, and one
as pointer in the setup, just to show you
different ways of doing it)
*/
// Thread 1 will be reading BTN1 pin, and will run for 3 secs
ButtonThread btn1Thread(BTN1, 3000);
// Thread 2 will be reading BTN1 pin, and will run for 5 secs
ButtonThread btn2Thread = ButtonThread(BTN2, 5000);
// Thread 3 will be instantiated in the setup()
ButtonThread *btn3Thread;
/*
Callback for ButtonThreads
*/
void btn1Callback(){
// When it's running, this thread will write to the serial.
/*
This math will print 'how long' the thread has been running,
since the button was/is pressed.
After pressing it, it should print as 0, and goes up untill
the thread duration (in this case, +-5000ms).
*/
Serial.print("BTN 1 Thread: ");
Serial.println(millis() - btn1Thread._lastButtonPushed);
}
void btn2Callback(){
/*
This thread will remain with the LED on pin 13 turned on
while it is running.
We detect that this method is called for the LAST time, if
the flag 'enabled' is FALSE on the btn2Thread object.
So, basically: If it's TRUE, we should turn ON the led, if not
we should turn OFF. We can simplify that into one line.
(Same 'inline' sintax as above)
*/
digitalWrite(LED, btn2Thread.enabled ? HIGH : LOW);
}
void btn3Callback(){
// When it's running, this thread will also write to the serial
Serial.println("BTN 3 Thread");
}
void setup(){
// Configure serial and output pins
Serial.begin(9600);
pinMode(LED, OUTPUT);
// Configure btn1Thread callback
// (During the 'enabled' time, it will run every 100ms, aka Interval)
btn1Thread.onRun(btn1Callback);
btn1Thread.setInterval(100);
// Configure btn2Thread callback and interval
btn2Thread.onRun(btn2Callback);
btn2Thread.setInterval(200);
// Instantiate btn3Thread
btn3Thread = new ButtonThread(BTN3, 4000);
// Configure btn3Thread callback and interval
btn3Thread->onRun(btn3Callback);
btn3Thread->setInterval(100);
// Adds all threads to the controller
controll.add(&btn1Thread); // & to pass the pointer to it
controll.add(&btn2Thread);
controll.add(btn3Thread); // Its already a pointer, no need for &
}
void loop(){
// Here we just run the main thread controller
controll.run();
}

View file

@ -0,0 +1,105 @@
#include "Thread.h"
#include "ThreadController.h"
/*
This is a more "complex" for of using Threads.
You can also inherit from Thread, and do your entire code on the class.
This allows you, to create for example:
Sensor Readings (aquire, filter, and save localy values)
Custom Blinks, Beeps...
Anything you can imagine.
Threads are more "usefull" when used within Timer interrupts
This way of coding is more "reusable", and "correct" (Object Oriented)
*/
/*
This example, requires a Timer Interrupt Library.
If you are using Arduino NANO, UNO... (with ATmega168/328)
Please go to: http://playground.arduino.cc/code/timer1
If you are using Arduino DUE,
Please go to: https://github.com/ivanseidel/DueTimer
Include the library corresponding to your Arduino.
*/
#include <DueTimer.h>
// #include <TimerOne.h>
// Create a new Class, called SensorThread, that inherits from Thread
class SensorThread: public Thread
{
public:
int value;
int pin;
// No, "run" cannot be anything...
// Because Thread uses the method "run" to run threads,
// we MUST overload this method here. using anything other
// than "run" will not work properly...
void run(){
// Reads the analog pin, and saves it localy
value = map(analogRead(pin), 0,1023,0,255);
runned();
}
};
// Now, let's use our new class of Thread
SensorThread analog1 = SensorThread();
SensorThread analog2 = SensorThread();
// Instantiate a new ThreadController
ThreadController controller = ThreadController();
// This is the callback for the Timer
void timerCallback(){
controller.run();
}
void setup(){
Serial.begin(9600);
// Configures Thread analog1
analog1.pin = A1;
analog1.setInterval(100);
// Configures Thread analog2
analog2.pin = A2;
analog2.setInterval(100);
// Add the Threads to our ThreadController
controller.add(&analog1);
controller.add(&analog2);
/*
If using DueTimer...
*/
Timer1.attachInterrupt(timerCallback).start(10000);
/*
If using TimerOne...
*/
// Timer1.initialize(20000);
// Timer1.attachInterrupt(timerCallback);
// Timer1.start();
}
void loop(){
// Do complex-crazy-timeconsuming-tasks here
delay(1000);
// Get the fresh readings
Serial.print("Analog1 Thread: ");
Serial.println(analog1.value);
Serial.print("Analog2 Thread: ");
Serial.println(analog2.value);
// Do more complex-crazy-timeconsuming-tasks here
delay(1000);
}

View file

@ -0,0 +1,35 @@
#include <Thread.h>
int ledPin = 13;
//My simple Thread
Thread myThread = Thread();
// callback for myThread
void niceCallback(){
static bool ledStatus = false;
ledStatus = !ledStatus;
digitalWrite(ledPin, ledStatus);
Serial.print("COOL! I'm running on: ");
Serial.println(millis());
}
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
myThread.onRun(niceCallback);
myThread.setInterval(500);
}
void loop(){
// checks if thread should run
if(myThread.shouldRun())
myThread.run();
// Other code...
int x = 0;
x = 1 + 2;
}

View file

@ -0,0 +1,48 @@
#include <Thread.h>
#include <ThreadController.h>
// ThreadController that will controll all threads
ThreadController controll = ThreadController();
//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...");
}
void setup(){
Serial.begin(9600);
// Configure myThread
myThread->onRun(niceCallback);
myThread->setInterval(500);
// Configure myThread
hisThread.onRun(boringCallback);
hisThread.setInterval(250);
// Adds both threads to the controller
controll.add(myThread);
controll.add(&hisThread); // & to pass the pointer to it
}
void loop(){
// run ThreadController
// 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;
}

View file

@ -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;
}