From b662e59707c038b9e3c28b0f699e03ed64d51847 Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sun, 28 Aug 2022 06:43:21 -0700 Subject: [PATCH] remove some uses of EFI_ERROR_CODE (#4496) * lua hooks * findAngleMatch * s * s --- firmware/controllers/core/state_sequence.cpp | 4 ++-- firmware/controllers/core/state_sequence.h | 4 ++-- firmware/controllers/lua/lua_hooks.cpp | 12 ++++++------ firmware/controllers/lua/script_impl.cpp | 15 +++++++++------ firmware/controllers/lua/script_impl.h | 6 +++--- .../trigger/decoders/trigger_structure.cpp | 3 +-- .../tests/ignition_injection/test_fuel_map.cpp | 6 +++--- unit_tests/tests/lua/test_lua_with_engine.cpp | 4 ++-- 8 files changed, 28 insertions(+), 26 deletions(-) diff --git a/firmware/controllers/core/state_sequence.cpp b/firmware/controllers/core/state_sequence.cpp index 610a5b0f55..fc7a121c66 100644 --- a/firmware/controllers/core/state_sequence.cpp +++ b/firmware/controllers/core/state_sequence.cpp @@ -40,10 +40,10 @@ int MultiChannelStateSequence::findInsertionAngle(const float angle) const { return 0; } -int MultiChannelStateSequence::findAngleMatch(const float angle) const { +expected MultiChannelStateSequence::findAngleMatch(const float angle) const { for (int i = 0; i < phaseCount; i++) { if (isSameF(getSwitchTime(i), angle)) return i; } - return EFI_ERROR_CODE; + return unexpected; } diff --git a/firmware/controllers/core/state_sequence.h b/firmware/controllers/core/state_sequence.h index 77e754cd64..55151c1987 100644 --- a/firmware/controllers/core/state_sequence.h +++ b/firmware/controllers/core/state_sequence.h @@ -47,8 +47,8 @@ public: // Make sure the switch times are in order and end at the very end. void checkSwitchTimes(float scale) const; - // Find the exact angle, or EFI_ERROR_CODE if it doesn't exist - int findAngleMatch(float angle) const; + // Find the exact angle, or unexpected if it doesn't exist + expected findAngleMatch(float angle) const; // returns the index at which given value would need to be inserted into sorted array int findInsertionAngle(float angle) const; diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 2fbd58dc19..a88f1d7532 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -616,11 +616,11 @@ void configureRusefiLuaHooks(lua_State* l) { lua_register(l, "findCurveIndex", [](lua_State* l) { auto name = luaL_checklstring(l, 1, nullptr); auto result = getCurveIndexByName(name); - if (result == EFI_ERROR_CODE) { + if (!result) { lua_pushnil(l); } else { // TS counts curve from 1 so convert indexing here - lua_pushnumber(l, result + HUMAN_OFFSET); + lua_pushnumber(l, result.Value + HUMAN_OFFSET); } return 1; }); @@ -633,11 +633,11 @@ void configureRusefiLuaHooks(lua_State* l) { [](lua_State* l) { auto name = luaL_checklstring(l, 1, nullptr); auto index = getTableIndexByName(name); - if (index == EFI_ERROR_CODE) { + if (!index) { lua_pushnil(l); } else { // TS counts curve from 1 so convert indexing here - lua_pushnumber(l, index + HUMAN_OFFSET); + lua_pushnumber(l, index.Value + HUMAN_OFFSET); } return 1; }); @@ -648,11 +648,11 @@ void configureRusefiLuaHooks(lua_State* l) { auto defaultValue = luaL_checknumber(l, 2); auto index = getSettingIndexByName(name); - if (index == EFI_ERROR_CODE) { + if (!index) { lua_pushnumber(l, defaultValue); } else { // TS counts curve from 1 so convert indexing here - lua_pushnumber(l, engineConfiguration->scriptSetting[index]); + lua_pushnumber(l, engineConfiguration->scriptSetting[index.Value]); } return 1; }); diff --git a/firmware/controllers/lua/script_impl.cpp b/firmware/controllers/lua/script_impl.cpp index ec71f76545..58530e3527 100644 --- a/firmware/controllers/lua/script_impl.cpp +++ b/firmware/controllers/lua/script_impl.cpp @@ -34,31 +34,34 @@ ValueProvider3D *getscriptTable(int index) { /** * @return zero-based index of curve with given name */ -int getCurveIndexByName(const char *name) { +expected getCurveIndexByName(const char *name) { for (int i = 0;iscriptCurveName[i])) { return i; } } - return EFI_ERROR_CODE; + + return unexpected; } -int getTableIndexByName(const char *name) { +expected getTableIndexByName(const char *name) { for (int i = 0;iscriptTableName[i])) { return i; } } - return EFI_ERROR_CODE; + + return unexpected; } -int getSettingIndexByName(const char *name) { +expected getSettingIndexByName(const char *name) { for (int i = 0;iscriptSettingName[i])) { return i; } } - return EFI_ERROR_CODE; + + return unexpected; } float getCurveValue(int index, float key) { diff --git a/firmware/controllers/lua/script_impl.h b/firmware/controllers/lua/script_impl.h index 113df42754..5b79c006f8 100644 --- a/firmware/controllers/lua/script_impl.h +++ b/firmware/controllers/lua/script_impl.h @@ -16,7 +16,7 @@ typedef Map3D fsio8_M void initScriptImpl(); float getCurveValue(int index, float key); -int getCurveIndexByName(const char *name); -int getTableIndexByName(const char *name); -int getSettingIndexByName(const char *name); +expected getCurveIndexByName(const char *name); +expected getTableIndexByName(const char *name); +expected getSettingIndexByName(const char *name); ValueProvider3D *getscriptTable(int index); diff --git a/firmware/controllers/trigger/decoders/trigger_structure.cpp b/firmware/controllers/trigger/decoders/trigger_structure.cpp index f1205f5449..5d79f3caf6 100644 --- a/firmware/controllers/trigger/decoders/trigger_structure.cpp +++ b/firmware/controllers/trigger/decoders/trigger_structure.cpp @@ -286,8 +286,7 @@ void TriggerWaveform::addEvent(angle_t angle, trigger_wheel_e const channelIndex return; } - int exactMatch = wave.findAngleMatch(angle); - if (exactMatch != (int)EFI_ERROR_CODE) { + if (wave.findAngleMatch(angle)) { warning(CUSTOM_ERR_SAME_ANGLE, "same angle: not supported"); setShapeDefinitionError(true); return; diff --git a/unit_tests/tests/ignition_injection/test_fuel_map.cpp b/unit_tests/tests/ignition_injection/test_fuel_map.cpp index c7b0742762..fee44430d5 100644 --- a/unit_tests/tests/ignition_injection/test_fuel_map.cpp +++ b/unit_tests/tests/ignition_injection/test_fuel_map.cpp @@ -119,9 +119,9 @@ static void configureFordAspireTriggerWaveform(TriggerWaveform * s) { ASSERT_FLOAT_EQ(121.90 / 720, s->wave.getSwitchTime(1)); ASSERT_FLOAT_EQ(657.03 / 720, s->wave.getSwitchTime(8)); - ASSERT_EQ( 0, s->wave.findAngleMatch(53.747 / 720.0)) << "expecting 0"; - assertEqualsM("expecting not found", -1, s->wave.findAngleMatch(53 / 720.0)); - ASSERT_EQ(7, s->wave.findAngleMatch(588.045 / 720.0)); + ASSERT_EQ(0, s->wave.findAngleMatch(53.747 / 720.0).value_or(-1)) << "expecting 0"; + ASSERT_FALSE(s->wave.findAngleMatch(53 / 720.0).Valid) << "expecting not found"; + ASSERT_EQ(7, s->wave.findAngleMatch(588.045 / 720.0).value_or(-1)); ASSERT_EQ( 0, s->wave.findInsertionAngle(23.747 / 720.0)) << "expecting 0"; ASSERT_EQ( 1, s->wave.findInsertionAngle(63.747 / 720.0)) << "expecting 1"; diff --git a/unit_tests/tests/lua/test_lua_with_engine.cpp b/unit_tests/tests/lua/test_lua_with_engine.cpp index d33a302e26..769e59a319 100644 --- a/unit_tests/tests/lua/test_lua_with_engine.cpp +++ b/unit_tests/tests/lua/test_lua_with_engine.cpp @@ -24,8 +24,8 @@ TEST(LuaHooks, TestCurve) { strcpy(engineConfiguration->scriptCurveName[3], "hello"); setLinearCurve(config->scriptCurve4, 500, 600, 1); - int index = getCurveIndexByName("helLO"); - ASSERT_EQ(index, 3); + auto index = getCurveIndexByName("helLO"); + ASSERT_EQ(index.value_or(-1), 3); EXPECT_EQ(testLuaReturnsNumberOrNil(curveTestScript).value_or(0), 540); }