From 44fa9b028b0e7771543aeb662b0fd32fd1ad9d87 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 2 Nov 2021 23:35:48 -0400 Subject: [PATCH] lua interpolate --- firmware/controllers/lua/lua_hooks_util.cpp | 19 +++++++++++++++++-- unit_tests/tests/lua/test_lua_hooks.cpp | 10 ++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/firmware/controllers/lua/lua_hooks_util.cpp b/firmware/controllers/lua/lua_hooks_util.cpp index 34d484ca50..9a66953484 100644 --- a/firmware/controllers/lua/lua_hooks_util.cpp +++ b/firmware/controllers/lua/lua_hooks_util.cpp @@ -16,8 +16,23 @@ static int lua_efi_print(lua_State* l) { return 0; } -void configureRusefiLuaUtilHooks(lua_State* l) { - lua_register(l, "print", lua_efi_print); +static int lua_interpolate(lua_State* l) { + auto x1 = luaL_checknumber(l, 1); + auto y1 = luaL_checknumber(l, 2); + auto x2 = luaL_checknumber(l, 3); + auto y2 = luaL_checknumber(l, 4); + auto x = luaL_checknumber(l, 5); + + auto result = interpolateMsg("lua", x1, y1, x2, y2, x); + + lua_pushnumber(l, result); + return 1; +} + + +void configureRusefiLuaUtilHooks(lua_State* l) { + lua_register(l, "print", lua_efi_print); + lua_register(l, "interpolate", lua_interpolate); } diff --git a/unit_tests/tests/lua/test_lua_hooks.cpp b/unit_tests/tests/lua/test_lua_hooks.cpp index 99031e5410..ab6b1da58d 100644 --- a/unit_tests/tests/lua/test_lua_hooks.cpp +++ b/unit_tests/tests/lua/test_lua_hooks.cpp @@ -92,6 +92,16 @@ TEST(LuaHooks, CanTxDataLength) { EXPECT_ANY_THROW(testLuaExecString("txCan(1, 0, 0, 26)")); } +static const char* interpolationTest = R"( +function testFunc() + return interpolate(1, 10, 5, 50, 3) +end +)"; + +TEST(LuaHooks, LuaInterpolate) { + EXPECT_EQ(testLuaReturnsNumber(interpolationTest), 30); +} + static const char* timerTest = R"( function testFunc() local a = Timer.new()