Improve multi (#7136)
[opentx.git] / radio / src / lua / lua_api.h
blob71a1cef01458a4587d0c661902f2b1e4c7369981
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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.
21 #ifndef _LUA_API_H_
22 #define _LUA_API_H_
24 #if defined(LUA)
26 extern "C" {
27 #include <lua.h>
28 #include <lauxlib.h>
29 #include <lualib.h>
30 #include <lrotable.h>
31 #include <lgc.h>
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
39 #else
40 #define LUA_SCRIPT_LOAD_MODE "bt" // binary or text, whichever is newer
41 #endif
42 #endif
44 extern lua_State * lsScripts;
45 extern lua_State * lsWidgets;
46 extern bool luaLcdAllowed;
47 #if defined(COLORLCD)
48 extern uint32_t luaExtraMemoryUsage;
49 #endif
51 void luaInit();
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 {
73 INPUT_TYPE_FIRST = 0,
74 INPUT_TYPE_VALUE = INPUT_TYPE_FIRST,
75 INPUT_TYPE_SOURCE,
76 INPUT_TYPE_LAST = INPUT_TYPE_SOURCE
79 struct ScriptInput {
80 const char *name;
81 uint8_t type;
82 int16_t min;
83 int16_t max;
84 int16_t def;
86 struct ScriptOutput {
87 const char *name;
88 int16_t value;
90 enum ScriptState {
91 SCRIPT_OK,
92 SCRIPT_NOFILE,
93 SCRIPT_SYNTAX_ERROR,
94 SCRIPT_PANIC,
95 SCRIPT_KILLED
97 enum ScriptReference {
98 SCRIPT_MIX_FIRST,
99 SCRIPT_MIX_LAST=SCRIPT_MIX_FIRST+MAX_SCRIPTS-1,
100 SCRIPT_FUNC_FIRST,
101 SCRIPT_FUNC_LAST=SCRIPT_FUNC_FIRST+MAX_SPECIAL_FUNCTIONS-1, // model functions
102 SCRIPT_GFUNC_FIRST,
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 {
108 uint8_t reference;
109 uint8_t state;
110 int run;
111 int background;
112 uint8_t instructions;
114 struct ScriptInputsOutputs {
115 uint8_t inputsCount;
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
141 #include <setjmp.h>
142 struct our_longjmp {
143 struct our_longjmp *previous;
144 jmp_buf b;
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 */ \
150 global_lj = &lj; \
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))
160 #else
161 #define IS_MASKABLE(key) ((key) != KEY_EXIT && (key) != KEY_ENTER)
162 #endif
163 struct LuaField {
164 uint16_t id;
165 char desc[50];
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);
174 #if LCD_W > 350
175 #define RADIO_TOOL_NAME_MAXLEN 40
176 #else
177 #define RADIO_TOOL_NAME_MAXLEN 16
178 #endif
180 bool readToolName(char * toolName, const char * filename);
181 bool isRadioScriptTool(const char * filename);
183 struct LuaMemTracer {
184 const char * script;
185 int lineno;
186 uint32_t alloc;
187 uint32_t free;
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)
196 #define luaInit()
197 #define LUA_INIT_THEMES_AND_WIDGETS()
198 #define LUA_LOAD_MODEL_SCRIPTS()
200 #endif // defined(LUA)
202 #endif // _LUA_API_H_