From 8ae8484dd9ff57cacae304f0764216cfd6fa698d Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Mon, 3 May 2021 14:46:12 -0700 Subject: [PATCH] interactive lua (#2614) * interactive lua * unnecessary pop * print return value * enable f7 lua * match f7 chconf --- firmware/config/stm32f7ems/efifeatures.h | 3 + firmware/controllers/lua/lua.cpp | 100 +++++++++++++----- .../hw_layer/ports/stm32/stm32f7/cfg/chconf.h | 6 +- 3 files changed, 81 insertions(+), 28 deletions(-) diff --git a/firmware/config/stm32f7ems/efifeatures.h b/firmware/config/stm32f7ems/efifeatures.h index 98bfbb8bd4..b7d8540fb1 100644 --- a/firmware/config/stm32f7ems/efifeatures.h +++ b/firmware/config/stm32f7ems/efifeatures.h @@ -52,3 +52,6 @@ #define EFI_CONSOLE_RX_BRAIN_PIN GPIOD_9 #define EFI_USE_COMPRESSED_INI_MSD + +#undef EFI_LUA +#define EFI_LUA TRUE diff --git a/firmware/controllers/lua/lua.cpp b/firmware/controllers/lua/lua.cpp index 8cd242fed9..08d85b0e71 100644 --- a/firmware/controllers/lua/lua.cpp +++ b/firmware/controllers/lua/lua.cpp @@ -8,6 +8,8 @@ #include "lua.hpp" #include "lua_hooks.h" +#define TAG "LUA " + #if EFI_PROD_CODE #include "ch.h" @@ -112,20 +114,77 @@ static LuaHandle setupLuaState() { } static bool loadScript(LuaHandle& ls, const char* scriptStr) { - efiPrintf("loading script length: %d", efiStrlen(scriptStr)); + efiPrintf(TAG "loading script length: %d...", efiStrlen(scriptStr)); if (0 != luaL_dostring(ls, scriptStr)) { - efiPrintf("LUA error loading script: %s", lua_tostring(ls, -1)); + efiPrintf(TAG "ERROR loading script: %s", lua_tostring(ls, -1)); lua_pop(ls, 1); return false; } - efiPrintf("script loaded"); + efiPrintf(TAG "script loaded successfully!"); return true; } #if !EFI_UNIT_TEST +static bool interactivePending = false; +static char interactiveCmd[100]; + +void doInteractive(LuaHandle& ls) { + if (!interactivePending) { + // no cmd pending, return + return; + } + + auto status = luaL_dostring(ls, interactiveCmd); + + if (0 == status) { + // Function call was OK, resolve return value and print it + if (lua_isinteger(ls, -1)) { + efiPrintf(TAG "interactive returned integer: %d", lua_tointeger(ls, -1)); + } else if (lua_isnumber(ls, -1)) { + efiPrintf(TAG "interactive returned number: %f", lua_tonumber(ls, -1)); + } else if (lua_isstring(ls, -1)) { + efiPrintf(TAG "interactive returned string: '%s'", lua_tostring(ls, -1)); + } else if (lua_isnil(ls, -1)) { + efiPrintf(TAG "interactive returned nil."); + } else { + efiPrintf(TAG "interactive returned nothing."); + } + } else { + // error with interactive command, print it + efiPrintf(TAG "interactive error: %s", lua_tostring(ls, -1)); + } + + interactivePending = false; + + lua_settop(ls, 0); +} + +void invokeTick(LuaHandle& ls) { + ScopePerf perf(PE::LuaTickFunction); + + // run the tick function + lua_getglobal(ls, "onTick"); + if (lua_isnil(ls, -1)) { + // TODO: handle missing tick function + lua_settop(ls, 0); + return; + } + + int status = lua_pcall(ls, 0, 0, 0); + + if (0 != status) { + // error calling hook function + auto errMsg = lua_tostring(ls, -1); + efiPrintf(TAG "error %s", errMsg); + lua_pop(ls, 1); + } + + lua_settop(ls, 0); +} + struct LuaThread : ThreadController<4096> { LuaThread() : ThreadController("lua", PRIO_LUA) { } @@ -154,29 +213,10 @@ void LuaThread::ThreadTask() { } while (!chThdShouldTerminateX()) { - // run the tick function - lua_getglobal(ls, "onTick"); - if (lua_isnil(ls, -1)) { - // TODO: handle missing tick function - lua_pop(ls, 1); - lua_settop(ls, 0); - continue; - } + // First, check if there is a pending interactive command entered by the user + doInteractive(ls); - { - ScopePerf perf(PE::LuaTickFunction); - - int status = lua_pcall(ls, 0, 0, 0); - - if (0 != status) { - // error calling hook function - auto errMsg = lua_tostring(ls, -1); - efiPrintf("lua err %s", errMsg); - lua_pop(ls, 1); - } - } - - lua_settop(ls, 0); + invokeTick(ls); chThdSleepMilliseconds(luaTickPeriodMs); } @@ -186,6 +226,16 @@ static LuaThread luaThread; void startLua() { luaThread.Start(); + + addConsoleActionS("lua", [](const char* str){ + if (interactivePending) { + return; + } + + strncpy(interactiveCmd, str, sizeof(interactiveCmd)); + + interactivePending = true; + }); } #else // not EFI_UNIT_TEST diff --git a/firmware/hw_layer/ports/stm32/stm32f7/cfg/chconf.h b/firmware/hw_layer/ports/stm32/stm32f7/cfg/chconf.h index dc889d6fec..e346eb85c7 100644 --- a/firmware/hw_layer/ports/stm32/stm32f7/cfg/chconf.h +++ b/firmware/hw_layer/ports/stm32/stm32f7/cfg/chconf.h @@ -364,7 +364,7 @@ * @note The default is @p TRUE. */ #if !defined(CH_CFG_USE_MEMCORE) -#define CH_CFG_USE_MEMCORE FALSE +#define CH_CFG_USE_MEMCORE TRUE #endif /** @@ -393,7 +393,7 @@ * @note Mutexes are recommended. */ #if !defined(CH_CFG_USE_HEAP) -#define CH_CFG_USE_HEAP FALSE +#define CH_CFG_USE_HEAP TRUE #endif /** @@ -652,7 +652,7 @@ * tickless mode. */ #if !defined(CH_DBG_THREADS_PROFILING) -#define CH_DBG_THREADS_PROFILING FALSE +#define CH_DBG_THREADS_PROFILING TRUE #endif /** @} */