1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-24 16:55:20 +03:00

Fixes #2084: Lua model.insertInput() could not add a line to an empty input or at the end of the existing input lines

Also added unsigned to various vars (ported from master)
This commit is contained in:
Damjan Adamic 2015-02-18 20:24:15 +01:00
parent ae16191e76
commit 2b8374f418

View file

@ -614,7 +614,7 @@ static int luaModelSetInfo(lua_State *L)
static int luaModelGetTimer(lua_State *L) static int luaModelGetTimer(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_TIMERS) { if (idx < MAX_TIMERS) {
TimerData & timer = g_model.timers[idx]; TimerData & timer = g_model.timers[idx];
lua_newtable(L); lua_newtable(L);
@ -633,7 +633,7 @@ static int luaModelGetTimer(lua_State *L)
static int luaModelSetTimer(lua_State *L) static int luaModelSetTimer(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_TIMERS) { if (idx < MAX_TIMERS) {
TimerData & timer = g_model.timers[idx]; TimerData & timer = g_model.timers[idx];
@ -667,47 +667,43 @@ static int luaModelSetTimer(lua_State *L)
static int luaModelResetTimer(lua_State *L) static int luaModelResetTimer(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_TIMERS) { if (idx < MAX_TIMERS) {
timerReset(idx); timerReset(idx);
} }
return 0; return 0;
} }
static int getFirstInput(int chn) static unsigned int getFirstInput(unsigned int chn)
{ {
for (int i=0; i<MAX_INPUTS; i++) { for (unsigned int i=0; i<MAX_INPUTS; i++) {
ExpoData * expo = expoAddress(i); ExpoData * expo = expoAddress(i);
if (!expo->srcRaw || expo->chn>chn) break; if (!expo->srcRaw || expo->chn >= chn) {
if (expo->chn == chn) {
return i; return i;
} }
} }
return -1; return 0;
} }
static int getInputsCountFromFirst(int chn, int first) static unsigned int getInputsCountFromFirst(unsigned int chn, unsigned int first)
{ {
int count = 0; unsigned int count = 0;
if (first >= 0) { for (unsigned int i=first; i<MAX_INPUTS; i++) {
for (int i=first; i<MAX_INPUTS; i++) { ExpoData * expo = expoAddress(i);
ExpoData * expo = expoAddress(i); if (!expo->srcRaw || expo->chn!=chn) break;
if (!expo->srcRaw || expo->chn!=chn) break; count++;
count++;
}
} }
return count; return count;
} }
static int getInputsCount(int chn) static unsigned int getInputsCount(unsigned int chn)
{ {
int first = getFirstInput(chn); return getInputsCountFromFirst(chn, getFirstInput(chn));
return getInputsCountFromFirst(chn, first);
} }
static int luaModelGetInputsCount(lua_State *L) static int luaModelGetInputsCount(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int count = getInputsCount(chn); int count = getInputsCount(chn);
lua_pushinteger(L, count); lua_pushinteger(L, count);
return 1; return 1;
@ -715,11 +711,11 @@ static int luaModelGetInputsCount(lua_State *L)
static int luaModelGetInput(lua_State *L) static int luaModelGetInput(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstInput(chn); unsigned int first = getFirstInput(chn);
int count = getInputsCountFromFirst(chn, first); unsigned int count = getInputsCountFromFirst(chn, first);
if (first>=0 && idx<count) { if (count && (idx < count)) {
ExpoData * expo = expoAddress(first+idx); ExpoData * expo = expoAddress(first+idx);
lua_newtable(L); lua_newtable(L);
lua_pushtablezstring(L, "name", expo->name); lua_pushtablezstring(L, "name", expo->name);
@ -735,15 +731,15 @@ static int luaModelGetInput(lua_State *L)
static int luaModelInsertInput(lua_State *L) static int luaModelInsertInput(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstInput(chn); unsigned int first = getFirstInput(chn);
int count = getInputsCountFromFirst(chn, first); unsigned int count = getInputsCountFromFirst(chn, first);
if (chn<MAX_INPUTS && getExpoMixCount(1)<MAX_INPUTS && idx<=count) { if (chn<MAX_INPUTS && getExpoMixCount(1)<MAX_INPUTS && idx<=count) {
idx = first+idx; idx = first + idx;
s_currCh = chn+1; s_currCh = chn + 1;
insertExpoMix(1, idx); insertExpoMix(1, idx);
ExpoData * expo = expoAddress(idx); ExpoData * expo = expoAddress(idx);
luaL_checktype(L, -1, LUA_TTABLE); luaL_checktype(L, -1, LUA_TTABLE);
@ -774,13 +770,13 @@ static int luaModelInsertInput(lua_State *L)
static int luaModelDeleteInput(lua_State *L) static int luaModelDeleteInput(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstInput(chn); int first = getFirstInput(chn);
int count = getInputsCountFromFirst(chn, first); unsigned int count = getInputsCountFromFirst(chn, first);
if (first>=0 && idx<count) { if (count && (idx < count)) {
deleteExpoMix(1, first+idx); deleteExpoMix(1, first+idx);
} }
@ -831,7 +827,7 @@ static int getMixesCount(int chn)
static int luaModelGetMixesCount(lua_State *L) static int luaModelGetMixesCount(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int count = getMixesCount(chn); int count = getMixesCount(chn);
lua_pushinteger(L, count); lua_pushinteger(L, count);
return 1; return 1;
@ -839,11 +835,11 @@ static int luaModelGetMixesCount(lua_State *L)
static int luaModelGetMix(lua_State *L) static int luaModelGetMix(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstMix(chn); int first = getFirstMix(chn);
int count = getMixesCountFromFirst(chn, first); unsigned int count = getMixesCountFromFirst(chn, first);
if (idx<count) { if (idx < count) {
MixData * mix = mixAddress(first+idx); MixData * mix = mixAddress(first+idx);
lua_newtable(L); lua_newtable(L);
lua_pushtablezstring(L, "name", mix->name); lua_pushtablezstring(L, "name", mix->name);
@ -870,11 +866,11 @@ static int luaModelGetMix(lua_State *L)
static int luaModelInsertMix(lua_State *L) static int luaModelInsertMix(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstMix(chn); int first = getFirstMix(chn);
int count = getMixesCountFromFirst(chn, first); unsigned int count = getMixesCountFromFirst(chn, first);
if (chn<NUM_CHNOUT && getExpoMixCount(0)<MAX_MIXERS && idx<=count) { if (chn<NUM_CHNOUT && getExpoMixCount(0)<MAX_MIXERS && idx<=count) {
idx += first; idx += first;
@ -939,13 +935,13 @@ static int luaModelInsertMix(lua_State *L)
static int luaModelDeleteMix(lua_State *L) static int luaModelDeleteMix(lua_State *L)
{ {
int chn = luaL_checkunsigned(L, 1); unsigned int chn = luaL_checkunsigned(L, 1);
int idx = luaL_checkunsigned(L, 2); unsigned int idx = luaL_checkunsigned(L, 2);
int first = getFirstMix(chn); int first = getFirstMix(chn);
int count = getMixesCountFromFirst(chn, first); unsigned int count = getMixesCountFromFirst(chn, first);
if (idx<count) { if (idx < count) {
deleteExpoMix(0, first+idx); deleteExpoMix(0, first+idx);
} }
@ -960,7 +956,7 @@ static int luaModelDeleteMixes(lua_State *L)
static int luaModelGetLogicalSwitch(lua_State *L) static int luaModelGetLogicalSwitch(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_LOGICAL_SWITCH) { if (idx < NUM_LOGICAL_SWITCH) {
LogicalSwitchData * sw = lswAddress(idx); LogicalSwitchData * sw = lswAddress(idx);
lua_newtable(L); lua_newtable(L);
@ -980,7 +976,7 @@ static int luaModelGetLogicalSwitch(lua_State *L)
static int luaModelSetLogicalSwitch(lua_State *L) static int luaModelSetLogicalSwitch(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_LOGICAL_SWITCH) { if (idx < NUM_LOGICAL_SWITCH) {
LogicalSwitchData * sw = lswAddress(idx); LogicalSwitchData * sw = lswAddress(idx);
memclear(sw, sizeof(LogicalSwitchData)); memclear(sw, sizeof(LogicalSwitchData));
@ -1018,7 +1014,7 @@ static int luaModelSetLogicalSwitch(lua_State *L)
static int luaModelGetCurve(lua_State *L) static int luaModelGetCurve(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < MAX_CURVES) { if (idx < MAX_CURVES) {
CurveInfo & curveInfo = g_model.curves[idx]; CurveInfo & curveInfo = g_model.curves[idx];
lua_newtable(L); lua_newtable(L);
@ -1060,7 +1056,7 @@ static int luaModelGetCurve(lua_State *L)
static int luaModelGetCustomFunction(lua_State *L) static int luaModelGetCustomFunction(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_CFN) { if (idx < NUM_CFN) {
CustomFunctionData * cfn = &g_model.customFn[idx]; CustomFunctionData * cfn = &g_model.customFn[idx];
lua_newtable(L); lua_newtable(L);
@ -1084,7 +1080,7 @@ static int luaModelGetCustomFunction(lua_State *L)
static int luaModelSetCustomFunction(lua_State *L) static int luaModelSetCustomFunction(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_CFN) { if (idx < NUM_CFN) {
CustomFunctionData * cfn = &g_model.customFn[idx]; CustomFunctionData * cfn = &g_model.customFn[idx];
memclear(cfn, sizeof(CustomFunctionData)); memclear(cfn, sizeof(CustomFunctionData));
@ -1123,7 +1119,7 @@ static int luaModelSetCustomFunction(lua_State *L)
static int luaModelGetOutput(lua_State *L) static int luaModelGetOutput(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_CHNOUT) { if (idx < NUM_CHNOUT) {
LimitData * limit = limitAddress(idx); LimitData * limit = limitAddress(idx);
lua_newtable(L); lua_newtable(L);
@ -1147,7 +1143,7 @@ static int luaModelGetOutput(lua_State *L)
static int luaModelSetOutput(lua_State *L) static int luaModelSetOutput(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
if (idx < NUM_CHNOUT) { if (idx < NUM_CHNOUT) {
LimitData * limit = limitAddress(idx); LimitData * limit = limitAddress(idx);
luaL_checktype(L, -1, LUA_TTABLE); luaL_checktype(L, -1, LUA_TTABLE);
@ -1191,8 +1187,8 @@ static int luaModelSetOutput(lua_State *L)
static int luaModelGetGlobalVariable(lua_State *L) static int luaModelGetGlobalVariable(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
int phase = luaL_checkunsigned(L, 2); unsigned int phase = luaL_checkunsigned(L, 2);
if (phase < MAX_FLIGHT_MODES && idx < MAX_GVARS) if (phase < MAX_FLIGHT_MODES && idx < MAX_GVARS)
lua_pushinteger(L, g_model.flightModeData[phase].gvars[idx]); lua_pushinteger(L, g_model.flightModeData[phase].gvars[idx]);
else else
@ -1202,8 +1198,8 @@ static int luaModelGetGlobalVariable(lua_State *L)
static int luaModelSetGlobalVariable(lua_State *L) static int luaModelSetGlobalVariable(lua_State *L)
{ {
int idx = luaL_checkunsigned(L, 1); unsigned int idx = luaL_checkunsigned(L, 1);
int phase = luaL_checkunsigned(L, 2); unsigned int phase = luaL_checkunsigned(L, 2);
int value = luaL_checkinteger(L, 3); int value = luaL_checkinteger(L, 3);
if (phase < MAX_FLIGHT_MODES && idx < MAX_GVARS && value >= -GVAR_MAX && value <= GVAR_MAX) { if (phase < MAX_FLIGHT_MODES && idx < MAX_GVARS && value >= -GVAR_MAX && value <= GVAR_MAX) {
g_model.flightModeData[phase].gvars[idx] = value; g_model.flightModeData[phase].gvars[idx] = value;