From 427cce75965035dc35708d6c3d14044b39860aec Mon Sep 17 00:00:00 2001 From: rusefillc <48498823+rusefillc@users.noreply.github.com> Date: Fri, 3 Sep 2021 20:21:39 -0400 Subject: [PATCH] Lua timer (#3218) * Lua: Timer class #3159 * Lua: Timer class #3159 * Lua: Timer class #3159 * Lua: Timer class #3159 * lua timer is happy (#3216) * hooks are happy * don't need that any more * format * update lib * module Co-authored-by: rusefillc Co-authored-by: Matthew Kennedy --- .gitmodules | 4 ++++ firmware/controllers/lua/lua.mk | 2 +- firmware/controllers/lua/lua_hooks.cpp | 10 ++++++++++ firmware/controllers/lua/luaaa | 1 + unit_tests/tests/lua/test_lua_hooks.cpp | 12 ++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 160000 firmware/controllers/lua/luaaa diff --git a/.gitmodules b/.gitmodules index 9f8c03774d..82b6ae47c8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -29,3 +29,7 @@ path = firmware/ext/lua url = https://github.com/rusefi/lua branch = rusefi-5.4.3 +[submodule "firmware/controllers/lua/luaaa"] + path = firmware/controllers/lua/luaaa + url = https://github.com/rusefi/luaaa + branch = rusefi_prod diff --git a/firmware/controllers/lua/lua.mk b/firmware/controllers/lua/lua.mk index 42492f1be0..6d9e79c32b 100644 --- a/firmware/controllers/lua/lua.mk +++ b/firmware/controllers/lua/lua.mk @@ -4,7 +4,7 @@ LUA_EXT=$(PROJECT_DIR)/ext/lua ALLCPPSRC += $(LUA_DIR)/lua.cpp \ $(LUA_DIR)/lua_hooks.cpp \ -ALLINC += $(LUA_DIR) $(LUA_EXT) +ALLINC += $(LUA_DIR) $(LUA_DIR)/luaaa $(LUA_EXT) ALLCSRC += \ $(LUA_EXT)/lapi.c \ $(LUA_EXT)/lcode.c \ diff --git a/firmware/controllers/lua/lua_hooks.cpp b/firmware/controllers/lua/lua_hooks.cpp index 8784c04074..fc2b36075c 100644 --- a/firmware/controllers/lua/lua_hooks.cpp +++ b/firmware/controllers/lua/lua_hooks.cpp @@ -8,6 +8,9 @@ #include "lua_airmass.h" #include "can_msg_tx.h" #include "settings.h" +#include +#include "luaaa.hpp" +using namespace luaaa; // Some functions lean on existing FSIO implementation #include "fsio_impl.h" @@ -307,6 +310,13 @@ static int lua_stopEngine(lua_State*) { #endif // EFI_UNIT_TEST void configureRusefiLuaHooks(lua_State* l) { + + LuaClass luaTimer(l, "Timer"); + luaTimer + .ctor() + .fun("reset", static_cast(&Timer::reset )) + .fun("getElapsedSeconds", static_cast(&Timer::getElapsedSeconds)); + lua_register(l, "print", lua_efi_print); lua_register(l, "readPin", lua_readpin); lua_register(l, "getAuxAnalog", lua_getAuxAnalog); diff --git a/firmware/controllers/lua/luaaa b/firmware/controllers/lua/luaaa new file mode 160000 index 0000000000..97ee01219e --- /dev/null +++ b/firmware/controllers/lua/luaaa @@ -0,0 +1 @@ +Subproject commit 97ee01219e52271f9c5bfb57522a9be919798b01 diff --git a/unit_tests/tests/lua/test_lua_hooks.cpp b/unit_tests/tests/lua/test_lua_hooks.cpp index 3f0bd383b1..125177fd84 100644 --- a/unit_tests/tests/lua/test_lua_hooks.cpp +++ b/unit_tests/tests/lua/test_lua_hooks.cpp @@ -73,3 +73,15 @@ TEST(LuaHooks, CanTxDataLength) { // invalid: not a table EXPECT_ANY_THROW(testLuaExecString("txCan(1, 0, 0, 26)")); } + +static const char* timerTest = R"( +function testFunc() + local a = Timer.new() + a:reset() + return a:getElapsedSeconds() +end +)"; + +TEST(LuaHooks, TestLuaTimer) { + EXPECT_EQ(testLuaReturnsNumber(timerTest), 0); +}