diff --git a/firmware/controllers/engine_controller_misc.cpp b/firmware/controllers/engine_controller_misc.cpp index 46273037ae..3ab962e7f0 100644 --- a/firmware/controllers/engine_controller_misc.cpp +++ b/firmware/controllers/engine_controller_misc.cpp @@ -7,9 +7,6 @@ #include "pch.h" - -extern ButtonDebounce startStopButtonDebounce; - static uint8_t nextThreadId = 0; void threadInitHook(void* vtp) { // No lock required, this is already under lock @@ -35,53 +32,3 @@ void irqEnterHook() {} void irqExitHook() {} void contextSwitchHook() {} #endif /* ENABLE_PERF_TRACE */ - -static void onStartStopButtonToggle() { - engine->engineState.startStopStateToggleCounter++; - - if (engine->rpmCalculator.isStopped()) { - bool wasStarterEngaged = enginePins.starterControl.getAndSet(1); - if (!wasStarterEngaged) { - engine->startStopStateLastPushTime = getTimeNowNt(); - efiPrintf("Let's crank this engine for up to %d seconds via %s!", - engineConfiguration->startCrankingDuration, - hwPortname(engineConfiguration->starterControlPin)); - } - } else if (engine->rpmCalculator.isRunning()) { - efiPrintf("Let's stop this engine!"); - doScheduleStopEngine(); - } -} - -void slowStartStopButtonCallback() { - bool startStopState = startStopButtonDebounce.readPinEvent(); - - if (startStopState && !engine->engineState.startStopState) { - // we are here on transition from 0 to 1 - onStartStopButtonToggle(); - } - engine->engineState.startStopState = startStopState; - - if (engine->startStopStateLastPushTime == 0) { - // nothing is going on with startStop button - return; - } - - if (engine->rpmCalculator.isRunning()) { - // turn starter off once engine is running - bool wasStarterEngaged = enginePins.starterControl.getAndSet(0); - if (wasStarterEngaged) { - efiPrintf("Engine runs we can disengage the starter"); - engine->startStopStateLastPushTime = 0; - } - } - - // todo: migrate to 'Timer' class - if (getTimeNowNt() - engine->startStopStateLastPushTime > NT_PER_SECOND * engineConfiguration->startCrankingDuration) { - bool wasStarterEngaged = enginePins.starterControl.getAndSet(0); - if (wasStarterEngaged) { - efiPrintf("Cranking timeout %d seconds", engineConfiguration->startCrankingDuration); - engine->startStopStateLastPushTime = 0; - } - } -} diff --git a/firmware/controllers/start_stop.cpp b/firmware/controllers/start_stop.cpp index cc544d5d35..0276c5e826 100644 --- a/firmware/controllers/start_stop.cpp +++ b/firmware/controllers/start_stop.cpp @@ -2,9 +2,59 @@ #include "start_stop.h" -ButtonDebounce startStopButtonDebounce("start_button"); +static ButtonDebounce startStopButtonDebounce("start_button"); void initStartStopButton() { /* startCrankingDuration is efitimesec_t, so we need to multiply it by 1000 to get milliseconds*/ startStopButtonDebounce.init((engineConfiguration->startCrankingDuration*1000), engineConfiguration->startStopButtonPin, engineConfiguration->startStopButtonMode); } + +static void onStartStopButtonToggle() { + engine->engineState.startStopStateToggleCounter++; + + if (engine->rpmCalculator.isStopped()) { + bool wasStarterEngaged = enginePins.starterControl.getAndSet(1); + if (!wasStarterEngaged) { + engine->startStopStateLastPushTime = getTimeNowNt(); + efiPrintf("Let's crank this engine for up to %d seconds via %s!", + engineConfiguration->startCrankingDuration, + hwPortname(engineConfiguration->starterControlPin)); + } + } else if (engine->rpmCalculator.isRunning()) { + efiPrintf("Let's stop this engine!"); + doScheduleStopEngine(); + } +} + +void slowStartStopButtonCallback() { + bool startStopState = startStopButtonDebounce.readPinEvent(); + + if (startStopState && !engine->engineState.startStopState) { + // we are here on transition from 0 to 1 + onStartStopButtonToggle(); + } + engine->engineState.startStopState = startStopState; + + if (engine->startStopStateLastPushTime == 0) { + // nothing is going on with startStop button + return; + } + + if (engine->rpmCalculator.isRunning()) { + // turn starter off once engine is running + bool wasStarterEngaged = enginePins.starterControl.getAndSet(0); + if (wasStarterEngaged) { + efiPrintf("Engine runs we can disengage the starter"); + engine->startStopStateLastPushTime = 0; + } + } + + // todo: migrate to 'Timer' class + if (getTimeNowNt() - engine->startStopStateLastPushTime > NT_PER_SECOND * engineConfiguration->startCrankingDuration) { + bool wasStarterEngaged = enginePins.starterControl.getAndSet(0); + if (wasStarterEngaged) { + efiPrintf("Cranking timeout %d seconds", engineConfiguration->startCrankingDuration); + engine->startStopStateLastPushTime = 0; + } + } +}