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"
|
|
|
|
|
2023-06-15 15:36:50 -07:00
|
|
|
ButtonDebounce startStopButtonDebounce("start_button");
|
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*/
|
2021-11-17 00:54:21 -08:00
|
|
|
startStopButtonDebounce.init((engineConfiguration->startCrankingDuration*1000), engineConfiguration->startStopButtonPin, engineConfiguration->startStopButtonMode);
|
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) {
|
2023-06-14 21:06:25 -07:00
|
|
|
engine->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 {
|
|
|
|
if (engine->startStopStateLastPush.hasElapsedSec(engineConfiguration->startCrankingDuration)) {
|
|
|
|
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() {
|
|
|
|
bool startStopState = startStopButtonDebounce.readPinEvent();
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
engine->engineState.startStopState = startStopState;
|
|
|
|
|
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
|
|
|
}
|