refactoring

This commit is contained in:
rusefi 2018-01-21 15:31:46 -05:00
parent 0112bd8237
commit cdc5ffa72f
1 changed files with 13 additions and 17 deletions

View File

@ -109,36 +109,32 @@ void PwmConfig::handleCycleStart() {
} }
} }
efitimeus_t PwmConfig::togglePwmState() {
return 0;
}
/** /**
* @return Next time for signal toggle * @return Next time for signal toggle
*/ */
static efitimeus_t togglePwmState(PwmConfig *state) { efitimeus_t PwmConfig::togglePwmState() {
#if DEBUG_PWM #if DEBUG_PWM
scheduleMsg(&logger, "togglePwmState phaseIndex=%d iteration=%d", state->safe.phaseIndex, state->safe.iteration); scheduleMsg(&logger, "togglePwmState phaseIndex=%d iteration=%d", safe.phaseIndex, safe.iteration);
scheduleMsg(&logger, "state->period=%f state->safe.period=%f", state->period, state->safe.period); scheduleMsg(&logger, "period=%f safe.period=%f", period, safe.period);
#endif #endif
if (cisnan(state->periodNt)) { if (cisnan(periodNt)) {
/** /**
* NaN period means PWM is paused * NaN period means PWM is paused
*/ */
return getTimeNowUs() + MS2US(100); return getTimeNowUs() + MS2US(100);
} }
state->handleCycleStart(); handleCycleStart();
/** /**
* Here is where the 'business logic' - the actual pin state change is happening * Here is where the 'business logic' - the actual pin state change is happening
*/ */
// callback state index is offset by one. todo: why? can we simplify this? // callback state index is offset by one. todo: why? can we simplify this?
int cbStateIndex = state->safe.phaseIndex == 0 ? state->phaseCount - 1 : state->safe.phaseIndex - 1; int cbStateIndex = safe.phaseIndex == 0 ? phaseCount - 1 : safe.phaseIndex - 1;
state->stateChangeCallback(state, cbStateIndex); stateChangeCallback(this, cbStateIndex);
efitimeus_t nextSwitchTimeUs = getNextSwitchTimeUs(state); efitimeus_t nextSwitchTimeUs = getNextSwitchTimeUs(this);
#if DEBUG_PWM #if DEBUG_PWM
scheduleMsg(&logger, "%s: nextSwitchTime %d", state->name, nextSwitchTime); scheduleMsg(&logger, "%s: nextSwitchTime %d", state->name, nextSwitchTime);
#endif /* DEBUG_PWM */ #endif /* DEBUG_PWM */
@ -157,10 +153,10 @@ static efitimeus_t togglePwmState(PwmConfig *state) {
// timeToSwitch = 10; // timeToSwitch = 10;
// } // }
state->safe.phaseIndex++; safe.phaseIndex++;
if (state->safe.phaseIndex == state->phaseCount) { if (safe.phaseIndex == phaseCount) {
state->safe.phaseIndex = 0; // restart safe.phaseIndex = 0; // restart
state->safe.iteration++; safe.iteration++;
} }
return nextSwitchTimeUs; return nextSwitchTimeUs;
} }
@ -172,7 +168,7 @@ static void timerCallback(PwmConfig *state) {
state->dbgNestingLevel++; state->dbgNestingLevel++;
efiAssertVoid(state->dbgNestingLevel < 25, "PWM nesting issue"); efiAssertVoid(state->dbgNestingLevel < 25, "PWM nesting issue");
efitimeus_t switchTimeUs = togglePwmState(state); efitimeus_t switchTimeUs = state->togglePwmState();
scheduleByTime(&state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state); scheduleByTime(&state->scheduling, switchTimeUs, (schfunc_t) timerCallback, state);
state->dbgNestingLevel--; state->dbgNestingLevel--;
} }