Loading files
#include <Arduino.h>
#include "Utils/MemoryHelper.h"
#include "Utils/Timer.h"
namespace Espfc::Utils {
int Timer::setInterval(uint32_t interval)
{
this->interval = interval;
this->rate = (1000000UL + interval / 2) / interval;
this->denom = 1;
this->delta = this->interval;
this->intervalf = this->interval * 0.000001f;
this->realRate = (float)this->rate;
iteration = 0;
return 1;
}
int Timer::setRate(uint32_t rate, uint32_t denom)
{
this->rate = (rate + denom / 2) / denom;
this->interval = (1000000UL + this->rate / 2) / this->rate;
this->denom = denom;
this->delta = this->interval;
this->intervalf = this->interval * 0.000001f;
this->realRate = (float)this->rate;
iteration = 0;
return 1;
}
bool FAST_CODE_ATTR Timer::check()
{
return check(micros());
}
int FAST_CODE_ATTR Timer::update()
{
return update(micros());
}
bool FAST_CODE_ATTR Timer::check(uint32_t now)
{
if (interval == 0) return false;
if (now < next) return false;
return update(now);
}
int FAST_CODE_ATTR Timer::update(uint32_t now)
{
next = now + interval;
delta = now - last;
last = now;
iteration++;
realRate += 0.1f * ((1000000.0f / delta) - realRate);
return 1;
}
bool FAST_CODE_ATTR Timer::syncTo(const Timer& t, uint32_t slot)
{
if (denom > 0)
{
if (slot > denom - 1) slot = denom - 1;
if (t.iteration % denom != slot) return false;
return update(micros());
}
return check(micros());
}
} // namespace Espfc::Utils
ec66650c4This file is pinned to the selected release. Download the original to open it in your design tool.