What is it?
Ever come across this thing that had to be done, but it didn’t have to be right now?
Schedule it for later using LibSchedule!
Using software timers (it doesn’t use IRQs or any of the MCU’s timers) you can call a function after X milliseconds or every X milliseconds, similar to Javascript’s setTimeout
and setInterval
functions.
How do I get started?
- Update your PokittoLib
#include <LibSchedule>
- Schedule something!
#include <Pokitto.h>
#include <LibLog>
#include <LibSchedule>
void test(){
LOG("test\n");
}
// Stand-alone functions can receive an argument
void test2(int arg){
LOG("test2 ", arg, "\n");
}
void test3(){
LOG("test3\n");
}
// A function given to `Schedule::repeat` can
// cancel its own timer by returning `false`
int callCount = 0;
bool test4(){
callCount++;
LOG("test4 ", callCount, "\n");
return callCount < 10; // stop once callCount reaches 10
}
class Test {
public:
void test(){
LOG("Class Test\n");
}
};
void init(){
// call test after 1 second using timer 0
Schedule::after<0>(1000, test);
// call test2 after 2 seconds using timer 1
Schedule::after<1>(2000, test2, 5);
// call test3 every second using timer 2
Schedule::repeat<2>(1000, test3);
// wait 10 seconds using timer 3, then cancel timer 2
Schedule::after<3>(10'000, [](){ Schedule::cancel<2>(); });
// call test4 until it returns false
Schedule::repeat<4>(100, test4);
// call a class method after half a second
Schedule::after<5>(500, &Test::test, test);
}