diff --git a/firmware/controllers/engine_controller.cpp b/firmware/controllers/engine_controller.cpp index ac390ce8ef..862194b444 100644 --- a/firmware/controllers/engine_controller.cpp +++ b/firmware/controllers/engine_controller.cpp @@ -772,7 +772,7 @@ void initEngineContoller(DECLARE_ENGINE_PARAMETER_SUFFIX) { * UNUSED_SIZE constants. */ #ifndef RAM_UNUSED_SIZE -#define RAM_UNUSED_SIZE 8000 +#define RAM_UNUSED_SIZE 3500 #endif #ifndef CCM_UNUSED_SIZE #define CCM_UNUSED_SIZE 600 diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index f09cdc7d77..6407a63f17 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -16,7 +16,7 @@ #define LUA_USER_HEAP 12000 #endif #ifndef CCM_UNUSED_SIZE -#define LUA_SYSTEM_HEAP 10000 +#define LUA_SYSTEM_HEAP 15000 #endif static char luaUserHeap[LUA_USER_HEAP]; @@ -204,6 +204,33 @@ static bool loadScript(LuaHandle& ls, const char* scriptStr) { return true; } +static LuaHandle systemLua; + +const char* getSystemLuaScript(); + +void initSystemLua() { + efiAssertVoid(OBD_PCM_Processor_Fault, !systemLua, "system lua already init"); + + Timer startTimer; + startTimer.reset(); + + systemLua = setupLuaState(myAlloc<1>); + + efiAssertVoid(OBD_PCM_Processor_Fault, systemLua, "system lua init fail"); + + if (!loadScript(systemLua, getSystemLuaScript())) { + firmwareError(OBD_PCM_Processor_Fault, "system lua script load fail"); + systemLua = nullptr; + return; + } + + auto startTime = startTimer.getElapsedSeconds(); + +#if !EFI_UNIT_TEST + efiPrintf("System Lua loaded in %.2f ms using %d bytes", startTime * 1'000, heaps[1].used()); +#endif +} + #if !EFI_UNIT_TEST static bool interactivePending = false; static char interactiveCmd[100]; @@ -312,6 +339,8 @@ static bool runOneLua(lua_Alloc alloc, const char* script) { } void LuaThread::ThreadTask() { + initSystemLua(); + while (!chThdShouldTerminateX()) { bool wasOk = runOneLua(myAlloc<0>, config->luaScript); @@ -327,33 +356,9 @@ void LuaThread::ThreadTask() { static LuaThread luaThread; -static LuaHandle systemLua; - -void initSystemLua() { - efiAssertVoid(OBD_PCM_Processor_Fault, !systemLua, "system lua already init"); - - Timer startTimer; - startTimer.reset(); - - systemLua = setupLuaState(myAlloc<1>); - - efiAssertVoid(OBD_PCM_Processor_Fault, systemLua, "system lua init fail"); - - if (!loadScript(systemLua, "function x() end")) { - firmwareError(OBD_PCM_Processor_Fault, "system lua script load fail"); - systemLua = nullptr; - return; - } - - auto startTime = startTimer.getElapsedSeconds(); - efiPrintf("System Lua loaded in %.2f ms using %d bytes", startTime * 1'000, heaps[1].used()); -} - void startLua() { luaThread.Start(); - initSystemLua(); - addConsoleActionS("lua", [](const char* str){ if (interactivePending) { return; @@ -382,7 +387,7 @@ void startLua() { #else // not EFI_UNIT_TEST void startLua() { - // todo + initSystemLua(); } #include diff --git a/firmware/controllers/lua/lua.mk b/firmware/controllers/lua/lua.mk index 6d9e79c32b..911a94f36d 100644 --- a/firmware/controllers/lua/lua.mk +++ b/firmware/controllers/lua/lua.mk @@ -3,6 +3,7 @@ LUA_EXT=$(PROJECT_DIR)/ext/lua ALLCPPSRC += $(LUA_DIR)/lua.cpp \ $(LUA_DIR)/lua_hooks.cpp \ + $(LUA_DIR)/system_lua.cpp \ ALLINC += $(LUA_DIR) $(LUA_DIR)/luaaa $(LUA_EXT) ALLCSRC += \ diff --git a/firmware/controllers/lua/system_lua.cpp b/firmware/controllers/lua/system_lua.cpp new file mode 100644 index 0000000000..de7c18c0b7 --- /dev/null +++ b/firmware/controllers/lua/system_lua.cpp @@ -0,0 +1,19 @@ +const char* getSystemLuaScript() { + return R"LUASCRIPT( + +function getConfig(x) + return x +end + +primeTimer = Timer.new() +primeTimer:reset(); + +function pumpLogic() + local prime = primeTimer:getElapsedSeconds() > getConfig(0) + local spinning = getTimeSinceTrigger() < 1 + + return prime or spinning +end + + )LUASCRIPT"; +} \ No newline at end of file diff --git a/unit_tests/tests/lua/test_lua_basic.cpp b/unit_tests/tests/lua/test_lua_basic.cpp index 5b108ba9b0..c5cb9e0898 100644 --- a/unit_tests/tests/lua/test_lua_basic.cpp +++ b/unit_tests/tests/lua/test_lua_basic.cpp @@ -76,3 +76,7 @@ TEST(LuaBasic, ExpectNumOrNilReturnsNothing) { end )"), unexpected); } + +TEST(SystemLua, ScriptLoads) { + startLua(); +}