diff --git a/firmware/controllers/trigger/trigger_emulator_algo.cpp b/firmware/controllers/trigger/trigger_emulator_algo.cpp index 33d2e57860..04c53b312f 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.cpp +++ b/firmware/controllers/trigger/trigger_emulator_algo.cpp @@ -15,6 +15,10 @@ */ #include "global.h" +int getPreviousIndex(const int currentIndex, const int size) { + return (currentIndex + size - 1) % size; +} + #if EFI_EMULATE_POSITION_SENSORS || defined(__DOXYGEN__) #include "engine.h" @@ -32,7 +36,7 @@ EXTERN_ENGINE ; void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) { - int prevIndex = (stateIndex + state->phaseCount - 1) % state->phaseCount; + int prevIndex = getPreviousIndex(stateIndex, state->phaseCount); pin_state_t primaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/prevIndex); pin_state_t newPrimaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/stateIndex); @@ -46,17 +50,14 @@ void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIn // todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent? if (primaryWheelState != newPrimaryWheelState) { - primaryWheelState = newPrimaryWheelState; hwHandleShaftSignal(primaryWheelState ? SHAFT_PRIMARY_RISING : SHAFT_PRIMARY_FALLING); } if (secondaryWheelState != newSecondaryWheelState) { - secondaryWheelState = newSecondaryWheelState; hwHandleShaftSignal(secondaryWheelState ? SHAFT_SECONDARY_RISING : SHAFT_SECONDARY_FALLING); } if (thirdWheelState != new3rdWheelState) { - thirdWheelState = new3rdWheelState; hwHandleShaftSignal(thirdWheelState ? SHAFT_3RD_RISING : SHAFT_3RD_FALLING); } diff --git a/firmware/controllers/trigger/trigger_emulator_algo.h b/firmware/controllers/trigger/trigger_emulator_algo.h index cf093c06f1..98c64a9146 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.h +++ b/firmware/controllers/trigger/trigger_emulator_algo.h @@ -19,4 +19,6 @@ public: void initTriggerEmulatorLogic(Logging *sharedLogger DECLARE_ENGINE_PARAMETER_SUFFIX); +int getPreviousIndex(const int currentIndex, const int size); + #endif /* TRIGGER_EMULATOR_ALGO_H_ */ diff --git a/firmware/controllers/trigger/trigger_simulator.cpp b/firmware/controllers/trigger/trigger_simulator.cpp index 231ae1709e..8daa037b0b 100644 --- a/firmware/controllers/trigger/trigger_simulator.cpp +++ b/firmware/controllers/trigger/trigger_simulator.cpp @@ -33,21 +33,21 @@ void TriggerStimulatorHelper::feedSimulatedEvent(TriggerState *state, TriggerSha DECLARE_ENGINE_PARAMETER_SUFFIX) { efiAssertVoid(CUSTOM_ERR_6593, shape->getSize() > 0, "size not zero"); int stateIndex = i % shape->getSize(); - int prevIndex = (stateIndex + shape->getSize() - 1 ) % shape->getSize(); + int prevIndex = getPreviousIndex(stateIndex, shape->getSize()); int loopIndex = i / shape->getSize(); int time = (int) (SIMULATION_CYCLE_PERIOD * (loopIndex + shape->wave.getSwitchTime(stateIndex))); - bool primaryWheelState = shape->wave.getChannelState(0, prevIndex); - bool newPrimaryWheelState = shape->wave.getChannelState(0, stateIndex); + pin_state_t primaryWheelState = shape->wave.getChannelState(0, prevIndex); + pin_state_t newPrimaryWheelState = shape->wave.getChannelState(0, stateIndex); - bool secondaryWheelState = shape->wave.getChannelState(1, prevIndex); - bool newSecondaryWheelState = shape->wave.getChannelState(1, stateIndex); + pin_state_t secondaryWheelState = shape->wave.getChannelState(1, prevIndex); + pin_state_t newSecondaryWheelState = shape->wave.getChannelState(1, stateIndex); - bool thirdWheelState = shape->wave.getChannelState(2, prevIndex); - bool new3rdWheelState = shape->wave.getChannelState(2, stateIndex); + pin_state_t thirdWheelState = shape->wave.getChannelState(2, prevIndex); + pin_state_t new3rdWheelState = shape->wave.getChannelState(2, stateIndex); #if EFI_UNIT_TEST || defined(__DOXYGEN__) if (printTriggerDebug) {