Timers, And Timer Callbacks#

Time Callback#

  • On expiry, a specified function is called

  • Caution: not in schedulable context

  • Cannot wait!

Oneshot Timer#

  • Expires once, at start-time

#include <why.h>
#include <why-timer.h>
#include <print>

int main()
{
    Why::init();

    Why::Timer timer([](){
        std::println("expired");
    });
    timer.start(
        Why::TimeSpec(2, 0),                           // <-- first expiry
        Why::TimeSpec(0, 0)                            // <-- and no more
    );

    Why::pause();

    return 0;
}

Periodic Example#

  • Starts at start-time, and from then on once every period

#include <why.h>
#include <why-timer.h>
#include <print>

int main()
{
    Why::init();

    Why::Timer timer([](){
        std::println("expired");
    });
    timer.start(
        Why::TimeSpec(2, 0),                           // <-- first expiry
        Why::TimeSpec(1, 0)                            // <-- period from then on
    );

    Why::pause();

    return 0;
}