reducing code duplication

This commit is contained in:
rusefi 2019-03-04 00:40:22 -05:00
parent 079d1447a9
commit 485b915499
3 changed files with 14 additions and 11 deletions

View File

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

View File

@ -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_ */

View File

@ -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) {