diff --git a/firmware/controllers/alternatorController.cpp b/firmware/controllers/alternatorController.cpp index 7068c94366..44364da78c 100644 --- a/firmware/controllers/alternatorController.cpp +++ b/firmware/controllers/alternatorController.cpp @@ -128,7 +128,7 @@ static void applyAlternatorPinState(PwmConfig *state, int stateIndex) { efiAssertVoid(CUSTOM_ERR_6643, stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex"); efiAssertVoid(CUSTOM_IDLE_WAVE_CNT, state->multiWave.waveCount == 1, "invalid idle waveCount"); OutputPin *output = state->outputPins[0]; - int value = state->multiWave.waves[0].getState(stateIndex); + int value = state->multiWave.getChannelState(/*channelIndex*/0, stateIndex); /** * 'engine->isAlternatorControlEnabled' would be false is RPM is too low */ diff --git a/firmware/controllers/core/EfiWave.cpp b/firmware/controllers/core/EfiWave.cpp index e53fbfdf56..5943fdcb54 100644 --- a/firmware/controllers/core/EfiWave.cpp +++ b/firmware/controllers/core/EfiWave.cpp @@ -30,7 +30,7 @@ void SingleWave::setState(int index, int state) { } void MultiWave::baseConstructor() { - waves = NULL; + channels = NULL; switchTimes = NULL; reset(); } @@ -44,9 +44,9 @@ MultiWave::MultiWave(float *switchTimes, SingleWave *waves) { init(switchTimes, waves); } -void MultiWave::init(float *switchTimes, SingleWave *waves) { +void MultiWave::init(float *switchTimes, SingleWave *channels) { this->switchTimes = switchTimes; - this->waves = waves; + this->channels = channels; } void MultiWave::reset(void) { @@ -70,7 +70,7 @@ void MultiWave::checkSwitchTimes(int size) { } int MultiWave::getChannelState(int channelIndex, int phaseIndex) const { - return waves[channelIndex].pinStates[phaseIndex]; + return channels[channelIndex].pinStates[phaseIndex]; } /** diff --git a/firmware/controllers/core/EfiWave.h b/firmware/controllers/core/EfiWave.h index 662d3bfaff..ef2d2a30ea 100644 --- a/firmware/controllers/core/EfiWave.h +++ b/firmware/controllers/core/EfiWave.h @@ -66,10 +66,10 @@ public: int findInsertionAngle(float angle, int size) const; /** - * Number of signal wires + * Number of signal channels */ int waveCount; - SingleWave *waves; + SingleWave *channels; //private: /** * values in the (0..1] range which refer to points within the period at at which pin state should be changed diff --git a/firmware/controllers/idle_thread.cpp b/firmware/controllers/idle_thread.cpp index 759afdcdb6..14b549ac3f 100644 --- a/firmware/controllers/idle_thread.cpp +++ b/firmware/controllers/idle_thread.cpp @@ -446,7 +446,7 @@ static void applyIdleSolenoidPinState(PwmConfig *state, int stateIndex) { efiAssertVoid(CUSTOM_ERR_6645, stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex"); efiAssertVoid(CUSTOM_ERR_6646, state->multiWave.waveCount == 1, "invalid idle waveCount"); OutputPin *output = state->outputPins[0]; - int value = state->multiWave.waves[0].pinStates[stateIndex]; + int value = state->multiWave.getChannelState(/*channelIndex*/0, stateIndex); if (!value /* always allow turning solenoid off */ || (GET_RPM() != 0 || timeToStopIdleTest != 0) /* do not run solenoid unless engine is spinning or bench testing in progress */ ) { diff --git a/firmware/controllers/system/pwm_generator_logic.cpp b/firmware/controllers/system/pwm_generator_logic.cpp index 7e9b30947c..c2d58e8e7e 100644 --- a/firmware/controllers/system/pwm_generator_logic.cpp +++ b/firmware/controllers/system/pwm_generator_logic.cpp @@ -230,10 +230,11 @@ void copyPwmParameters(PwmConfig *state, int phaseCount, float *switchTimes, int for (int phaseIndex = 0; phaseIndex < phaseCount; phaseIndex++) { state->multiWave.setSwitchTime(phaseIndex, switchTimes[phaseIndex]); - for (int waveIndex = 0; waveIndex < waveCount; waveIndex++) { -// print("output switch time index (%d/%d) at %.2f to %d\r\n", phaseIndex,waveIndex, + for (int channelIndex = 0; channelIndex < waveCount; channelIndex++) { +// print("output switch time index (%d/%d) at %.2f to %d\r\n", phaseIndex, channelIndex, // switchTimes[phaseIndex], pinStates[waveIndex][phaseIndex]); - state->multiWave.waves[waveIndex].pinStates[phaseIndex] = pinStates[waveIndex][phaseIndex]; + int value = pinStates[channelIndex][phaseIndex]; + state->multiWave.channels[channelIndex].setState(phaseIndex, value); } } if (state->mode == PM_NORMAL) { @@ -310,9 +311,9 @@ void startSimplePwmExt(SimplePwm *state, const char *msg, brain_pin_e brainPin, void applyPinState(PwmConfig *state, int stateIndex) { efiAssertVoid(CUSTOM_ERR_6663, stateIndex < PWM_PHASE_MAX_COUNT, "invalid stateIndex"); efiAssertVoid(CUSTOM_ERR_6664, state->multiWave.waveCount <= PWM_PHASE_MAX_WAVE_PER_PWM, "invalid waveCount"); - for (int waveIndex = 0; waveIndex < state->multiWave.waveCount; waveIndex++) { - OutputPin *output = state->outputPins[waveIndex]; - int value = state->multiWave.waves[waveIndex].pinStates[stateIndex]; + for (int channelIndex = 0; channelIndex < state->multiWave.waveCount; channelIndex++) { + OutputPin *output = state->outputPins[channelIndex]; + int value = state->multiWave.getChannelState(channelIndex, stateIndex); output->setValue(value); } } diff --git a/firmware/controllers/trigger/decoders/trigger_honda.cpp b/firmware/controllers/trigger/decoders/trigger_honda.cpp index b8fefe9159..0132ffa5b7 100644 --- a/firmware/controllers/trigger/decoders/trigger_honda.cpp +++ b/firmware/controllers/trigger/decoders/trigger_honda.cpp @@ -10,10 +10,10 @@ #define S24 (720.0f / 24 / 2) -static float addAccordPair(TriggerShape *s, float sb, trigger_wheel_e const waveIndex DECLARE_ENGINE_PARAMETER_SUFFIX) { - s->addEvent2(sb, waveIndex, TV_RISE PASS_ENGINE_PARAMETER_SUFFIX); +static float addAccordPair(TriggerShape *s, float sb, trigger_wheel_e const channelIndex DECLARE_ENGINE_PARAMETER_SUFFIX) { + s->addEvent2(sb, channelIndex, TV_RISE PASS_ENGINE_PARAMETER_SUFFIX); sb += S24; - s->addEvent2(sb, waveIndex, TV_FALL PASS_ENGINE_PARAMETER_SUFFIX); + s->addEvent2(sb, channelIndex, TV_FALL PASS_ENGINE_PARAMETER_SUFFIX); sb += S24; return sb; diff --git a/firmware/controllers/trigger/trigger_emulator_algo.cpp b/firmware/controllers/trigger/trigger_emulator_algo.cpp index 38beb80d04..827b099d28 100644 --- a/firmware/controllers/trigger/trigger_emulator_algo.cpp +++ b/firmware/controllers/trigger/trigger_emulator_algo.cpp @@ -36,14 +36,14 @@ EXTERN_ENGINE void TriggerEmulatorHelper::handleEmulatorCallback(PwmConfig *state, int stateIndex) { int prevIndex = (stateIndex + state->phaseCount - 1) % state->phaseCount; - bool primaryWheelState = state->multiWave.waves[0].pinStates[prevIndex]; - int newPrimaryWheelState = state->multiWave.waves[0].pinStates[stateIndex]; + bool primaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/prevIndex); + int newPrimaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 0, /*phaseIndex*/stateIndex); - bool secondaryWheelState = state->multiWave.waves[1].pinStates[prevIndex]; - int newSecondaryWheelState = state->multiWave.waves[1].pinStates[stateIndex]; + bool secondaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 1, /*phaseIndex*/prevIndex); + int newSecondaryWheelState = state->multiWave.getChannelState(/* channelIndex*/ 1, /*phaseIndex*/stateIndex); - bool thirdWheelState = state->multiWave.waves[2].pinStates[prevIndex]; - int new3rdWheelState = state->multiWave.waves[2].pinStates[stateIndex]; + bool thirdWheelState = state->multiWave.getChannelState(/* channelIndex*/ 2, /*phaseIndex*/prevIndex); + int new3rdWheelState = state->multiWave.getChannelState(/* channelIndex*/ 2, /*phaseIndex*/stateIndex); // todo: code duplication with TriggerStimulatorHelper::feedSimulatedEvent? @@ -122,8 +122,10 @@ static void updateTriggerShapeIfNeeded(PwmConfig *state) { TriggerShape *s = &engine->triggerCentral.triggerShape; - pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, - s->wave.waves[2].pinStates }; + pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { + s->wave.channels[0].pinStates, + s->wave.channels[1].pinStates, + s->wave.channels[2].pinStates }; copyPwmParameters(state, s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, pinStates); state->safe.periodNt = -1; // this would cause loop re-initialization } @@ -163,8 +165,10 @@ void initTriggerEmulatorLogic(Logging *sharedLogger) { TriggerShape *s = &engine->triggerCentral.triggerShape; setTriggerEmulatorRPM(engineConfiguration->bc.triggerSimulatorFrequency PASS_ENGINE_PARAMETER_SUFFIX); - pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { s->wave.waves[0].pinStates, s->wave.waves[1].pinStates, - s->wave.waves[2].pinStates }; + pin_state_t *pinStates[PWM_PHASE_MAX_WAVE_PER_PWM] = { + s->wave.channels[0].pinStates, + s->wave.channels[1].pinStates, + s->wave.channels[2].pinStates }; triggerSignal.weComplexInit("position sensor", s->getSize(), s->wave.switchTimes, PWM_PHASE_MAX_WAVE_PER_PWM, pinStates, updateTriggerShapeIfNeeded, emulatorApplyPinState); diff --git a/firmware/controllers/trigger/trigger_structure.cpp b/firmware/controllers/trigger/trigger_structure.cpp index fb9c154c5a..1c9a535e76 100644 --- a/firmware/controllers/trigger/trigger_structure.cpp +++ b/firmware/controllers/trigger/trigger_structure.cpp @@ -32,8 +32,8 @@ EXTERN_ENGINE; trigger_shape_helper::trigger_shape_helper() { memset(&pinStates, 0, sizeof(pinStates)); - for (int i = 0; i < TRIGGER_CHANNEL_COUNT; i++) { - waves[i].init(pinStates[i]); + for (int channelIndex = 0; channelIndex < TRIGGER_CHANNEL_COUNT; channelIndex++) { + channels[channelIndex].init(pinStates[channelIndex]); } } @@ -41,7 +41,7 @@ TriggerShape::TriggerShape() : wave(switchTimesBuffer, NULL) { version = 0; initialize(OM_NONE, false); - wave.waves = h.waves; + wave.channels = h.channels; memset(triggerIndexByAngle, 0, sizeof(triggerIndexByAngle)); } @@ -280,9 +280,9 @@ angle_t TriggerShape::getAngle(int index) const { return getCycleDuration() * crankCycle + getSwitchAngle(remainder); } -void TriggerShape::addEvent3(angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const stateParam, float filterLeft, float filterRight DECLARE_ENGINE_PARAMETER_SUFFIX) { +void TriggerShape::addEvent3(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const stateParam, float filterLeft, float filterRight DECLARE_ENGINE_PARAMETER_SUFFIX) { if (angle > filterLeft && angle < filterRight) - addEvent2(angle, waveIndex, stateParam PASS_ENGINE_PARAMETER_SUFFIX); + addEvent2(angle, channelIndex, stateParam PASS_ENGINE_PARAMETER_SUFFIX); } operation_mode_e TriggerShape::getOperationMode() { @@ -296,7 +296,7 @@ extern bool printTriggerDebug; void TriggerShape::calculateExpectedEventCounts(bool useOnlyRisingEdgeForTrigger) { // todo: move the following logic from below here // if (!useOnlyRisingEdgeForTrigger || stateParam == TV_RISE) { -// expectedEventCount[waveIndex]++; +// expectedEventCount[channelIndex]++; // } } @@ -304,25 +304,25 @@ void TriggerShape::calculateExpectedEventCounts(bool useOnlyRisingEdgeForTrigger /** * Deprecated - see https://github.com/rusefi/rusefi/issues/635 */ -void TriggerShape::addEvent2(angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const stateParam DECLARE_ENGINE_PARAMETER_SUFFIX) { +void TriggerShape::addEvent2(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const stateParam DECLARE_ENGINE_PARAMETER_SUFFIX) { /** * While '720' value works perfectly it has not much sense for crank sensor-only scenario. */ - addEvent(engineConfiguration->useOnlyRisingEdgeForTrigger, angle / getEngineCycle(operationMode), waveIndex, stateParam); + addEvent(engineConfiguration->useOnlyRisingEdgeForTrigger, angle / getEngineCycle(operationMode), channelIndex, stateParam); } // todo: the whole 'useOnlyRisingEdgeForTrigger' parameter and logic should not be here // todo: see calculateExpectedEventCounts // related calculation should be done once trigger is initialized outside of trigger shape scope -void TriggerShape::addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const stateParam) { +void TriggerShape::addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const stateParam) { efiAssertVoid(CUSTOM_OMODE_UNDEF, operationMode != OM_NONE, "operationMode not set"); - efiAssertVoid(CUSTOM_ERR_6598, waveIndex!= T_SECONDARY || needSecondTriggerInput, "secondary needed or not?"); + efiAssertVoid(CUSTOM_ERR_6598, channelIndex!= T_SECONDARY || needSecondTriggerInput, "secondary needed or not?"); #if EFI_UNIT_TEST || defined(__DOXYGEN__) if (printTriggerDebug) { - printf("addEvent2 %.2f i=%d r/f=%d\r\n", angle, waveIndex, stateParam); + printf("addEvent2 %.2f i=%d r/f=%d\r\n", angle, channelIndex, stateParam); } #endif @@ -334,13 +334,13 @@ void TriggerShape::addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, tri } #if EFI_UNIT_TEST || defined(__DOXYGEN__) - int signal = waveIndex * 1000 + stateParam; + int signal = channelIndex * 1000 + stateParam; triggerSignals[privateTriggerDefinitionSize] = signal; #endif if (!useOnlyRisingEdgeForTrigger || stateParam == TV_RISE) { - expectedEventCount[waveIndex]++; + expectedEventCount[channelIndex]++; } efiAssertVoid(CUSTOM_ERR_6599, angle > 0, "angle should be positive"); @@ -355,19 +355,19 @@ void TriggerShape::addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, tri if (privateTriggerDefinitionSize == 0) { privateTriggerDefinitionSize = 1; for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) { - SingleWave *wave = &this->wave.waves[i]; + SingleWave *wave = &this->wave.channels[i]; if (wave->pinStates == NULL) { warning(CUSTOM_ERR_STATE_NULL, "wave pinStates is NULL"); shapeDefinitionError = true; return; } - wave->pinStates[0] = initialState[i]; + wave->setState(/* channelIndex */ 0, /* value */ initialState[i]); } isFrontEvent[0] = TV_RISE == stateParam; wave.setSwitchTime(0, angle); - wave.waves[waveIndex].setState(0, state); + wave.channels[channelIndex].setState(/* channelIndex */ 0, /* value */ state); return; } @@ -402,10 +402,11 @@ void TriggerShape::addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, tri privateTriggerDefinitionSize++; for (int i = 0; i < PWM_PHASE_MAX_WAVE_PER_PWM; i++) { - wave.waves[i].pinStates[index] = wave.getChannelState(i, index - 1); + int value = wave.getChannelState(/* channelIndex */i, index - 1); + wave.channels[i].setState(index, value); } wave.setSwitchTime(index, angle); - wave.waves[waveIndex].setState(index, state); + wave.channels[channelIndex].setState(index, state); } angle_t TriggerShape::getSwitchAngle(int index) const { diff --git a/firmware/controllers/trigger/trigger_structure.h b/firmware/controllers/trigger/trigger_structure.h index 57d8995c45..d1008cdbb8 100644 --- a/firmware/controllers/trigger/trigger_structure.h +++ b/firmware/controllers/trigger/trigger_structure.h @@ -37,7 +37,7 @@ class trigger_shape_helper { public: trigger_shape_helper(); - SingleWave waves[TRIGGER_CHANNEL_COUNT]; + SingleWave channels[TRIGGER_CHANNEL_COUNT]; private: pin_state_t pinStates[TRIGGER_CHANNEL_COUNT][PWM_PHASE_MAX_COUNT]; }; @@ -175,10 +175,10 @@ public: */ int privateTriggerDefinitionSize; - void addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const state); - void addEvent2(angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const state DECLARE_ENGINE_PARAMETER_SUFFIX); + void addEvent(bool useOnlyRisingEdgeForTrigger, angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const state); + void addEvent2(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const state DECLARE_ENGINE_PARAMETER_SUFFIX); - void addEvent3(angle_t angle, trigger_wheel_e const waveIndex, trigger_value_e const stateParam, float filterLeft, float filterRight DECLARE_ENGINE_PARAMETER_SUFFIX); + void addEvent3(angle_t angle, trigger_wheel_e const channelIndex, trigger_value_e const stateParam, float filterLeft, float filterRight DECLARE_ENGINE_PARAMETER_SUFFIX); operation_mode_e getOperationMode(); void initialize(operation_mode_e operationMode, bool needSecondTriggerInput); diff --git a/unit_tests/test_pwm_generator.cpp b/unit_tests/test_pwm_generator.cpp index af6592be70..65bf01aa94 100644 --- a/unit_tests/test_pwm_generator.cpp +++ b/unit_tests/test_pwm_generator.cpp @@ -20,7 +20,7 @@ static int expectedTimeOfNextEvent; static int pinValue = -1; static void testApplyPinState(PwmConfig *state, int stateIndex) { - pinValue = state->multiWave.waves[0].pinStates[stateIndex]; + pinValue = state->multiWave.getChannelState(/*channelIndex*/0, stateIndex); printf("PWM_test: setPinValue=%d @ timeNow=%d\r\n", pinValue, timeNowUs); }