Skip to content

Instantly share code, notes, and snippets.

@mianos
Created March 29, 2018 10:54
Show Gist options
  • Save mianos/51458a8113f4b03a40cb7aab7ec5430b to your computer and use it in GitHub Desktop.
Save mianos/51458a8113f4b03a40cb7aab7ec5430b to your computer and use it in GitHub Desktop.
Class that creates and dispatches tasks.
#include <Arduino.h>
#define _TASK_LTS_POINTER
#include <TaskScheduler.h>
class STT {
static Scheduler *gts;
Task *test_task;
int counter;
public:
STT(Scheduler *ts) : counter(0) {
gts = ts;
}
void init() {
test_task = new Task(TASK_SECOND, TASK_FOREVER, &stt_task, gts, true);
test_task->setLtsPointer(this);
}
static void stt_task() {
Task& this_task = gts->currentTask();
STT *self = (STT *)this_task.getLtsPointer();
self->counter++;
}
void print() {
Serial.printf("Counter %d\n", counter);
}
};
Scheduler *STT::gts;
// end of header
Scheduler ts;
void display() {
Task& this_task = ts.currentTask();
STT *anstt = (STT *)this_task.getLtsPointer();
anstt->print();
}
void setup() {
Serial.begin(115200);
STT *anstt = new STT(&ts);
anstt->init();
Task *tt = new Task(100, TASK_FOREVER, &display, &ts, true);
tt->setLtsPointer(anstt);
}
void loop() {
ts.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment