Wednesday, September 17, 2014

MULTI THREADING IN ARDUINO #18

Multi Threading makes the operation of multiple threads or process to run at a same time.What is does is switches between two tasks very quick so that it seems all the jobs are done at same time . This is pseudo parallelism. First we need a standard C PT thread library.
Here's the link for the PT library.
https://drive.google.com/file/d/0Bx0yz8JxaXcALWQ4WUVJaFl2c3M/edit?usp=sharing

Second we need to extract the library and then add the folder to arduino library folder.
Then include the file in our code using

#inlcude  <pt.h>

Now lets blink two leds attach to pin 13 (default led pin) and add another to pin 12.Toggle them with  500 ms and 1000ms with these line of codes.

static int LED_1 = 13;
static int LED_2 = 12;
static long led1rate = 500;
static long led2rate = 1000;

Now we have two tasks to do, one to toggle led 1 and another to toggle led 2. And lets make a function with toggles andy gpio pin.

static struct pt pt1,pt2; // This makes two threads to create called pt1 and pt2.

void led_toggle(int led){
  digitalWrite(led,!digitalRead(led));//toggles the pin led.
}

Now defining the duration of the threads to call using

pt *pt is pointer to out thread variable
led is pin number and timeout is delay for toggle.

static int thread1(struct pt *pt,int led,long timeout){
  static long t1=0;//static timer which sets only once
  PT_BEGIN(pt);//thread begins here
  while(1){
    PT_WAIT_UNTIL(pt,(millis()-t1)>timeout);//wait until time out period here and do another task
    led_toggle(LED_1);//toggle led
    t1=millis();//get current time
  }
  PT_END(pt);//threads end
}

and in the case for the led two

static int thread2(struct pt *pt,int led,long timeout){
  static long t2=0;
  PT_BEGIN(pt);
  while(1){
    PT_WAIT_UNTIL(pt,(millis()-t2)>timeout);
    led_toggle(LED_2);
    t2=millis();
  }
  PT_END(pt);
}

Now lets setup the project using void setup
void setup() {
  pinMode(LED_1,OUTPUT);
  pinMode(LED_2,OUTPUT);
  PT_INIT(&pt1);// initialize the threads
  PT_INIT(&pt2);
}


Now run the loop forever. then the led starts blinking with led1rate and led2rate foreever.
void loop() {
  thread1(&pt1,LED_1,led1rate);
  thread2(&pt2,LED_2,led2rate);
}

Here's the all code in once. .

#include <pt.h>
static int LED_1 = 13;
static int LED_2 = 12;
static long led1rate = 500;
static long led2rate = 1000;
static struct pt pt1,pt2;

void led_toggle(int led){
  digitalWrite(led,!digitalRead(led));
}

static int thread1(struct pt *pt,int led,long timeout){
  static long t1=0;
  PT_BEGIN(pt);
  while(1){
    PT_WAIT_UNTIL(pt,(millis()-t1)>timeout);
    led_toggle(LED_1);
    t1=millis();
  }
  PT_END(pt);
}

static int thread2(struct pt *pt,int led,long timeout){
  static long t2=0;
  PT_BEGIN(pt);
  while(1){
    PT_WAIT_UNTIL(pt,(millis()-t2)>timeout);
    led_toggle(LED_2);
    t2=millis();
  }
  PT_END(pt);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
  PT_INIT(&pt1);
  PT_INIT(&pt2);
}

void loop() {
  // put your main code here, to run repeatedly:
  thread1(&pt1,LED_1,led1rate);
  thread2(&pt2,LED_2,led2rate);
}

No comments:

Post a Comment