interactive lua (#2614)

* interactive lua

* unnecessary pop

* print return value

* enable f7 lua

* match f7 chconf
This commit is contained in:
Matthew Kennedy 2021-05-03 14:46:12 -07:00 committed by GitHub
parent 0f4de02223
commit 8ae8484dd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 28 deletions

View File

@ -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

View File

@ -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

View File

@ -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
/** @} */