diff --git a/firmware/controllers/algo/launch_control.cpp b/firmware/controllers/algo/launch_control.cpp index 15b9b24af4..ebdf33e812 100644 --- a/firmware/controllers/algo/launch_control.cpp +++ b/firmware/controllers/algo/launch_control.cpp @@ -152,14 +152,14 @@ void LaunchControlBase::update() { if (!combinedConditions) { // conditions not met, reset timer - launchTimer = getTimeNowNt(); + m_launchTimer.reset(); engine->isLaunchCondition = false; engine->setLaunchBoostDuty = false; engine->applyLaunchControlRetard = false; engine->applyLaunchExtraFuel = false; } else { // If conditions are met... - if ((getTimeNowNt() - launchTimer > MS2NT(timeDelay * 1000)) && combinedConditions) { + if (m_launchTimer.hasElapsedMs(timeDelay) && combinedConditions) { engine->isLaunchCondition = true; // ...enable launch! engine->applyLaunchExtraFuel = true; } diff --git a/firmware/controllers/algo/launch_control.h b/firmware/controllers/algo/launch_control.h index cfd5c32785..f9d61baacf 100644 --- a/firmware/controllers/algo/launch_control.h +++ b/firmware/controllers/algo/launch_control.h @@ -8,6 +8,7 @@ #pragma once #include "engine_ptr.h" +#include "timer.h" class Logging; void initLaunchControl(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); @@ -29,5 +30,5 @@ public: bool isLaunchConditionMet(int rpm) const; private: - efitick_t launchTimer = 0; + Timer m_launchTimer; }; diff --git a/firmware/util/timer.cpp b/firmware/util/timer.cpp new file mode 100644 index 0000000000..20601bc111 --- /dev/null +++ b/firmware/util/timer.cpp @@ -0,0 +1,27 @@ +#include "timer.h" +#include "global.h" + +void Timer::reset() { + m_lastReset = getTimeNowNt(); +} + +bool Timer::hasElapsedSec(float seconds) const { + return hasElapsedMs(seconds * 1e3); +} + +bool Timer::hasElapsedMs(float milliseconds) const { + return hasElapsedUs(milliseconds * 1e3); +} + +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); +} diff --git a/firmware/util/timer.h b/firmware/util/timer.h new file mode 100644 index 0000000000..fd45e2d3c1 --- /dev/null +++ b/firmware/util/timer.h @@ -0,0 +1,14 @@ +#pragma once + +#include "efitime.h" + +class Timer { +public: + void reset(); + bool hasElapsedSec(float seconds) const; + bool hasElapsedMs(float ms) const; + bool hasElapsedUs(float us) const; + +private: + efitick_t m_lastReset = INT64_MIN; +}; diff --git a/firmware/util/util.mk b/firmware/util/util.mk index 3012b3cd71..ef24d8396b 100644 --- a/firmware/util/util.mk +++ b/firmware/util/util.mk @@ -17,6 +17,7 @@ UTILSRC_CPP = \ $(PROJECT_DIR)/util/loggingcentral.cpp \ $(PROJECT_DIR)/util/cli_registry.cpp \ $(PROJECT_DIR)/util/efilib.cpp \ + $(PROJECT_DIR)/util/timer.cpp \ UTIL_INC = \