#include "virtual_timer/VirtualTimerDistributor.h"
#include "virtual_timer/VirtualTimer.h"

using namespace pinetime::virtual_timer;

VirtualTimer::VirtualTimer(uint64_t period_ms)
    : period(period_ms)
{
}

void VirtualTimer::timer_enable()
{
    VirtualTimerDistributor & distri = VirtualTimerDistributor::instance();
    this->start_time = distri.tick();
    distri.register_timer(this);
}

void VirtualTimer::timer_disable()
{
    VirtualTimerDistributor::instance().unregister_timer(this);
}

void VirtualTimer::timer_notification(uint64_t tick_ms)
{
    if((tick_ms - this->start_time) >= (this->period)) {
        this->notification();
    }
}

void VirtualTimer::timer_set_period_ms(uint64_t period_ms)
{
    this->period = period_ms;
}

// void VirtualTimer::notify()
// {
//     asm volatile("nop");
// }