From 350a047eadf7c47a83bc977c79e7007b9058c168 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Tue, 23 Feb 2021 11:57:14 -1000 Subject: [PATCH] cleanup (#2386) --- firmware/controllers/core/fsio_core.cpp | 27 +++------------------- firmware/controllers/core/fsio_core.h | 10 +------- firmware/controllers/core/fsio_impl.cpp | 8 +++---- unit_tests/tests/test_logic_expression.cpp | 12 +++++----- 4 files changed, 14 insertions(+), 43 deletions(-) diff --git a/firmware/controllers/core/fsio_core.cpp b/firmware/controllers/core/fsio_core.cpp index 936ef0a3cc..e605145fa3 100644 --- a/firmware/controllers/core/fsio_core.cpp +++ b/firmware/controllers/core/fsio_core.cpp @@ -95,21 +95,11 @@ LECalculator::LECalculator() { } void LECalculator::reset() { - m_program = nullptr; stack.reset(); currentCalculationLogPosition = 0; memset(calcLogAction, 0, sizeof(calcLogAction)); } -void LECalculator::reset(LEElement *element) { - reset(); - setProgram(element); -} - -void LECalculator::setProgram(LEElement* program) { - m_program = program; -} - bool float2bool(float v) { return v != 0; } @@ -269,24 +259,13 @@ FsioResult LECalculator::processElement(const LEElement *element DECLARE_ENGINE_ } } -float LECalculator::getValue2(float selfValue, LEElement *fistElementInList DECLARE_ENGINE_PARAMETER_SUFFIX) { - reset(fistElementInList); - return getValue(selfValue PASS_ENGINE_PARAMETER_SUFFIX); -} - -bool LECalculator::isEmpty() const { - return !m_program; -} - -float LECalculator::getValue(float selfValue DECLARE_ENGINE_PARAMETER_SUFFIX) { - if (isEmpty()) { +float LECalculator::evaluate(float selfValue, const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX) { + if (!element) { warning(CUSTOM_NO_FSIO, "no FSIO code"); return NAN; } - const LEElement* element = m_program; - - stack.reset(); + reset(); int counter = 0; diff --git a/firmware/controllers/core/fsio_core.h b/firmware/controllers/core/fsio_core.h index 40ea3ae1d4..2d92cb70a7 100644 --- a/firmware/controllers/core/fsio_core.h +++ b/firmware/controllers/core/fsio_core.h @@ -130,12 +130,8 @@ typedef FLStack calc_stack_t; class LECalculator { public: LECalculator(); - float getValue(float selfValue DECLARE_ENGINE_PARAMETER_SUFFIX); - float getValue2(float selfValue, LEElement *fistElementInList DECLARE_ENGINE_PARAMETER_SUFFIX); - - bool isEmpty() const; + float evaluate(float selfValue, const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX); void reset(); - void reset(LEElement *element); // Log history of calculation actions for debugging le_action_e calcLogAction[MAX_CALC_LOG]; @@ -143,14 +139,10 @@ public: int currentCalculationLogPosition; private: - void setProgram(LEElement* program); - void push(le_action_e action, float value); FsioResult processElement(const LEElement* element DECLARE_ENGINE_PARAMETER_SUFFIX); float pop(le_action_e action); - LEElement* m_program = nullptr; - calc_stack_t stack; }; diff --git a/firmware/controllers/core/fsio_impl.cpp b/firmware/controllers/core/fsio_impl.cpp index 61a13050c8..04dbf2942d 100644 --- a/firmware/controllers/core/fsio_impl.cpp +++ b/firmware/controllers/core/fsio_impl.cpp @@ -337,7 +337,7 @@ float getFsioOutputValue(int index DECLARE_ENGINE_PARAMETER_SUFFIX) { warning(CUSTOM_NO_FSIO, "no FSIO for #%d %s", index + 1, hwPortname(CONFIG(fsioOutputPins)[index])); return NAN; } else { - return calc.getValue2(engine->fsioState.fsioLastValue[index], state.fsioLogics[index] PASS_ENGINE_PARAMETER_SUFFIX); + return calc.evaluate(engine->fsioState.fsioLastValue[index], state.fsioLogics[index] PASS_ENGINE_PARAMETER_SUFFIX); } } @@ -404,7 +404,7 @@ static void setPinState(const char * msg, OutputPin *pin, LEElement *element DEC if (!element) { warning(CUSTOM_FSIO_INVALID_EXPRESSION, "invalid expression for %s", msg); } else { - int value = (int)calc.getValue2(pin->getLogicValue(), element PASS_ENGINE_PARAMETER_SUFFIX); + int value = (int)calc.evaluate(pin->getLogicValue(), element PASS_ENGINE_PARAMETER_SUFFIX); if (pin->isInitialized() && value != pin->getLogicValue()) { for (int i = 0;i < calc.currentCalculationLogPosition;i++) { @@ -446,7 +446,7 @@ static bool updateValueOrWarning(int humanIndex, const char *msg, float *value D return false; } else { float beforeValue = *value; - *value = calc.getValue2(beforeValue, element PASS_ENGINE_PARAMETER_SUFFIX); + *value = calc.evaluate(beforeValue, element PASS_ENGINE_PARAMETER_SUFFIX); // floating '==' comparison without EPS seems fine here return (beforeValue != *value); } @@ -666,7 +666,7 @@ static void rpnEval(char *line) { if (e == NULL) { scheduleMsg(logger, "parsing failed"); } else { - float result = evalCalc.getValue2(0, e PASS_ENGINE_PARAMETER_SUFFIX); + float result = evalCalc.evaluate(0, e PASS_ENGINE_PARAMETER_SUFFIX); scheduleMsg(logger, "Evaluate result: %.2f", result); } #endif diff --git a/unit_tests/tests/test_logic_expression.cpp b/unit_tests/tests/test_logic_expression.cpp index 4505290427..acd4ffaae4 100644 --- a/unit_tests/tests/test_logic_expression.cpp +++ b/unit_tests/tests/test_logic_expression.cpp @@ -117,7 +117,7 @@ static void testExpression2(float selfValue, const char *line, float expected, E EXPAND_Engine; - ASSERT_NEAR(expected, c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX), EPS4D) << line; + ASSERT_NEAR(expected, c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX), EPS4D) << line; } static void testExpression2(float selfValue, const char *line, float expected, const std::unordered_map& sensorVals = {}) { @@ -144,20 +144,20 @@ TEST(fsio, testHysteresisSelf) { double selfValue = 0; engine->fsioState.mockRpm = 0; - selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); + selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); ASSERT_EQ(0, selfValue); engine->fsioState.mockRpm = 430; - selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); + selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); // OFF since not ON yet ASSERT_EQ(0, selfValue); engine->fsioState.mockRpm = 460; - selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); + selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); ASSERT_EQ(1, selfValue); engine->fsioState.mockRpm = 430; - selfValue = c.getValue2(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); + selfValue = c.evaluate(selfValue, element PASS_ENGINE_PARAMETER_SUFFIX); // OFF since was ON yet ASSERT_EQ(1, selfValue); } @@ -241,7 +241,7 @@ TEST(fsio, testLogicExpressions) { LEElement * element = pool.parseExpression("fan NOT coolant 90 > AND fan coolant 85 > AND OR"); ASSERT_TRUE(element != NULL) << "Not NULL expected"; LECalculator c; - ASSERT_EQ( 1, c.getValue2(0, element PASS_ENGINE_PARAMETER_SUFFIX)) << "that expression"; + ASSERT_EQ( 1, c.evaluate(0, element PASS_ENGINE_PARAMETER_SUFFIX)) << "that expression"; ASSERT_EQ(12, c.currentCalculationLogPosition); ASSERT_EQ(102, c.calcLogAction[0]);