interactive lua (#2614)
* interactive lua * unnecessary pop * print return value * enable f7 lua * match f7 chconf
This commit is contained in:
parent
0f4de02223
commit
8ae8484dd9
|
@ -52,3 +52,6 @@
|
||||||
#define EFI_CONSOLE_RX_BRAIN_PIN GPIOD_9
|
#define EFI_CONSOLE_RX_BRAIN_PIN GPIOD_9
|
||||||
|
|
||||||
#define EFI_USE_COMPRESSED_INI_MSD
|
#define EFI_USE_COMPRESSED_INI_MSD
|
||||||
|
|
||||||
|
#undef EFI_LUA
|
||||||
|
#define EFI_LUA TRUE
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#include "lua.hpp"
|
#include "lua.hpp"
|
||||||
#include "lua_hooks.h"
|
#include "lua_hooks.h"
|
||||||
|
|
||||||
|
#define TAG "LUA "
|
||||||
|
|
||||||
#if EFI_PROD_CODE
|
#if EFI_PROD_CODE
|
||||||
#include "ch.h"
|
#include "ch.h"
|
||||||
|
|
||||||
|
@ -112,20 +114,77 @@ static LuaHandle setupLuaState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool loadScript(LuaHandle& ls, const char* scriptStr) {
|
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)) {
|
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);
|
lua_pop(ls, 1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
efiPrintf("script loaded");
|
efiPrintf(TAG "script loaded successfully!");
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !EFI_UNIT_TEST
|
#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> {
|
struct LuaThread : ThreadController<4096> {
|
||||||
LuaThread() : ThreadController("lua", PRIO_LUA) { }
|
LuaThread() : ThreadController("lua", PRIO_LUA) { }
|
||||||
|
|
||||||
|
@ -154,29 +213,10 @@ void LuaThread::ThreadTask() {
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!chThdShouldTerminateX()) {
|
while (!chThdShouldTerminateX()) {
|
||||||
// run the tick function
|
// First, check if there is a pending interactive command entered by the user
|
||||||
lua_getglobal(ls, "onTick");
|
doInteractive(ls);
|
||||||
if (lua_isnil(ls, -1)) {
|
|
||||||
// TODO: handle missing tick function
|
|
||||||
lua_pop(ls, 1);
|
|
||||||
lua_settop(ls, 0);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
invokeTick(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);
|
|
||||||
|
|
||||||
chThdSleepMilliseconds(luaTickPeriodMs);
|
chThdSleepMilliseconds(luaTickPeriodMs);
|
||||||
}
|
}
|
||||||
|
@ -186,6 +226,16 @@ static LuaThread luaThread;
|
||||||
|
|
||||||
void startLua() {
|
void startLua() {
|
||||||
luaThread.Start();
|
luaThread.Start();
|
||||||
|
|
||||||
|
addConsoleActionS("lua", [](const char* str){
|
||||||
|
if (interactivePending) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(interactiveCmd, str, sizeof(interactiveCmd));
|
||||||
|
|
||||||
|
interactivePending = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // not EFI_UNIT_TEST
|
#else // not EFI_UNIT_TEST
|
||||||
|
|
|
@ -364,7 +364,7 @@
|
||||||
* @note The default is @p TRUE.
|
* @note The default is @p TRUE.
|
||||||
*/
|
*/
|
||||||
#if !defined(CH_CFG_USE_MEMCORE)
|
#if !defined(CH_CFG_USE_MEMCORE)
|
||||||
#define CH_CFG_USE_MEMCORE FALSE
|
#define CH_CFG_USE_MEMCORE TRUE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -393,7 +393,7 @@
|
||||||
* @note Mutexes are recommended.
|
* @note Mutexes are recommended.
|
||||||
*/
|
*/
|
||||||
#if !defined(CH_CFG_USE_HEAP)
|
#if !defined(CH_CFG_USE_HEAP)
|
||||||
#define CH_CFG_USE_HEAP FALSE
|
#define CH_CFG_USE_HEAP TRUE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -652,7 +652,7 @@
|
||||||
* tickless mode.
|
* tickless mode.
|
||||||
*/
|
*/
|
||||||
#if !defined(CH_DBG_THREADS_PROFILING)
|
#if !defined(CH_DBG_THREADS_PROFILING)
|
||||||
#define CH_DBG_THREADS_PROFILING FALSE
|
#define CH_DBG_THREADS_PROFILING TRUE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
Loading…
Reference in New Issue