feat(lua): Add several Lua API funcs and constants (#2279)

* LUA: Add Bitmap.toMask and lcd.drawBitmapPattern functions

* LUA: Add lcd.invertRect function

* LUA: Add Bitmap.resize function

* LUA: Add getOutputValue function

* LUA: Add timer showElapsed configuration

* LUA: Add model extendedLimits configuration

* LUA: Add some global constants

* Add model jitterFilter configuration

* Add model.getSwashRing and setSwashRing functions
This commit is contained in:
David PHAM-VAN
2022-09-08 20:03:58 +10:00
committed by GitHub
parent ab640e3055
commit 155a854131
3 changed files with 327 additions and 6 deletions
+110 -6
View File
@@ -38,22 +38,28 @@ Get current Model information
@retval table model information:
* `name` (string) model name
* `extendedLimits` (boolean) extended limits enabled
* `jitterFilter` (number) model level ADC filter
* `bitmap` (string) bitmap name (not present on X7)
* `filename` (string) model filename
@status current Introduced in 2.0.6, changed in 2.2.0, filename added in 2.6.0
@status current Introduced in 2.0.6, changed in 2.2.0, filename added in 2.6.0, extendedLimits, jitterFilter, and labels added in 2.8.0
*/
static int luaModelGetInfo(lua_State *L)
{
lua_newtable(L);
lua_pushtablenstring(L, "name", g_model.header.name);
lua_pushtableboolean(L, "extendedLimits", g_model.extendedLimits);
lua_pushtableinteger(L, "jitterFilter", g_model.jitterFilter);
#if LCD_DEPTH > 1
lua_pushtablenstring(L, "bitmap", g_model.header.bitmap);
#endif
#if defined(STORAGE_MODELSLIST)
lua_pushtablenstring(L, "labels", g_model.header.labels);
lua_pushtablenstring(L, "filename", g_eeGeneral.currModelFilename);
#else
lua_pushtablenstring(L, "labels", "");
char fname[MODELIDX_STRLEN + sizeof(YAML_EXT)];
getModelNumberStr(g_eeGeneral.currModel, fname);
strcat(fname, YAML_EXT);
@@ -73,7 +79,7 @@ Set the current Model information
@notice If a parameter is missing from the value, then
that parameter remains unchanged.
@status current Introduced in 2.0.6, changed in TODO
@status current Introduced in 2.0.6, extendedLimits and jitterFilter added in 2.8.0
*/
static int luaModelSetInfo(lua_State *L)
{
@@ -88,6 +94,14 @@ static int luaModelSetInfo(lua_State *L)
memcpy(modelHeaders[g_eeGeneral.currModel].name, g_model.header.name, sizeof(g_model.header.name));
#endif
}
else if (!strcmp(key, "extendedLimits")) {
g_model.extendedLimits = lua_toboolean(L, -1);
}
else if (!strcmp(key, "jitterFilter")) {
auto j = lua_tounsigned(L, -1);
if (j > OVERRIDE_ON) j = OVERRIDE_ON;
g_model.jitterFilter = j;
}
#if LCD_DEPTH > 1
else if (!strcmp(key, "bitmap")) {
const char * name = luaL_checkstring(L, -1);
@@ -260,8 +274,9 @@ Get model timer parameters
* `minuteBeep` (boolean) minute beep
* `persistent` (number) persistent timer
* `name` (string) timer name
* `showElapsed` (boolean) show elapsed
@status current Introduced in 2.0.0, name added in 2.3.6
@status current Introduced in 2.0.0, name added in 2.3.6, showElapsed added in 2.8.0
*/
static int luaModelGetTimer(lua_State *L)
{
@@ -276,6 +291,7 @@ static int luaModelGetTimer(lua_State *L)
lua_pushtableboolean(L, "minuteBeep", timer.minuteBeep);
lua_pushtableinteger(L, "persistent", timer.persistent);
lua_pushtablenstring(L, "name", timer.name);
lua_pushtableboolean(L, "showElapsed", timer.showElapsed);
}
else {
lua_pushnil(L);
@@ -295,7 +311,7 @@ Set model timer parameters
@notice If a parameter is missing from the value, then
that parameter remains unchanged.
@status current Introduced in 2.0.0, name added in 2.3.6
@status current Introduced in 2.0.0, name added in 2.3.6, showElapsed added in 2.8.0
*/
static int luaModelSetTimer(lua_State *L)
{
@@ -325,10 +341,13 @@ static int luaModelSetTimer(lua_State *L)
else if (!strcmp(key, "persistent")) {
timer.persistent = luaL_checkinteger(L, -1);
}
if (!strcmp(key, "name")) {
else if (!strcmp(key, "name")) {
const char * name = luaL_checkstring(L, -1);
strncpy(timer.name, name, sizeof(timer.name));
}
else if (!strcmp(key, "showElapsed")) {
timer.showElapsed = lua_toboolean(L, -1);
}
}
storageDirty(EE_MODEL);
}
@@ -1616,6 +1635,87 @@ static int luaModelResetSensor(lua_State *L)
return 1;
}
#if defined(HELI)
/*luadoc
@function model.getSwashRing()
Get heli swash parameters
@retval table with heli swash parameters:
* `type` (number) 0=---, 1=120, 2=120X, 3=140, 4=90
* `value` (number) swash ring value (normally 0)
* 'collectiveSource' (number) source index
* 'aileronSource' (number) source index
* 'elevatorSource' (number) source index
* 'collectiveWeight'(value) -100 to 100
* 'aileronWeight' (value) -100 to 100
* 'elevatorWeight' (value) -100 to 100
@status current Introduced in 2.8.0
*/
static int luaModelGetSwashRing(lua_State *L)
{
lua_newtable(L);
lua_pushtableinteger(L, "type", g_model.swashR.type);
lua_pushtableinteger(L, "value", g_model.swashR.value);
lua_pushtableinteger(L, "collectiveSource", g_model.swashR.collectiveSource);
lua_pushtableinteger(L, "aileronSource", g_model.swashR.aileronSource);
lua_pushtableinteger(L, "elevatorSource", g_model.swashR.elevatorSource);
lua_pushtableinteger(L, "collectiveWeight", g_model.swashR.collectiveWeight);
lua_pushtableinteger(L, "aileronWeight", g_model.swashR.aileronWeight);
lua_pushtableinteger(L, "elevatorWeight", g_model.swashR.elevatorWeight);
return 1;
}
/*luadoc
@function model.setSwashRing(params)
Set heli swash parameters
@param value (table) swash ring parameters, see model.getSwashRing() for table format
@notice If a parameter is missing, then that parameter remains unchanged.
@status current Introduced in 2.8.0
*/
static int luaModelSetSwashRing(lua_State *L)
{
luaL_checktype(L, -1, LUA_TTABLE);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
luaL_checktype(L, -2, LUA_TSTRING); // key is string
const char * key = luaL_checkstring(L, -2);
if (!strcmp(key, "type")) {
g_model.swashR.type = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "value")) {
g_model.swashR.value = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "collectiveSource")) {
g_model.swashR.collectiveSource = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "aileronSource")) {
g_model.swashR.aileronSource = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "elevatorSource")) {
g_model.swashR.elevatorSource = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "collectiveWeight")) {
g_model.swashR.collectiveWeight = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "aileronWeight")) {
g_model.swashR.aileronWeight = luaL_checkinteger(L, -1);
}
else if (!strcmp(key, "elevatorWeight")) {
g_model.swashR.elevatorWeight = luaL_checkinteger(L, -1);
}
}
storageDirty(EE_MODEL);
return 0;
}
#endif // HELI
const luaL_Reg modelLib[] = {
{ "getInfo", luaModelGetInfo },
{ "setInfo", luaModelSetInfo },
@@ -1652,5 +1752,9 @@ const luaL_Reg modelLib[] = {
#endif
{ "getSensor", luaModelGetSensor },
{ "resetSensor", luaModelResetSensor },
{ NULL, NULL } /* sentinel */
#if defined(HELI)
{ "getSwashRing", luaModelGetSwashRing },
{ "setSwashRing", luaModelSetSwashRing },
#endif
{ nullptr, nullptr } /* sentinel */
};