leOS - a simple taskmanager/scheduler

A couples of days ago I wrote leOS2.
One of the most frequent appointment that I’ve found reading the messages of the users that tried leOS is about the incompatibility between the scheduler and other libraries that use timer 2: this is because a timer can only be used by one application.

So I’ve read about the architecture of the microcontrollers I usually use and I’ve realized to use the WatchDog Timer, WDT. This circuit is usually used to reset the microcontroller if the user doesn’t reset its counter before this one has expired. This is good in critical applications where the sketch must never freeze, both for logical errors or for infinite loops caused by external factors (i.e. datas that should arrive through an ethernet or serial connection). The WatchDog is particular because it uses the internal 128 kHz oscillator as clock source, so it can operate asynchronously respect to the system clock; additionally, it can not only reset the microcontroller but it can also raise an interrupt. We can use the latter and intercept the corresponding ISR, putting in it the leOS’s scheduler: in this manner the scheduler won’t use any timer of the MCU. This is leOS2.

To use leOS2 you just have to copy it into the folder /libraries that is included in your sketchs’ folder, and then add your sketch the inclusion of the library and the creation of a new instance of leOS2.
You are now ready to use leOS2 like its predecessor. Only 2 methods are changed, let’s see them in detail. Due to the fact that leOS2 is based on WDT and that this only has a fixed clock and a few prescalers, there are some limitations on the suitable intervals for a task: the minimum available interval is 16 ms, and the intervals must also be multiplies of this value. So, 16 ms represents a “tick” and the intervals must be passed to addTask() & modifyTask() in ticks and not in milliseconds as in leOS. So, addTask(function, 100) will not add a task scheduled to be run every 100 ms but every 100 ticks, or 1,600 ms. To help converting from milliseconds to ticks (if you don’t want to add a simple bit shift, i.e.: interval>>4) I’vew added a new function called convertMs() that accepts a time in milliseconds and returns the corresponding number of ticks.

As usual, more details can be found on my website:
http://www.leonardomiliani.com/?p=516&lang=en