chipKIT® Development Platform

Inspired by Arduino™

Question using TaskManager

Created Thu, 15 Jan 2015 11:19:49 +0000 by GastonLagaffe


GastonLagaffe

Thu, 15 Jan 2015 11:19:49 +0000

Salut, as I am new to chipKit I dig through the documentation and stumbled into TaskManager. Looks great but the documentation is sparse, especially there is only one example (which works great). I try to use setTaskVar but cannot get it to work. Anybody experienced with this? This is the test code:

#include "System_Defs.h"

int blink_id;
unsigned long blink_var;
void blink_task(int id, void * tptr) {
  int led_pin;
  led_pin = *(int *)getTaskVar(id);
//  led_pin = 13;
  digitalWrite(led_pin, !digitalRead(led_pin)); // Toggle pin state
}

void setup() {
  pinMode(13, OUTPUT);
  blink_id = createTask(blink_task, 250, TASK_ENABLE, &blink_var);
  setTaskVar(blink_id, (void *)13);
}

void loop() {
}

In my understanding, the code should launch the task and the setTastVar command should make the led on pin 13 blink. However, nothing happens. Hard-coding the led_pin (comment line) works.

Ciao, Mathias


majenko

Thu, 15 Jan 2015 11:45:10 +0000

You've got two extra *s you don't want.

The task variable is stored as a pointer - it's up to you to interpret it right.

In this line:

setTaskVar(blink_id, (void *)13);

you're setting the internal task variable pointer to point to address 13. Then in this line:

led_pin = *(int *)getTaskVar(id);

you're then saying "Get me the value that is stored at the address stored in the task variable".

Which if course is wrong.

Instead you just want to cast the address returned (address 13) straight to an integer:

led_pin = (int)getTaskVar(id);

GastonLagaffe

Thu, 15 Jan 2015 17:40:51 +0000

Salut,

thanks for "pointing" me into the right direction. After years with Fortran my mind is corrupted there :lol:

I created a short example that may be used by others. It makes a set a LED blink and changes the blinking period in loop(). I deliberately use delay() to demonstrate the tasks running while in delay().

This is a brilliant advantage over the Arduino! No fiddling with timers just to have a bunch of parallel tasks - I love it :D

Now I need to understand how to access variables from within the task routine... My code:

/*
 Blink Task (tested in UECIDE version 0.8.7)
 Utlizing the chipKIT task manager rather than delay functions.
 This example code is in the public domain.
*/
#include "System_Defs.h"
const uint8_t LEFT=0;
const uint8_t RIGHT=1;
uint8_t nP[2][8] = {
	{
		0,17, 9,10,11,12,13,14	}
	,{
		18,17, 1, 2, 3, 6, 7, 8	}
}; // pins of version 1.0 using DP32 board definitions

volatile int blink_id[8];
unsigned long blink_var[8];
int toggle_id;
unsigned long toggle_var;
volatile int state = 0;

void blink_task(int id, void * tptr) {
  int led_pin;
  unsigned long period;
  period = getTaskPeriod(id);
  led_pin = (int)getTaskVar(id);
  digitalWrite(led_pin, !digitalRead(led_pin)); // Toggle pin state
}

void toggle_task() {
	switch (state){
		case 0:
			for (uint8_t i=0; i<8; i++){
			  setTaskPeriod(blink_id[i], 100);
			}
		  state++;
			break;
		case 1:
			for (uint8_t i=0; i<8; i++){
			  setTaskPeriod(blink_id[i], (200+i*10));
			}
		  state=0;
			break;
	}
}

void setup() {
	for (uint8_t i=0; i<8; i++){
		pinMode(nP[LEFT][i], OUTPUT);
		digitalWrite(nP[LEFT][i],LOW);
	  blink_id[i] = createTask(blink_task, 200, TASK_ENABLE, &blink_var[i]);
	  setTaskVar(blink_id[i], (void *)nP[LEFT][i]);
	}
}

void loop() {
	toggle_task();
	delay(2000);
}