Simple Task Scheduler. For the moment only implements continuous running tasks and periodic tasks.
Example
two blinking leds
#include "mbed.h" #include "task_scheduler.h" Serial pc(USBTX, USBRX); // tx, r class Foo { DigitalOut myled1; int counter; public: Foo(void) :myled1(LED1) { counter = 0; } void alive_task(void) { if (counter++ % 30000 == 0) { myled1 = !myled1; } } }; class Bar { DigitalOut myled2; public: Bar(void) :myled2(LED2) {} void control_task(void) { myled2 = !myled2; } }; int main() { Foo foo; Bar bar; SimpleTaskScheduler::TaskScheduler scheduler; scheduler.create_continuous_task(&foo, &Foo::alive_task); scheduler.create_periodic_task(&bar, &Bar::control_task, 1); while(1) { scheduler.update(); } }
task_scheduler.h
- Committer:
- dwini
- Date:
- 2016-07-11
- Revision:
- 0:ba0870ec8714
- Child:
- 2:1d20f2b3c788
File content as of revision 0:ba0870ec8714:
#pragma once #include <vector> #include "task.h" #include "periodic_task.h" // Definition of template methods need to be inside header file. // http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor namespace SimpleTaskScheduler { class TaskScheduler { private: std::vector<Task *> tasks; public: TaskScheduler(void); ~TaskScheduler(void); public: void update(void); public: template<typename T> int create_continuous_task(T* object, void (T::*method)(void)) { Task * task = new Task(object, method); return add_task(task); } template<typename T> int create_periodic_task(T* object, void (T::*method)(void), int period_seconds) { Task * task = new PeriodicTask(object, method, period_seconds); return add_task(task); } private: int add_task(Task * task); }; };