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()
This commit is contained in:
Raphael Coeffic
2022-11-30 15:57:42 +10:00
committed by GitHub
parent a267520236
commit 8f3dd7f72e
2 changed files with 20 additions and 14 deletions
+2 -2
View File
@@ -1187,7 +1187,7 @@ static int luaGhostTelemetryPop(lua_State * L)
lua_newtable(L);
for (uint8_t i=0; i<length-2; i++) {
luaInputTelemetryFifo->pop(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);
+18 -12
View File
@@ -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<int16_t>(val, TRIM_EXTENDED_MIN, TRIM_EXTENDED_MAX);
else
val = limit<int16_t>(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;