2021-07-25 22:05:17 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
2023-06-15 15:36:50 -07:00
|
|
|
/**
|
|
|
|
* See test_start_stop.cpp
|
|
|
|
*/
|
|
|
|
|
2020-09-09 00:22:15 -07:00
|
|
|
#include "start_stop.h"
|
2024-05-08 19:14:54 -07:00
|
|
|
#include "ignition_controller.h"
|
2020-09-09 00:22:15 -07:00
|
|
|
|
2021-11-16 01:15:29 -08:00
|
|
|
void initStartStopButton() {
|
2020-09-20 11:01:00 -07:00
|
|
|
/* startCrankingDuration is efitimesec_t, so we need to multiply it by 1000 to get milliseconds*/
|
2024-05-08 19:14:54 -07:00
|
|
|
engine->startStopState.startStopButtonDebounce.init((engineConfiguration->startCrankingDuration*1000),
|
2024-04-11 21:29:35 -07:00
|
|
|
engineConfiguration->startStopButtonPin,
|
|
|
|
engineConfiguration->startStopButtonMode,
|
|
|
|
engineConfiguration->startRequestPinInverted);
|
2020-09-09 00:22:15 -07:00
|
|
|
}
|
2023-06-14 20:37:51 -07:00
|
|
|
|
|
|
|
static void onStartStopButtonToggle() {
|
|
|
|
engine->engineState.startStopStateToggleCounter++;
|
|
|
|
|
|
|
|
if (engine->rpmCalculator.isStopped()) {
|
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(1);
|
|
|
|
if (!wasStarterEngaged) {
|
2024-05-08 19:14:54 -07:00
|
|
|
engine->startStopState.startStopStateLastPush.reset();
|
2023-06-14 20:37:51 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 21:06:25 -07:00
|
|
|
static void disengageStarterIfNeeded() {
|
|
|
|
if (engine->rpmCalculator.isRunning()) {
|
|
|
|
// turn starter off once engine is now running!
|
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(0);
|
|
|
|
if (wasStarterEngaged) {
|
|
|
|
efiPrintf("Engine runs we can disengage the starter");
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-08 19:14:54 -07:00
|
|
|
if (engine->startStopState.startStopStateLastPush.hasElapsedSec(engineConfiguration->startCrankingDuration)) {
|
2023-06-14 21:06:25 -07:00
|
|
|
bool wasStarterEngaged = enginePins.starterControl.getAndSet(0);
|
|
|
|
if (wasStarterEngaged) {
|
|
|
|
efiPrintf("Cranking timeout %d seconds", engineConfiguration->startCrankingDuration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 20:37:51 -07:00
|
|
|
void slowStartStopButtonCallback() {
|
2024-05-08 19:14:54 -07:00
|
|
|
if (!isIgnVoltage()) {
|
2024-05-08 20:08:40 -07:00
|
|
|
// nothing to crank if we are powered only via USB
|
2024-05-08 19:14:54 -07:00
|
|
|
engine->startStopState.timeSinceIgnitionPower.reset();
|
2024-05-08 19:59:55 -07:00
|
|
|
return;
|
2024-05-08 19:14:54 -07:00
|
|
|
} else if (engine->startStopState.isFirstTime) {
|
2024-05-08 20:08:40 -07:00
|
|
|
// initialize when first time with proper power
|
|
|
|
engine->startStopState.timeSinceIgnitionPower.reset();
|
2024-05-08 19:14:54 -07:00
|
|
|
engine->startStopState.isFirstTime = false;
|
|
|
|
}
|
|
|
|
|
2024-05-08 20:08:40 -07:00
|
|
|
if (engine->startStopState.timeSinceIgnitionPower.getElapsedUs() < MS2US(engineConfiguration->startButtonSuppressOnStartUpMs)) {
|
2023-09-22 10:01:43 -07:00
|
|
|
// where are odd cases of start button combined with ECU power source button we do not want to crank right on start
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-08 19:14:54 -07:00
|
|
|
bool startStopState = engine->startStopState.startStopButtonDebounce.readPinEvent();
|
2023-06-14 20:37:51 -07:00
|
|
|
|
|
|
|
if (startStopState && !engine->engineState.startStopState) {
|
|
|
|
// we are here on transition from 0 to 1
|
2023-06-14 21:06:25 -07:00
|
|
|
// TODO: huh? looks like 'stop engine' feature is broken?! we invoke 'toggle' method under "from off to on" condition?!
|
2023-06-14 20:37:51 -07:00
|
|
|
onStartStopButtonToggle();
|
|
|
|
}
|
2024-05-08 19:14:54 -07:00
|
|
|
// todo: we shall extract start_stop.txt from engine_state.txt
|
2023-06-14 20:37:51 -07:00
|
|
|
engine->engineState.startStopState = startStopState;
|
2024-05-08 19:14:54 -07:00
|
|
|
engine->engineState.startStopPhysicalState = engine->startStopState.startStopButtonDebounce.getPhysicalState();
|
2023-06-14 20:37:51 -07:00
|
|
|
|
2023-06-14 21:06:25 -07:00
|
|
|
bool isStarterEngaged = enginePins.starterControl.getLogicValue();
|
2023-06-14 20:37:51 -07:00
|
|
|
|
2023-06-14 21:06:25 -07:00
|
|
|
if (isStarterEngaged) {
|
|
|
|
disengageStarterIfNeeded();
|
|
|
|
}
|
2023-06-14 20:37:51 -07:00
|
|
|
}
|