GetTaskVar

From chipKIT
Jump to: navigation, search

Synopsis

 void * getTaskVar(int id)

Parameter

task  : Pointer to the task function

Description

This function returns the current value of the task user variable. Note that the return value is typed to be a void pointer. This means that the return value must be cast to some specific type and stored into a variable of that type in order to be used.

Return Value

The current value of the task variable

Conforming To

The Task Management Functions are specific to ChipKit and exceed the Arduino 1.6.x specification.

Example

 /*
  Blink Task (tested in UECIDE version 0.8.7)
  Utlizing the chipKIT task manager rather than delay functions.
  The use of the delay() function in loop() is done on purpose to demonstrate that the delay() call is not blocking the task
  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   }
 }; 
 
 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);
 }

See Also

createTask(), destroyTask(), getTaskId(), getTaskNextExec(), getTaskPeriod(), getTaskState(), getTaskVar(), setTaskPeriod(), setTaskState(), setTaskVar(), startTaskAt(),