From 30780d82ec4af1b5f6447f9184d2ec7f447ff8e5 Mon Sep 17 00:00:00 2001 From: Jesper Frickmann Date: Sat, 13 Nov 2021 00:10:33 -0500 Subject: [PATCH] [lua] Add shared memory variables which can be accessed from Lua widgets and other Lua scripts (#1018) * Issue #132 Added 16 shared memory variables which can be accessed bot from Lua widgets and other Lua scripts. * Notice Only available on radios with color display --- radio/src/lua/api_general.cpp | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/radio/src/lua/api_general.cpp b/radio/src/lua/api_general.cpp index 4f17b96ff..939991430 100644 --- a/radio/src/lua/api_general.cpp +++ b/radio/src/lua/api_general.cpp @@ -1789,6 +1789,61 @@ static int luaSerialRead(lua_State * L) return 1; } +#if defined(COLORLCD) +static int shmVar[16] = {0}; + +/*luadoc +@function setShmVar(id, value) + +@param id: integer between 1 and 16 identifying the shared memory variable. + +@param value: integer. The value of the shared memory variable. + +Sets the value of a shared memory variable that can be used for passing data between Lua widgets and other Lua scripts. + +@status current Introduced in 2.6 + +@notice Only available on radios with color display +*/ + +static int luaSetShmVar(lua_State * L) +{ + int id = luaL_checkinteger(L, 1); + int value = luaL_checkinteger(L, 2); + + if (1 <= id && id <= 16) + shmVar[id - 1] = value; + + return 0; +} + +/*luadoc +@function getShmVar(id) + +@param id: integer between 1 and 16 identifying the shared memory variable. + +@retval value: integer. The value of the shared memory variable. + +Gets the value of a shared memory variable that can be used for passing data between Lua widgets and other Lua scripts. + +@status current Introduced in 2.6 + +@notice Only available on radios with color display +*/ + +static int luaGetShmVar(lua_State * L) +{ + int id = luaL_checkinteger(L, 1); + + if (1 <= id && id <= 16) + lua_pushinteger(L, shmVar[id - 1]); + else + lua_pushnil(L); + + return 1; +} +#endif + const luaL_Reg opentxLib[] = { { "getTime", luaGetTime }, { "getDateTime", luaGetDateTime }, @@ -1844,6 +1899,10 @@ const luaL_Reg opentxLib[] = { { "setSerialBaudrate", luaSetSerialBaudrate }, { "serialWrite", luaSerialWrite }, { "serialRead", luaSerialRead }, +#if defined(COLORLCD) + { "setShmVar", luaSetShmVar }, + { "getShmVar", luaGetShmVar }, +#endif { nullptr, nullptr } /* sentinel */ };