Download the file
  1. #ifndef Scheduler_h
  2. #define Scheduler_h
  3.  
  4. #if ARDUINO>=100
  5. #include <Arduino.h> // Arduino 1.0
  6. #else
  7. #include <Wprogram.h> // Arduino 0022
  8. #endif
  9.  
  10. class Task {
  11. public:
  12. virtual bool canRun(uint32_t now) = 0;
  13. virtual void run(uint32_t now) = 0;
  14. };
  15.  
  16. class TimedTask : public Task {
  17. public:
  18. inline TimedTask(uint32_t when) { runTime = when; }
  19. virtual bool canRun(uint32_t now);
  20. inline void setRunTime(uint32_t when) { runTime = when; }
  21. inline void incRunTime(uint32_t inc) { runTime += inc; }
  22. inline uint32_t getRunTime() { return runTime; }
  23. protected:
  24. uint32_t runTime;
  25. };
  26.  
  27. class TaskScheduler {
  28. public:
  29. TaskScheduler(Task **task, uint8_t numTasks);
  30. void run();
  31. private:
  32. Task **tasks;
  33. int numTasks;
  34. };
  35.  
  36. #endif
  37.  
  38. /*
  39. class FooTask: public TimedTask {
  40. public:
  41.   FooTask(): TimedTask(millis()) {};
  42.   void run(uint32_t now) { incRunTime(10); }
  43. };
  44.  
  45. FooTask foo;
  46. Task* tasks[] = { &foo };
  47. TaskScheduler scheduler(tasks, 1);
  48.  
  49. void setup() {
  50.   scheduler.run();
  51. }
  52.  
  53. void loop() {
  54. }
  55. */
  56.  
  57. // vim:ft=arduino
  58.