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(); } }
Diff: task_scheduler.cpp
- Revision:
- 0:ba0870ec8714
- Child:
- 3:bd96023168df
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task_scheduler.cpp Mon Jul 11 14:58:22 2016 +0000 @@ -0,0 +1,23 @@ +#include "task_scheduler.h" + +namespace SimpleTaskScheduler { + + TaskScheduler::TaskScheduler(void) { } + + TaskScheduler::~TaskScheduler(void) { + for (unsigned int i = 0; i < tasks.size(); i++) { + delete tasks[i]; + } + } + + void TaskScheduler::update(void) { + for (unsigned int i = 0; i < tasks.size(); i++) { + tasks[i]->run(); + } + } + + int TaskScheduler::add_task(Task * task) { + tasks.push_back(task); + return task->get_id(); + } +};