[Wiki]Everything there is to know about LibSchedule

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

6 Likes

This is just fantastic. And I see another new lib in there - LibLog!

3 Likes

I have been using the scheduler to call an method of my Game class and it has been working under the Windows simulator… However when I try top compile it for Pokitto, I receive this error suggesting the method must be static.

Your example above is not static.

C:\\..\\Scopa_Pokitto\\src\\Game_Play.cpp: In member function 'void Game::doPlay_Hand()':
C:\\..\\Scopa_Pokitto\\src\\Game_Play.cpp:181:87: error: invalid use of non-static member function 'void Game::doPlay_Hand_ClearMessage()'
                         Schedule::after<0>(3000, Game::doPlay_Hand_ClearMessage, *this);
                                                                                       ^
C:\\..\\Scopa_Pokitto\\src\\Game_Play.cpp:68:6: note: declared here
 void Game::doPlay_Hand_ClearMessage() {
      ^~~~

And the code (abbreviated):

.h

void doPlay_Hand();
void doPlay_Hand_ClearMessage();

.cpp

void Game::doPlay_Hand_ClearMessage() {
    std::cout << "Do stuff" << std::endl;
}
void Game::doPlay_Hand() {
    if (test) {
       Schedule::after<0>(3000, Game::doPlay_Hand_ClearMessage, *this);
    }
    ....

Does it work if you add an & before the class name?
Schedule::after<0>(3000, &Game::doPlay_Hand_ClearMessage, *this);

1 Like