Arduino-Scheduler
Portable Collaborative Multi-Tasking Scheduler for Arduino
Scheduler.h
Go to the documentation of this file.
1 
19 #ifndef SCHEDULER_H
20 #define SCHEDULER_H
21 
22 #include <setjmp.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 
27 public:
31  typedef void (*func_t)();
32 
41  static bool begin(size_t stackSize);
42 
54  static bool start(func_t taskSetup, func_t taskLoop,
55  size_t stackSize = DEFAULT_STACK_SIZE);
56 
65  static bool startLoop(func_t taskLoop,
66  size_t stackSize = DEFAULT_STACK_SIZE)
67  {
68  return (start(NULL, taskLoop, stackSize));
69  }
70 
74  static void yield();
75 
82  static size_t stack();
83 
84 protected:
93  static void init(func_t setup, func_t loop, const uint8_t* stack);
94 
98  struct task_t {
101  jmp_buf context;
102  const uint8_t* stack;
103  };
104 
105 #if defined(TEENSYDUINO)
106 
107  static const size_t DEFAULT_STACK_SIZE = 1024;
108  static const size_t DEFAULT_MAIN_STACK_SIZE = 2048;
109 #if defined(__MK20DX256__)
110 
111  static const size_t STACK_MAX = 16384;
112 #elif defined(__MK64FX512__)
113 
114  static const size_t STACK_MAX = 49152;
115 #elif defined(__MK66FX1M0__)
116 
117  static const size_t STACK_MAX = 65536;
118 #else
119 #error "Scheduler.h: Teensy board not supported"
120 #endif
121 
122 #elif defined(ARDUINO_ARCH_AVR)
123 
124  static const size_t DEFAULT_STACK_SIZE = 128;
125  static const size_t DEFAULT_MAIN_STACK_SIZE = 256;
126 
127 #elif defined(ARDUINO_ARCH_SAM)
128 
129  static const size_t DEFAULT_STACK_SIZE = 1024;
130  static const size_t DEFAULT_MAIN_STACK_SIZE = 2048;
131  static const size_t STACK_MAX = 32768;
132 
133 #elif defined(ARDUINO_ARCH_SAMD)
134 
135  static const size_t DEFAULT_STACK_SIZE = 1024;
136  static const size_t DEFAULT_MAIN_STACK_SIZE = 2048;
137  static const size_t STACK_MAX = 16384;
138 
139 #else
140 #error "Scheduler.h: Arduino board not supported"
141 #endif
142 
144  static task_t s_main;
145 
147  static task_t* s_running;
148 
150  static size_t s_top;
151 };
152 
155 
162 #define await(cond) while (!(cond)) Scheduler.yield()
163 
164 #endif
static size_t s_top
Definition: Scheduler.h:150
void loop()
Definition: Alarm.h:35
static task_t * s_running
Definition: Scheduler.h:147
static bool startLoop(func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.h:65
const uint8_t * stack
Task stack top.
Definition: Scheduler.h:102
void(* func_t)()
Definition: Scheduler.h:31
void setup()
Definition: Alarm.h:31
static task_t s_main
Definition: Scheduler.h:144
SchedulerClass Scheduler
Definition: Scheduler.cpp:53
task_t * next
Next task.
Definition: Scheduler.h:99
static bool begin(size_t stackSize)
Definition: Scheduler.cpp:69
task_t * prev
Previous task.
Definition: Scheduler.h:100
static size_t stack()
Definition: Scheduler.cpp:127
static void yield()
Definition: Scheduler.cpp:117
static void init(func_t setup, func_t loop, const uint8_t *stack)
Definition: Scheduler.cpp:135
jmp_buf context
Task context.
Definition: Scheduler.h:101
static bool start(func_t taskSetup, func_t taskLoop, size_t stackSize=DEFAULT_STACK_SIZE)
Definition: Scheduler.cpp:76