From 62f2a321f2da614e02fcd1e0a7f2714c91da54bc Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Sun, 7 Dec 2014 17:05:16 +0100 Subject: [PATCH] Refactored luaTask() based on script type --- radio/src/lua_api.cpp | 142 +++++------------------------------------ radio/src/lua_api.h | 15 +++-- radio/src/main_arm.cpp | 12 ++-- 3 files changed, 31 insertions(+), 138 deletions(-) diff --git a/radio/src/lua_api.cpp b/radio/src/lua_api.cpp index 07483b2e3..e8abbbac5 100644 --- a/radio/src/lua_api.cpp +++ b/radio/src/lua_api.cpp @@ -1831,7 +1831,7 @@ void luaDoOneRunStandalone(uint8_t evt) } } -bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) +bool luaDoOneRunPermanentScript(uint8_t evt, int i, uint8_t scriptType) { ScriptInternalData & sid = scriptInternalData[i]; if (sid.state != SCRIPT_OK) return false; @@ -1843,11 +1843,10 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) #endif ScriptInputsOutputs * sio = NULL; #if SCRIPT_MIX_FIRST > 0 - if (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST) { + if ((scriptType & RUN_MIX_SCRIPT) && (sid.reference >= SCRIPT_MIX_FIRST && sid.reference <= SCRIPT_MIX_LAST)) { #else - if (sid.reference <= SCRIPT_MIX_LAST) { + if ((scriptType & RUN_MIX_SCRIPT) && (sid.reference <= SCRIPT_MIX_LAST)) { #endif - if (runGuiScripts) return false; //mixer script is not gui script ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST]; sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST]; inputsCount = sio->inputsCount; @@ -1862,9 +1861,8 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) lua_pushinteger(L, sd.inputs[j] + sio->inputs[j].def); } } - else if (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST) { + else if ((scriptType & RUN_FUNC_SCRIPT) && (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_FUNC_LAST)) { CustomFunctionData & fn = g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST]; - if (runGuiScripts) return false; //function script is not gui script if (!getSwitch(fn.swtch)) return false; #if defined(SIMU) || defined(DEBUG) filename = fn.play.name; @@ -1876,14 +1874,13 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts) TelemetryScriptData & script = g_model.frsky.screens[sid.reference-SCRIPT_TELEMETRY_FIRST].script; filename = script.file; #endif - if (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view) { - if (!runGuiScripts) return false; //telemetry script gui part, do not run this time + if ((scriptType & RUN_TELEM_FG_SCRIPT) && + (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view)) { lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); lua_pushinteger(L, evt); inputsCount = 1; } - else if (sid.background) { - if (runGuiScripts) return false; //telemetry script non-gui part, do not run this time + else if ((scriptType & RUN_TELEM_BG_SCRIPT) && (sid.background)) { lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background); } else { @@ -1948,61 +1945,16 @@ void luaDoGc() } } - -uint32_t lastRunTime; - -void luaRunNonGuiScripts() +bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage) { - //execute one cycle of all Lua cripts that don't use LCD (model scripts, function scripts) - - uint32_t t0 = get_tmr10ms(); - static uint32_t lastLuaTime = 0; - uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); - lastLuaTime = t0; - if (interval > maxLuaInterval) { - maxLuaInterval = interval; - } - - luaLcdAllowed = false; - if (luaState == INTERPRETER_PANIC) return; - if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) return; - - if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { - luaState = 0; - luaInit(); - if (luaState == INTERPRETER_PANIC) return; - luaLoadPermanentScripts(); - if (luaState == INTERPRETER_PANIC) return; - } - for (int i=0; i maxLuaDuration) { - maxLuaDuration = lastRunTime; - } - return scriptWasRun; } -#if 0 -void luaTask(uint8_t evt) -{ - uint32_t t0 = get_tmr10ms(); - static uint32_t lastLuaTime = 0; - uint16_t interval = (lastLuaTime == 0 ? 0 : (t0 - lastLuaTime)); - lastLuaTime = t0; - if (interval > maxLuaInterval) { - maxLuaInterval = interval; - } - - if (luaState == INTERPRETER_PANIC) { - return; - } - - if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) { - PROTECT_LUA() { - luaDoOneRunStandalone(evt); - } - else { - luaDisable(); - return; - } - UNPROTECT_LUA(); - } - else { - // model scripts - if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { - luaState = 0; - luaInit(); - if (luaState == INTERPRETER_PANIC) return; - luaLoadPermanentScripts(); - if (luaState == INTERPRETER_PANIC) return; - } - - for (int i=0; i maxLuaDuration) { - maxLuaDuration = t0; - } -} -#endif - int luaGetMemUsed() { return (lua_gc(L, LUA_GCCOUNT, 0) << 10) + lua_gc(L, LUA_GCCOUNTB, 0); diff --git a/radio/src/lua_api.h b/radio/src/lua_api.h index 6df19f118..d3e041253 100644 --- a/radio/src/lua_api.h +++ b/radio/src/lua_api.h @@ -38,6 +38,12 @@ #define luaapi_h +#define RUN_MIX_SCRIPT (1 << 0) +#define RUN_FUNC_SCRIPT (1 << 1) +#define RUN_TELEM_BG_SCRIPT (1 << 2) +#define RUN_TELEM_FG_SCRIPT (1 << 3) +#define RUN_STNDAL_SCRIPT (1 << 4) + #if defined(LUA) struct ScriptInput { @@ -88,8 +94,7 @@ extern ScriptInternalData scriptInternalData[MAX_SCRIPTS]; extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS]; void luaClose(); - // void luaInit(); - // void luaTask(uint8_t evt); + bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage); void luaExec(const char *filename); int luaGetMemUsed(); #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions @@ -110,19 +115,15 @@ if (setjmp(lj.b) == 0) #define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */ - void luaRunNonGuiScripts(); - bool luaRunGuiScripts(uint8_t evt); - extern uint16_t maxLuaInterval; extern uint16_t maxLuaDuration; - - #else // #if defined(LUA) #define LUA_LOAD_MODEL_SCRIPTS() #define LUA_LOAD_MODEL_SCRIPT(idx) #define LUA_STANDALONE_SCRIPT_RUNNING() (0) #define luaRunNonGuiScripts() #define luaRunGuiScripts(evt) (false) + #define luaAllowLcdUsage(allow) #endif #endif // #ifndef luaapi_h diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index 406a0ff0f..df789d239 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -129,7 +129,7 @@ void perMain() #endif // run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) - luaRunNonGuiScripts(); + luaTask(0, RUN_MIX_SCRIPT | RUN_FUNC_SCRIPT | RUN_TELEM_BG_SCRIPT, false); // wait for LCD DMA to finish before continuing, because code from this point // is allowed to change the contents of LCD buffer @@ -139,10 +139,12 @@ void perMain() lcdWaitDmaEnd(); // draw LCD from menus or from Lua script - if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts - // let Lua manage LCD fully - } - else { + // run Lua scripts that use LCD + bool scriptWasRun = luaTask(evt, RUN_TELEM_FG_SCRIPT | RUN_STNDAL_SCRIPT, true); + + // TODO luaTask timing must be done here + + if (!scriptWasRun) { // normal GUI from menus const char *warn = s_warning; uint8_t menu = s_menu_count;