From ba1073ace64d8548b35cda52661bd3e93939f102 Mon Sep 17 00:00:00 2001 From: Max Paperno Date: Thu, 30 Nov 2017 13:10:11 -0500 Subject: [PATCH] [Lua] Allow global functions to play scripts. (#5440) --- radio/src/lua/interface.cpp | 14 +++++++------- radio/src/lua/lua_api.h | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/radio/src/lua/interface.cpp b/radio/src/lua/interface.cpp index 838f50a49..024f565f8 100644 --- a/radio/src/lua/interface.cpp +++ b/radio/src/lua/interface.cpp @@ -112,7 +112,7 @@ void luaHook(lua_State * L, lua_Debug *ar) else if (instructionsPercent < 10) { max = 0; } -#else +#else if (instructionsPercent > 100) { // From now on, as soon as a line is executed, error // keep erroring until you're script reaches the top @@ -631,14 +631,14 @@ bool luaLoadMixScript(uint8_t index) return true; } -bool luaLoadFunctionScript(uint8_t index) +bool luaLoadFunctionScript(uint8_t index, uint8_t ref) { - CustomFunctionData & fn = g_model.customFn[index]; + CustomFunctionData & fn = (ref < SCRIPT_GFUNC_FIRST ? g_model.customFn[index] : g_eeGeneral.customFn[index]); if (fn.func == FUNC_PLAY_SCRIPT && ZEXIST(fn.play.name)) { if (luaScriptsCount < MAX_SCRIPTS) { ScriptInternalData & sid = scriptInternalData[luaScriptsCount++]; - sid.reference = SCRIPT_FUNC_FIRST+index; + sid.reference = ref + index; sid.state = SCRIPT_NOFILE; char filename[sizeof(SCRIPTS_FUNCS_PATH)+sizeof(fn.play.name)+sizeof(SCRIPT_EXT)] = SCRIPTS_FUNCS_PATH "/"; strncpy(filename+sizeof(SCRIPTS_FUNCS_PATH), fn.play.name, sizeof(fn.play.name)); @@ -712,7 +712,7 @@ void luaLoadPermanentScripts() // Load custom function scripts for (int i=0; iinputs[j].def); } } - 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]; + else if ((scriptType & RUN_FUNC_SCRIPT) && (sid.reference >= SCRIPT_FUNC_FIRST && sid.reference <= SCRIPT_GFUNC_LAST)) { + CustomFunctionData & fn = (sid.reference < SCRIPT_GFUNC_FIRST ? g_model.customFn[sid.reference-SCRIPT_FUNC_FIRST] : g_eeGeneral.customFn[sid.reference-SCRIPT_GFUNC_FIRST]); #if defined(SIMU) || defined(DEBUG) filename = fn.play.name; #endif diff --git a/radio/src/lua/lua_api.h b/radio/src/lua/lua_api.h index 9e3287efc..08a936d83 100644 --- a/radio/src/lua/lua_api.h +++ b/radio/src/lua/lua_api.h @@ -98,7 +98,9 @@ enum ScriptReference { SCRIPT_MIX_FIRST, SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1, SCRIPT_FUNC_FIRST, - SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, + SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // model functions + SCRIPT_GFUNC_FIRST, + SCRIPT_GFUNC_LAST=SCRIPT_GFUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // global functions SCRIPT_TELEMETRY_FIRST, SCRIPT_TELEMETRY_LAST=SCRIPT_TELEMETRY_FIRST+MAX_SCRIPTS, // telem0 and telem1 .. telem7 }; -- 2.11.4.GIT