2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2020-11-30 16:35:06 -08:00
|
|
|
#include "timer.h"
|
|
|
|
|
2022-05-09 01:04:38 -07:00
|
|
|
Timer::Timer() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:35:06 -08:00
|
|
|
void Timer::reset() {
|
|
|
|
m_lastReset = getTimeNowNt();
|
|
|
|
}
|
|
|
|
|
2022-05-09 01:04:38 -07:00
|
|
|
void Timer::init() {
|
|
|
|
m_lastReset = INT64_MIN / 8;
|
|
|
|
}
|
|
|
|
|
2020-12-03 08:13:45 -08:00
|
|
|
void Timer::reset(efitick_t nowNt) {
|
|
|
|
m_lastReset = nowNt;
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:35:06 -08:00
|
|
|
bool Timer::hasElapsedSec(float seconds) const {
|
2021-11-15 16:34:42 -08:00
|
|
|
return hasElapsedMs(seconds * 1000);
|
2020-11-30 16:35:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::hasElapsedMs(float milliseconds) const {
|
2021-11-15 16:34:42 -08:00
|
|
|
return hasElapsedUs(milliseconds * 1000);
|
2020-11-30 16:35:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::hasElapsedUs(float microseconds) const {
|
|
|
|
auto delta = getTimeNowNt() - m_lastReset;
|
|
|
|
|
|
|
|
// If larger than 32 bits, timer has certainly expired
|
|
|
|
if (delta >= UINT32_MAX) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto delta32 = (uint32_t)delta;
|
|
|
|
|
|
|
|
return delta32 > USF2NT(microseconds);
|
|
|
|
}
|
2020-12-03 08:13:45 -08:00
|
|
|
|
|
|
|
float Timer::getElapsedSeconds() const {
|
2020-12-06 13:01:45 -08:00
|
|
|
return getElapsedSeconds(getTimeNowNt());
|
|
|
|
}
|
|
|
|
|
|
|
|
float Timer::getElapsedSeconds(efitick_t nowNt) const {
|
2021-07-28 11:14:51 -07:00
|
|
|
return 1 / US_PER_SECOND_F * getElapsedUs(nowNt);
|
2021-03-07 12:32:41 -08:00
|
|
|
}
|
|
|
|
|
2021-11-14 11:53:44 -08:00
|
|
|
float Timer::getElapsedUs() const {
|
|
|
|
return getElapsedUs(getTimeNowNt());
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:32:41 -08:00
|
|
|
float Timer::getElapsedUs(efitick_t nowNt) const {
|
2021-11-14 11:53:44 -08:00
|
|
|
auto deltaNt = nowNt - m_lastReset;
|
2020-12-03 08:13:45 -08:00
|
|
|
|
2020-12-21 03:16:58 -08:00
|
|
|
// Yes, things can happen slightly in the future if we get a lucky interrupt between
|
|
|
|
// the timestamp and this subtraction, that updates m_lastReset to what's now "the future",
|
|
|
|
// resulting in a negative delta.
|
2021-11-14 11:53:44 -08:00
|
|
|
if (deltaNt < 0) {
|
2020-12-21 03:16:58 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-14 11:53:44 -08:00
|
|
|
if (deltaNt > UINT32_MAX - 1) {
|
|
|
|
deltaNt = UINT32_MAX - 1;
|
2020-12-03 08:13:45 -08:00
|
|
|
}
|
|
|
|
|
2021-11-14 11:53:44 -08:00
|
|
|
auto delta32 = (uint32_t)deltaNt;
|
2020-12-03 08:13:45 -08:00
|
|
|
|
2021-03-07 12:32:41 -08:00
|
|
|
return NT2US(delta32);
|
2020-12-06 13:01:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Timer::getElapsedSecondsAndReset(efitick_t nowNt) {
|
|
|
|
float result = getElapsedSeconds(nowNt);
|
|
|
|
|
|
|
|
reset(nowNt);
|
|
|
|
|
|
|
|
return result;
|
2020-12-03 08:13:45 -08:00
|
|
|
}
|