only:better code placement, zero logic changes

This commit is contained in:
rusefillc 2023-06-14 23:37:51 -04:00
parent 67b1df1c5e
commit a9af65a0a8
2 changed files with 51 additions and 54 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}