diff --git a/radio/src/gui/212x64/model_setup.cpp b/radio/src/gui/212x64/model_setup.cpp index 9a26ced49..5d86a23f8 100644 --- a/radio/src/gui/212x64/model_setup.cpp +++ b/radio/src/gui/212x64/model_setup.cpp @@ -808,7 +808,7 @@ void menuModelSetup(event_t event) case ITEM_MODEL_SETUP_THROTTLE_TRIM_SWITCH: lcdDrawTextAlignedLeft(y, STR_TTRIM_SW); if (attr) - CHECK_INCDEC_MODELVAR_ZERO(event, g_model.thrTrimSw, MAX_TRIMS - 1); + CHECK_INCDEC_MODELVAR_ZERO(event, g_model.thrTrimSw, keysGetMaxTrims() - 1); drawSource(MODEL_SETUP_2ND_COLUMN, y, g_model.getThrottleStickTrimSource(), attr); break; diff --git a/radio/src/gui/colorlcd/model_flightmodes.cpp b/radio/src/gui/colorlcd/model_flightmodes.cpp index 20c14120f..817130885 100644 --- a/radio/src/gui/colorlcd/model_flightmodes.cpp +++ b/radio/src/gui/colorlcd/model_flightmodes.cpp @@ -108,7 +108,7 @@ class FlightModeEdit : public Page FlexGridLayout trim_grid(trims_col_dsc, line_row_dsc); - for (int t = 0; t < MAX_TRIMS; t++) { + for (int t = 0; t < keysGetMaxTrims(); t++) { lastTrim[t] = p_fm->trim[t].value; if ((t % TRIMS_PER_LINE) == 0) { diff --git a/radio/src/gui/colorlcd/radio_diagkeys.cpp b/radio/src/gui/colorlcd/radio_diagkeys.cpp index 39711bf36..8814e1906 100644 --- a/radio/src/gui/colorlcd/radio_diagkeys.cpp +++ b/radio/src/gui/colorlcd/radio_diagkeys.cpp @@ -25,6 +25,8 @@ #include "hal/rotary_encoder.h" +static const uint8_t _trimMap[MAX_TRIMS * 2] = {6, 7, 4, 5, 2, 3, 0, 1, 8, 9, 10, 11}; + static EnumKeys get_ith_key(uint8_t i) { auto supported_keys = keysGetSupported(); @@ -118,7 +120,7 @@ class RadioKeyDiagsWindow : public Window } #endif // SWITCHES - for (uint8_t i = 0; i < MAX_SWITCHES; i++) { + for (uint8_t i = 0; i < switchGetMaxSwitches(); i++) { if (SWITCH_EXISTS(i)) { coord_t y = 1 + FH * i; getvalue_t val = getValue(MIXSRC_FIRST_SWITCH + i); @@ -128,18 +130,13 @@ class RadioKeyDiagsWindow : public Window } // TRIMS - for (uint8_t i = 0; i < MAX_TRIMS * 2; i++) { -#if MAX_TRIMS * 2 == 12 - const uint8_t trimMap[MAX_TRIMS * 2] = {6, 7, 4, 5, 2, 3, 0, 1, 8, 9, 10, 11}; -#else - const uint8_t trimMap[MAX_TRIMS * 2] = {6, 7, 4, 5, 2, 3, 0, 1}; -#endif + for (uint8_t i = 0; i < keysGetMaxTrims() * 2; i++) { coord_t y = 1 + FH + FH * (i / 2); if (i & 1) { dc->drawText(TRIM_COLUMN, y, "T", COLOR_THEME_PRIMARY1); dc->drawNumber(TRIM_COLUMN + 10, y, i / 2 + 1, COLOR_THEME_PRIMARY1); } - displayTrimState(dc, i & 1 ? TRIM_PLUS_COLUMN : TRIM_MINUS_COLUMN, y, trimMap[i]); + displayTrimState(dc, i & 1 ? TRIM_PLUS_COLUMN : TRIM_MINUS_COLUMN, y, _trimMap[i]); } }; diff --git a/radio/src/keys.cpp b/radio/src/keys.cpp index 42c863528..87278241a 100644 --- a/radio/src/keys.cpp +++ b/radio/src/keys.cpp @@ -316,8 +316,6 @@ static uint32_t transpose_trims() bool keysPollingCycle() { - uint32_t i; - uint32_t trims_input; uint32_t keys_input = readKeys(); @@ -332,7 +330,7 @@ bool keysPollingCycle() trims_input = readTrims(); #endif - for (i = 0; i < MAX_KEYS; i++) { + for (int i = 0; i < keysGetMaxKeys(); i++) { event_t evt = keys[i].input(keys_input & (1 << i)); if (evt) { if (i != KEY_SHIFT && evt != _MSK_KEY_REPT) { @@ -341,7 +339,8 @@ bool keysPollingCycle() } } - for (i = 0; i < MAX_TRIMS * 2; i++) { + auto trim_switches = keysGetMaxTrims() * 2; + for (int i = 0; i < trim_switches; i++) { event_t evt = trim_keys[i].input(trims_input & (1 << i)); if (evt) pushTrimEvent(evt | i); } diff --git a/radio/src/lua/api_model.cpp b/radio/src/lua/api_model.cpp index 6999d4cc8..2b31855f6 100644 --- a/radio/src/lua/api_model.cpp +++ b/radio/src/lua/api_model.cpp @@ -488,7 +488,7 @@ static int luaModelGetFlightMode(lua_State * L) lua_pushtableinteger(L, "fadeOut", fm->fadeOut); lua_pushstring(L, "trimsValues"); lua_newtable(L); - for (uint8_t i = 0; i < MAX_TRIMS; i++) { + for (uint8_t i = 0; i < keysGetMaxTrims(); i++) { lua_pushinteger(L, i + 1); lua_pushinteger(L, fm->trim[i].value); lua_settable(L, -3); @@ -496,7 +496,7 @@ static int luaModelGetFlightMode(lua_State * L) lua_settable(L, -3); lua_pushstring(L, "trimsModes"); lua_newtable(L); - for (uint8_t i = 0; i < MAX_TRIMS; i++) { + for (uint8_t i = 0; i < keysGetMaxTrims(); i++) { lua_pushinteger(L, i + 1); lua_pushinteger(L, fm->trim[i].mode); lua_settable(L, -3); @@ -530,6 +530,7 @@ static int luaModelSetFlightMode(lua_State * L) } FlightModeData * fm = flightModeAddress(flightMode); luaL_checktype(L, -1, LUA_TTABLE); + auto max_trims = keysGetMaxTrims(); 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); @@ -550,13 +551,13 @@ static int luaModelSetFlightMode(lua_State * L) luaL_checktype(L, -1, LUA_TTABLE); 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 >= MAX_TRIMS) continue; + if (idx < 0 || idx >= max_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 < MAX_TRIMS) + if (idx < max_trims) fm->trim[idx].value = val; } } @@ -564,9 +565,9 @@ static int luaModelSetFlightMode(lua_State * L) luaL_checktype(L, -1, LUA_TTABLE); 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 >= MAX_TRIMS) continue; + if (idx < 0 || idx >= max_trims) continue; uint16_t val = luaL_checkinteger(L, -1); - if (idx < MAX_TRIMS) + if (idx < max_trims) fm->trim[idx].mode = (val & 0x1F); } } diff --git a/radio/src/mixer.cpp b/radio/src/mixer.cpp index b49af813c..ed9977728 100644 --- a/radio/src/mixer.cpp +++ b/radio/src/mixer.cpp @@ -464,18 +464,17 @@ getvalue_t getValue(mixsrc_t i, bool* valid) void evalTrims() { uint8_t phase = mixerCurrentFlightMode; - for (uint8_t i=0; i throttle trim if applicable int16_t trim = getTrimValue(phase, i); if (trimsCheckTimer > 0) { trim = 0; } - trims[i] = trim*2; + trims[i] = trim * 2; } } - // TODO: move to analogs.cpp void evalInputs(uint8_t mode) { diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 9cb996a32..7bb9a6c5c 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -930,7 +930,7 @@ void alert(const char * title, const char * msg , uint8_t sound) #if defined(GVARS) #if MAX_TRIMS == 8 -int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1, -1, -1 }; + int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1, -1, -1 }; #elif MAX_TRIMS == 6 int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1 }; #elif MAX_TRIMS == 4 @@ -1424,7 +1424,7 @@ void moveTrimsToOffsets() // copy state of 3 primary to subtrim } // reset all trims, except throttle (if throttle trim) - for (uint8_t i=0; i