From 8f3dd7f72e878607ab28af2b0c8ccca6e835fdd5 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Wed, 30 Nov 2022 06:57:42 +0100 Subject: [PATCH] fix(lua): Array indexes for get/setFlightMode and getCurve (#2567) * Lua array indexes start at 1 * LUA model.setFlightMode: use array index to compute trim index This allows for providing an incomplete table like this: ``` -- Disable T5 & T6 on FM0 modes = {} modes[5] = 31 modes[6] = 31 t = {} t["trimsModes"] = modes model.setFlightMode(0, t) ``` * Remove warning & cleanup * Fix trim value constraints in model.setFlightMode() --- radio/src/lua/api_general.cpp | 4 ++-- radio/src/lua/api_model.cpp | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/radio/src/lua/api_general.cpp b/radio/src/lua/api_general.cpp index 3ea74968e..d2231f3b6 100644 --- a/radio/src/lua/api_general.cpp +++ b/radio/src/lua/api_general.cpp @@ -1187,7 +1187,7 @@ static int luaGhostTelemetryPop(lua_State * L) lua_newtable(L); for (uint8_t i=0; ipop(data); - lua_pushinteger(L, i+1); + lua_pushinteger(L, i + 1); lua_pushinteger(L, data); lua_settable(L, -3); } @@ -2587,7 +2587,7 @@ static int luaSources(lua_State * L) static int luaGetOutputValue(lua_State * L) { mixsrc_t idx = luaL_checkinteger(L, 1); - if (idx >= 0 && idx < MAX_OUTPUT_CHANNELS) { + if (idx < MAX_OUTPUT_CHANNELS) { lua_pushinteger(L, channelOutputs[idx]); } else { lua_pushinteger(L, 0); diff --git a/radio/src/lua/api_model.cpp b/radio/src/lua/api_model.cpp index 00af53270..842fb8613 100644 --- a/radio/src/lua/api_model.cpp +++ b/radio/src/lua/api_model.cpp @@ -467,7 +467,7 @@ static int luaModelGetFlightMode(lua_State * L) lua_pushstring(L, "trimsValues"); lua_newtable(L); for (uint8_t i = 0; i < NUM_TRIMS; i++) { - lua_pushinteger(L, i); + lua_pushinteger(L, i + 1); lua_pushinteger(L, fm->trim[i].value); lua_settable(L, -3); } @@ -475,7 +475,7 @@ static int luaModelGetFlightMode(lua_State * L) lua_pushstring(L, "trimsModes"); lua_newtable(L); for (uint8_t i = 0; i < NUM_TRIMS; i++) { - lua_pushinteger(L, i); + lua_pushinteger(L, i + 1); lua_pushinteger(L, fm->trim[i].mode); lua_settable(L, -3); } @@ -526,17 +526,23 @@ static int luaModelSetFlightMode(lua_State * L) } else if (!strcmp(key, "trimsValues")) { luaL_checktype(L, -1, LUA_TTABLE); - uint8_t idx = 0; - for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1), idx++) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + int idx = luaL_checkinteger(L, -2) - 1; // key is integer + if (idx < 0 || idx >= NUM_TRIMS) continue; int16_t val = luaL_checkinteger(L, -1); + if (g_model.extendedTrims) + val = limit(val, TRIM_EXTENDED_MIN, TRIM_EXTENDED_MAX); + else + val = limit(val, TRIM_MIN, TRIM_MAX); if (idx < NUM_TRIMS) - fm->trim[idx].value = (val & 0x3FF); + fm->trim[idx].value = val; } } else if (!strcmp(key, "trimsModes")) { luaL_checktype(L, -1, LUA_TTABLE); - uint8_t idx = 0; - for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1), idx++) { + for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { + int idx = luaL_checkinteger(L, -2) - 1; // key is integer + if (idx < 0 || idx >= NUM_TRIMS) continue; uint16_t val = luaL_checkinteger(L, -1); if (idx < NUM_TRIMS) fm->trim[idx].mode = (val & 0x1F); @@ -1087,7 +1093,7 @@ static int luaModelGetCurve(lua_State *L) lua_newtable(L); int8_t * point = curveAddress(idx); for (int i=0; i < CurveHeader.points + 5; i++) { - lua_pushinteger(L, i); + lua_pushinteger(L, i + 1); lua_pushinteger(L, *point++); lua_settable(L, -3); } @@ -1095,15 +1101,15 @@ static int luaModelGetCurve(lua_State *L) if (CurveHeader.type == CURVE_TYPE_CUSTOM) { lua_pushstring(L, "x"); lua_newtable(L); - lua_pushinteger(L, 0); + lua_pushinteger(L, 1); lua_pushinteger(L, -100); lua_settable(L, -3); for (int i=0; i < CurveHeader.points + 3; i++) { - lua_pushinteger(L, i+1); + lua_pushinteger(L, i + 2); lua_pushinteger(L, *point++); lua_settable(L, -3); } - lua_pushinteger(L, CurveHeader.points + 4); + lua_pushinteger(L, CurveHeader.points + 5); lua_pushinteger(L, 100); lua_settable(L, -3); lua_settable(L, -3); @@ -1198,7 +1204,7 @@ static int luaModelSetCurve(lua_State *L) bool isX = !strcmp(key, "x"); for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { - int idx = luaL_checkinteger(L, -2) - 1; + int idx = luaL_checkinteger(L, -2) - 1; // key is integer if (idx < 0 || idx > MAX_POINTS_PER_CURVE) { lua_pushinteger(L, 4); return 1;