diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 8073a3beea..dfb527068f 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -16,6 +16,8 @@ using namespace luaaa; // Some functions lean on existing FSIO implementation #include "fsio_impl.h" +#define HUMAN_OFFSET 1 + #if EFI_UNIT_TEST Engine *engineForLuaUnitTests; #endif @@ -96,6 +98,7 @@ static int lua_table3d(lua_State* l) { } static int lua_curve2d(lua_State* l) { + // index starting from 1 auto curveIdx = luaL_checkinteger(l, 1); auto x = luaL_checknumber(l, 2); @@ -104,12 +107,28 @@ static int lua_curve2d(lua_State* l) { EXPAND_Engine; #endif - auto result = getCurveValue(curveIdx, x PASS_ENGINE_PARAMETER_SUFFIX); + auto result = getCurveValue(curveIdx - HUMAN_OFFSET, x PASS_ENGINE_PARAMETER_SUFFIX); lua_pushnumber(l, result); return 1; } +static int lua_findCurveIndex(lua_State* l) { +#if EFI_UNIT_TEST + Engine *engine = engineForLuaUnitTests; + EXPAND_Engine; +#endif + auto name = luaL_checklstring(l, 1, nullptr); + auto result = getCurveIndexByName(name PASS_ENGINE_PARAMETER_SUFFIX); + if (result == EFI_ERROR_CODE) { + lua_pushnil(l); + } else { + // TS counts curve from 1 so convert indexing here + lua_pushnumber(l, result + HUMAN_OFFSET); + } + return 1; +} + static int lua_txCan(lua_State* l) { auto channel = luaL_checkinteger(l, 1); // TODO: support multiple channels @@ -410,6 +429,7 @@ void configureRusefiLuaHooks(lua_State* l) { lua_register(l, "hasSensor", lua_hasSensor); lua_register(l, "table3d", lua_table3d); lua_register(l, "curve", lua_curve2d); + lua_register(l, "findCurveIndex", lua_findCurveIndex); lua_register(l, "txCan", lua_txCan); #if !EFI_UNIT_TEST diff --git a/unit_tests/tests/lua/test_lua_with_engine.cpp b/unit_tests/tests/lua/test_lua_with_engine.cpp new file mode 100644 index 0000000000..e265671dba --- /dev/null +++ b/unit_tests/tests/lua/test_lua_with_engine.cpp @@ -0,0 +1,31 @@ +/* + * test_lua_with_engine.cpp + * + * Created on: Nov 5, 2021 + * Author: rusefi + */ + +#include "pch.h" +#include "fsio_impl.h" +#include "rusefi_lua.h" + +static const char* curveTestScript = R"( + +function testFunc() + index = findCurveIndex("HELLO") + return curve(index, 40) +end + +)"; + +TEST(LuaHooks, TestCurve) { + WITH_ENGINE_TEST_HELPER(TEST_ENGINE); + + strcpy(engineConfiguration->scriptCurveName[3], "hello"); + setLinearCurve(engineConfiguration->scriptCurve4, 500, 600, 1); + + int index = getCurveIndexByName("helLO" PASS_ENGINE_PARAMETER_SUFFIX); + ASSERT_EQ(index, 3); + + EXPECT_EQ(testLuaReturnsNumberOrNil(curveTestScript).value_or(0), 540); +} diff --git a/unit_tests/tests/tests.mk b/unit_tests/tests/tests.mk index 69ed23bdff..02bbb94854 100644 --- a/unit_tests/tests/tests.mk +++ b/unit_tests/tests/tests.mk @@ -24,6 +24,7 @@ TESTS_SRC_CPP = \ tests/ignition_injection/test_fuel_computer.cpp \ tests/ignition_injection/test_injector_model.cpp \ tests/lua/test_lua_basic.cpp \ + tests/lua/test_lua_with_engine.cpp \ tests/lua/test_lua_hooks.cpp \ tests/sensor/test_cj125.cpp \ tests/test_change_engine_type.cpp \