Create a multiboot toolbox kit
They are’nt real threads, that why I’m saying emulation of threads. I’m using the TaskScheduler library :
Github : https://github.com/arkhipenko/TaskScheduler Arduino : http://playground.arduino.cc/Code/TaskScheduler Documentation :http://www.smart4smart.com/TaskScheduler.html The library allows you to :
- Run a function periodically
- Run a function a certain number of time
- Create a scheduler with priorities
- Dynamically change frequencies, iterations
- …
Launch a thread with a frequency time
#include <TaskScheduler.h>
void t2Callback();
Task t2(3000, TASK_FOREVER, &t2Callback);
Scheduler runner;
void t2Callback() {
Serial.print("La tâche t2 se lance toutes les 3 secondes.");
}
void setup () {
runner.init();
runner.addTask(t2);
t2.enable();
}
void loop () {
/* Ici on peux mettre tout autre code qui va boucler à l'infini*/
runner.execute();
}
Launch a thread with a frequency time during a certain time
#define _TASK_SLEEP_ON_IDLE_RUN
#include <TaskScheduler.h>
int ledR=2;
Scheduler ts;
void WrapperCallback();
bool BlinkEnable();
void BlinkDisable();
void LEDON();
void LEDOFF();
/* 1 jour = 86 400 000 microsecondes*/
Task tWrapper(86400000L,TASK_FOREVER,&WrapperCallback,&ts,true);
Task tBlink(5000,TASK_ONCE,NULL,&ts,false,&BlinkEnable,&BlinkDisable);
/* Pendant 5 secondes*/
Task tLED(0,TASK_FOREVER,NULL,&ts,false,NULL,&LEDOFF);
/*--------------------------------*/
void WrapperCallback(){
tBlink.restartDelayed();
}
/*----------------------------------*/
bool BlinkEnable(){
tLED.setCallback(&LEDON);
tLED.enable();
return true;
}
void BlinkDisable(){
tLED.disable();
}
void LEDON(){
digitalWrite(ledR,HIGH);
}
void LEDOFF(){
digitalWrite(ledR,LOW);
}
/*----------------------------------*/
void setup() {
pinMode(ledR,OUTPUT);
}
void loop() {
ts.execute();
}