5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
34 #ifndef LUA_SCRIPT_LOAD_MODE
35 // Can force loading of binary (.luac) or plain-text (.lua) versions of scripts specifically, and control
36 // compilation options. See interface.cpp:luaLoadScriptFileToState() <mode> parameter description for details.
37 #if !defined(LUA_COMPILER) || defined(SIMU) || defined(DEBUG)
38 #define LUA_SCRIPT_LOAD_MODE "T" // prefer loading .lua source file for full debug info
40 #define LUA_SCRIPT_LOAD_MODE "bt" // binary or text, whichever is newer
44 extern lua_State
* lsScripts
;
45 extern lua_State
* lsWidgets
;
46 extern bool luaLcdAllowed
;
48 extern uint32_t luaExtraMemoryUsage
;
52 void luaInitThemesAndWidgets();
53 #define LUA_INIT_THEMES_AND_WIDGETS() luaInitThemesAndWidgets()
55 #define lua_registernumber(L, n, i) (lua_pushnumber(L, (i)), lua_setglobal(L, (n)))
56 #define lua_registerint(L, n, i) (lua_pushinteger(L, (i)), lua_setglobal(L, (n)))
57 #define lua_pushtablenil(L, k) (lua_pushstring(L, (k)), lua_pushnil(L), lua_settable(L, -3))
58 #define lua_pushtableboolean(L, k, v) (lua_pushstring(L, (k)), lua_pushboolean(L, (v)), lua_settable(L, -3))
59 #define lua_pushtableinteger(L, k, v) (lua_pushstring(L, (k)), lua_pushinteger(L, (v)), lua_settable(L, -3))
60 #define lua_pushtablenumber(L, k, v) (lua_pushstring(L, (k)), lua_pushnumber(L, (v)), lua_settable(L, -3))
61 #define lua_pushtablestring(L, k, v) (lua_pushstring(L, (k)), lua_pushstring(L, (v)), lua_settable(L, -3))
62 #define lua_pushtablenzstring(L, k, v) { char tmp[sizeof(v)+1]; strncpy(tmp, (v), sizeof(tmp)-1); tmp[sizeof(v)] = '\0'; lua_pushstring(L, (k)); lua_pushstring(L, tmp); lua_settable(L, -3); }
63 #define lua_pushtablezstring(L, k, v) { char tmp[sizeof(v)+1]; zchar2str(tmp, (v), sizeof(tmp)-1); lua_pushstring(L, (k)); lua_pushstring(L, tmp); lua_settable(L, -3); }
64 #define lua_registerlib(L, name, tab) (luaL_newmetatable(L, name), luaL_setfuncs(L, tab, 0), lua_setglobal(L, name))
66 #define RUN_MIX_SCRIPT (1 << 0)
67 #define RUN_FUNC_SCRIPT (1 << 1)
68 #define RUN_TELEM_BG_SCRIPT (1 << 2)
69 #define RUN_TELEM_FG_SCRIPT (1 << 3)
70 #define RUN_STNDAL_SCRIPT (1 << 4)
72 enum luaScriptInputType
{
74 INPUT_TYPE_VALUE
= INPUT_TYPE_FIRST
,
76 INPUT_TYPE_LAST
= INPUT_TYPE_SOURCE
97 enum ScriptReference
{
99 SCRIPT_MIX_LAST
=SCRIPT_MIX_FIRST
+MAX_SCRIPTS
-1,
101 SCRIPT_FUNC_LAST
=SCRIPT_FUNC_FIRST
+MAX_SPECIAL_FUNCTIONS
-1, // model functions
103 SCRIPT_GFUNC_LAST
=SCRIPT_GFUNC_FIRST
+MAX_SPECIAL_FUNCTIONS
-1, // global functions
104 SCRIPT_TELEMETRY_FIRST
,
105 SCRIPT_TELEMETRY_LAST
=SCRIPT_TELEMETRY_FIRST
+MAX_SCRIPTS
, // telem0 and telem1 .. telem7
107 struct ScriptInternalData
{
112 uint8_t instructions
;
114 struct ScriptInputsOutputs
{
116 ScriptInput inputs
[MAX_SCRIPT_INPUTS
];
117 uint8_t outputsCount
;
118 ScriptOutput outputs
[MAX_SCRIPT_OUTPUTS
];
120 #define INTERPRETER_RUNNING_STANDALONE_SCRIPT 1
121 #define INTERPRETER_RELOAD_PERMANENT_SCRIPTS 2
122 #define INTERPRETER_PANIC 255
123 extern uint8_t luaState
;
124 extern uint8_t luaScriptsCount
;
125 extern ScriptInternalData standaloneScript
;
126 extern ScriptInternalData scriptInternalData
[MAX_SCRIPTS
];
127 extern ScriptInputsOutputs scriptInputsOutputs
[MAX_SCRIPTS
];
128 void luaClose(lua_State
** L
);
129 bool luaTask(event_t evt
, uint8_t scriptType
, bool allowLcdUsage
);
130 void checkLuaMemoryUsage();
131 void luaExec(const char * filename
);
132 void luaDoGc(lua_State
* L
, bool full
);
133 void luaError(lua_State
* L
, uint8_t error
, bool acknowledge
=true);
134 uint32_t luaGetMemUsed(lua_State
* L
);
135 void luaGetValueAndPush(lua_State
* L
, int src
);
136 #define luaGetCpuUsed(idx) scriptInternalData[idx].instructions
137 uint8_t isTelemetryScriptAvailable(uint8_t index
);
138 #define LUA_LOAD_MODEL_SCRIPTS() luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS
139 #define LUA_LOAD_MODEL_SCRIPT(idx) luaState |= INTERPRETER_RELOAD_PERMANENT_SCRIPTS
140 // Lua PROTECT/UNPROTECT
143 struct our_longjmp
*previous
;
145 volatile int status
; /* error code */
147 extern struct our_longjmp
* global_lj
;
148 #define PROTECT_LUA() { struct our_longjmp lj; \
149 lj.previous = global_lj; /* chain new error handler */ \
151 if (setjmp(lj.b) == 0)
152 #define UNPROTECT_LUA() global_lj = lj.previous; } /* restore old error handler */
154 extern uint16_t maxLuaInterval
;
155 extern uint16_t maxLuaDuration
;
156 extern uint8_t instructionsPercent
;
158 #if defined(KEYS_GPIO_REG_PAGE)
159 #define IS_MASKABLE(key) ((key) != KEY_EXIT && (key) != KEY_ENTER && ((luaState & INTERPRETER_RUNNING_STANDALONE_SCRIPT) || (key) != KEY_PAGE))
161 #define IS_MASKABLE(key) ((key) != KEY_EXIT && (key) != KEY_ENTER)
167 bool luaFindFieldByName(const char * name
, LuaField
& field
, unsigned int flags
=0);
168 void luaLoadThemes();
169 void luaRegisterLibraries(lua_State
* L
);
170 void registerBitmapClass(lua_State
* L
);
171 void luaSetInstructionsLimit(lua_State
* L
, int count
);
172 int luaLoadScriptFileToState(lua_State
* L
, const char * filename
, const char * mode
);
175 #define RADIO_TOOL_NAME_MAXLEN 40
177 #define RADIO_TOOL_NAME_MAXLEN 16
180 bool readToolName(char * toolName
, const char * filename
);
181 bool isRadioScriptTool(const char * filename
);
183 struct LuaMemTracer
{
190 void * tracer_alloc(void * ud
, void * ptr
, size_t osize
, size_t nsize
);
191 void luaHook(lua_State
* L
, lua_Debug
*ar
);
194 #else // defined(LUA)
197 #define LUA_INIT_THEMES_AND_WIDGETS()
198 #define LUA_LOAD_MODEL_SCRIPTS()
200 #endif // defined(LUA)
202 #endif // _LUA_API_H_