1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-23 16:25:16 +03:00

Refactored luaTask() based on script type

This commit is contained in:
Damjan Adamic 2014-12-07 17:05:16 +01:00
parent 6f46ef23da
commit 62f2a321f2
3 changed files with 31 additions and 138 deletions

View file

@ -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]; ScriptInternalData & sid = scriptInternalData[i];
if (sid.state != SCRIPT_OK) return false; if (sid.state != SCRIPT_OK) return false;
@ -1843,11 +1843,10 @@ bool luaDoOneRunPermanentScript(uint8_t evt, int i, bool runGuiScripts)
#endif #endif
ScriptInputsOutputs * sio = NULL; ScriptInputsOutputs * sio = NULL;
#if SCRIPT_MIX_FIRST > 0 #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 #else
if (sid.reference <= SCRIPT_MIX_LAST) { if ((scriptType & RUN_MIX_SCRIPT) && (sid.reference <= SCRIPT_MIX_LAST)) {
#endif #endif
if (runGuiScripts) return false; //mixer script is not gui script
ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST]; ScriptData & sd = g_model.scriptsData[sid.reference-SCRIPT_MIX_FIRST];
sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST]; sio = &scriptInputsOutputs[sid.reference-SCRIPT_MIX_FIRST];
inputsCount = sio->inputsCount; 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); 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]; 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 (!getSwitch(fn.swtch)) return false;
#if defined(SIMU) || defined(DEBUG) #if defined(SIMU) || defined(DEBUG)
filename = fn.play.name; 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; TelemetryScriptData & script = g_model.frsky.screens[sid.reference-SCRIPT_TELEMETRY_FIRST].script;
filename = script.file; filename = script.file;
#endif #endif
if (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view) { if ((scriptType & RUN_TELEM_FG_SCRIPT) &&
if (!runGuiScripts) return false; //telemetry script gui part, do not run this time (g_menuStack[0]==menuTelemetryFrsky && sid.reference==SCRIPT_TELEMETRY_FIRST+s_frsky_view)) {
lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run); lua_rawgeti(L, LUA_REGISTRYINDEX, sid.run);
lua_pushinteger(L, evt); lua_pushinteger(L, evt);
inputsCount = 1; inputsCount = 1;
} }
else if (sid.background) { else if ((scriptType & RUN_TELEM_BG_SCRIPT) && (sid.background)) {
if (runGuiScripts) return false; //telemetry script non-gui part, do not run this time
lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background); lua_rawgeti(L, LUA_REGISTRYINDEX, sid.background);
} }
else { else {
@ -1948,61 +1945,16 @@ void luaDoGc()
} }
} }
bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage)
uint32_t lastRunTime;
void luaRunNonGuiScripts()
{ {
//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<luaScriptsCount; i++) {
PROTECT_LUA() {
luaDoOneRunPermanentScript(0, i, false);
}
else {
luaDisable();
return;
}
UNPROTECT_LUA();
//todo gc step between scripts
}
luaDoGc();
lastRunTime = get_tmr10ms() - t0;
}
bool luaRunGuiScripts(uint8_t evt)
{
//execute one cycle of Lua scripts that use LCD
//either stand-alone or other (telemetry scripts)
uint32_t t0 = get_tmr10ms();
luaLcdAllowed = true;
if (luaState == INTERPRETER_PANIC) return false; if (luaState == INTERPRETER_PANIC) return false;
luaLcdAllowed = allowLcdUsage;
bool scriptWasRun = false; bool scriptWasRun = false;
// we run either standalone script or permanent scripts
if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) { if (luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) {
// run standalone script
if ((scriptType & RUN_STNDAL_SCRIPT) == 0) return false;
PROTECT_LUA() { PROTECT_LUA() {
luaDoOneRunStandalone(evt); luaDoOneRunStandalone(evt);
scriptWasRun = true; scriptWasRun = true;
@ -2014,7 +1966,7 @@ bool luaRunGuiScripts(uint8_t evt)
UNPROTECT_LUA(); UNPROTECT_LUA();
} }
else { else {
// model scripts // run permanent scripts
if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) { if (luaState & INTERPRETER_RELOAD_PERMANENT_SCRIPTS) {
luaState = 0; luaState = 0;
luaInit(); luaInit();
@ -2025,82 +1977,20 @@ bool luaRunGuiScripts(uint8_t evt)
for (int i=0; i<luaScriptsCount; i++) { for (int i=0; i<luaScriptsCount; i++) {
PROTECT_LUA() { PROTECT_LUA() {
scriptWasRun |= luaDoOneRunPermanentScript(evt, i, true); scriptWasRun |= luaDoOneRunPermanentScript(evt, i, scriptType);
} }
else { else {
luaDisable(); luaDisable();
return false; break;
} }
UNPROTECT_LUA(); UNPROTECT_LUA();
//todo gc step between scripts //todo gc step between scripts
} }
} }
luaDoGc(); luaDoGc();
lastRunTime += get_tmr10ms() - t0;
if (lastRunTime > maxLuaDuration) {
maxLuaDuration = lastRunTime;
}
return scriptWasRun; 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<luaScriptsCount; i++) {
PROTECT_LUA() {
luaDoOneRunPermanentScript(evt, i);
}
else {
luaDisable();
return;
}
UNPROTECT_LUA();
}
}
luaDoGc();
t0 = get_tmr10ms() - t0;
if (t0 > maxLuaDuration) {
maxLuaDuration = t0;
}
}
#endif
int luaGetMemUsed() int luaGetMemUsed()
{ {
return (lua_gc(L, LUA_GCCOUNT, 0) << 10) + lua_gc(L, LUA_GCCOUNTB, 0); return (lua_gc(L, LUA_GCCOUNT, 0) << 10) + lua_gc(L, LUA_GCCOUNTB, 0);

View file

@ -38,6 +38,12 @@
#define luaapi_h #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) #if defined(LUA)
struct ScriptInput { struct ScriptInput {
@ -88,8 +94,7 @@
extern ScriptInternalData scriptInternalData[MAX_SCRIPTS]; extern ScriptInternalData scriptInternalData[MAX_SCRIPTS];
extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS]; extern ScriptInputsOutputs scriptInputsOutputs[MAX_SCRIPTS];
void luaClose(); void luaClose();
// void luaInit(); bool luaTask(uint8_t evt, uint8_t scriptType, bool allowLcdUsage);
// void luaTask(uint8_t evt);
void luaExec(const char *filename); void luaExec(const char *filename);
int luaGetMemUsed(); int luaGetMemUsed();
#define luaGetCpuUsed(idx) scriptInternalData[idx].instructions #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions
@ -110,19 +115,15 @@
if (setjmp(lj.b) == 0) if (setjmp(lj.b) == 0)
#define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */ #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 maxLuaInterval;
extern uint16_t maxLuaDuration; extern uint16_t maxLuaDuration;
#else // #if defined(LUA) #else // #if defined(LUA)
#define LUA_LOAD_MODEL_SCRIPTS() #define LUA_LOAD_MODEL_SCRIPTS()
#define LUA_LOAD_MODEL_SCRIPT(idx) #define LUA_LOAD_MODEL_SCRIPT(idx)
#define LUA_STANDALONE_SCRIPT_RUNNING() (0) #define LUA_STANDALONE_SCRIPT_RUNNING() (0)
#define luaRunNonGuiScripts() #define luaRunNonGuiScripts()
#define luaRunGuiScripts(evt) (false) #define luaRunGuiScripts(evt) (false)
#define luaAllowLcdUsage(allow)
#endif #endif
#endif // #ifndef luaapi_h #endif // #ifndef luaapi_h

View file

@ -129,7 +129,7 @@ void perMain()
#endif #endif
// run Lua scripts that don't use LCD (to use CPU time while LCD DMA is running) // 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 // wait for LCD DMA to finish before continuing, because code from this point
// is allowed to change the contents of LCD buffer // is allowed to change the contents of LCD buffer
@ -139,10 +139,12 @@ void perMain()
lcdWaitDmaEnd(); lcdWaitDmaEnd();
// draw LCD from menus or from Lua script // draw LCD from menus or from Lua script
if (luaRunGuiScripts(evt)) { // either stand-alone or telemetry scripts // run Lua scripts that use LCD
// let Lua manage LCD fully bool scriptWasRun = luaTask(evt, RUN_TELEM_FG_SCRIPT | RUN_STNDAL_SCRIPT, true);
}
else { // TODO luaTask timing must be done here
if (!scriptWasRun) {
// normal GUI from menus // normal GUI from menus
const char *warn = s_warning; const char *warn = s_warning;
uint8_t menu = s_menu_count; uint8_t menu = s_menu_count;