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();
    }
}

tasks/task.cpp

Committer:
dwini
Date:
2016-12-13
Revision:
3:bd96023168df
Parent:
1:4eccf61f4ef7

File content as of revision 3:bd96023168df:

#include "task.h"

namespace SimpleTaskScheduler {

  unsigned int Task::get_id(void) { return this->id; }
  void Task::call(void) { this->callback.call(); }

  unsigned int Task::generate_id(void) {
    static int numberOfInstances = 0;
    return numberOfInstances++;
  }

  void Task::stop(void) {
    nextState = STOPPED;
  }

  void Task::start(void) {
    nextState = WAITING;
  }
};