arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.32k stars 243 forks source link

Tasks callbacks with inputs fail. #139

Closed dr-mrsthemonarch closed 2 years ago

dr-mrsthemonarch commented 2 years ago

With this minimal Code:

#include <array>
#include <Arduino.h>

std::array<std::array<float, 3> , 2> vals = {0}; //imagine this is full of sensor data
String stringRead;
int cs14 = 1;
#define _TASK_SCHEDULING_OPTIONS
#include <TaskScheduler.h>

void cs14Print(int pin );
Scheduler runner;
//Tasks
Task sensor14(1000, TASK_FOREVER, &cs14Print(int pin));

void cs14Print(int pin) {
  Serial.print(vals[pin][0]);
  Serial.print(',');
  Serial.print(vals[pin][1]);
  Serial.print(',');
  Serial.println(vals[pin][2]);
};

void setup() {
  // serial to display data
  Serial.begin(115200);
  //  delay(100);
  runner.init();
  runner.addTask(sensor14);
  sensor14.enable();
}

void loop() {
  runner.execute();
}

I'm trying to create tasks that I can feed various values, rather than repeating code over and over again for various tasks. I have developed a CLI that can take different Integers to turn on or off different sensors through using SPI and chip select (some 16 differents sensors)

Though it seems this isn't possible, as I get the following error, or various different ones when trying to start a task callback with variables.

taskwithoptions:15:46: error: expected primary-expression before 'int'
 Task sensor14(1000, TASK_FOREVER, &cs14Print(int pin));
                                              ^~~
exit status 1
expected primary-expression before 'int'

nor can I find in any of your (excellent) examples of this being done either. Is this not possible? Or is there a better way around this? Love using the library otherwise!

arkhipenko commented 2 years ago

You cannot pass parameters to the callback methods. You have to use LTS for that - check appropriate examples. 

https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example08_LTS

#define _TASK_LTS_POINTER

…will compile TaskScheduler with support for Local Task Storage pointer (LTS). LTS is a generic (void*) pointer that could be set to reference a variable or a structure specific to a particular task. A callback method can get access to specific variables by getting a reference to a currently running task from the scheduler, and then casting (void*) LTS pointer to the appropriate pointer type.

NOTE: above parameters are DISABLED by default, and need to be explicitly enabled by placing appropriate #define statements in front of the #include statement for the TaskScheduler header file.