fix: Use keysGetMaxTrims() instead of MAX_TRIMS (#3860)

* fix: use keysGetMaxTrims() instead of MAX_TRIMS

* feat: add support for virtual trims

This is necessary for radios like X-lite where half of the trims are simulated
This commit is contained in:
Raphael Coeffic
2023-07-30 14:18:58 +10:00
committed by GitHub
parent 76423370cd
commit 574369aca4
13 changed files with 55 additions and 36 deletions
+1 -1
View File
@@ -808,7 +808,7 @@ void menuModelSetup(event_t event)
case ITEM_MODEL_SETUP_THROTTLE_TRIM_SWITCH: case ITEM_MODEL_SETUP_THROTTLE_TRIM_SWITCH:
lcdDrawTextAlignedLeft(y, STR_TTRIM_SW); lcdDrawTextAlignedLeft(y, STR_TTRIM_SW);
if (attr) 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); drawSource(MODEL_SETUP_2ND_COLUMN, y, g_model.getThrottleStickTrimSource(), attr);
break; break;
+1 -1
View File
@@ -108,7 +108,7 @@ class FlightModeEdit : public Page
FlexGridLayout trim_grid(trims_col_dsc, line_row_dsc); 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; lastTrim[t] = p_fm->trim[t].value;
if ((t % TRIMS_PER_LINE) == 0) { if ((t % TRIMS_PER_LINE) == 0) {
+5 -8
View File
@@ -25,6 +25,8 @@
#include "hal/rotary_encoder.h" #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) static EnumKeys get_ith_key(uint8_t i)
{ {
auto supported_keys = keysGetSupported(); auto supported_keys = keysGetSupported();
@@ -118,7 +120,7 @@ class RadioKeyDiagsWindow : public Window
} }
#endif #endif
// SWITCHES // SWITCHES
for (uint8_t i = 0; i < MAX_SWITCHES; i++) { for (uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
if (SWITCH_EXISTS(i)) { if (SWITCH_EXISTS(i)) {
coord_t y = 1 + FH * i; coord_t y = 1 + FH * i;
getvalue_t val = getValue(MIXSRC_FIRST_SWITCH + i); getvalue_t val = getValue(MIXSRC_FIRST_SWITCH + i);
@@ -128,18 +130,13 @@ class RadioKeyDiagsWindow : public Window
} }
// TRIMS // TRIMS
for (uint8_t i = 0; i < MAX_TRIMS * 2; i++) { for (uint8_t i = 0; i < keysGetMaxTrims() * 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
coord_t y = 1 + FH + FH * (i / 2); coord_t y = 1 + FH + FH * (i / 2);
if (i & 1) { if (i & 1) {
dc->drawText(TRIM_COLUMN, y, "T", COLOR_THEME_PRIMARY1); dc->drawText(TRIM_COLUMN, y, "T", COLOR_THEME_PRIMARY1);
dc->drawNumber(TRIM_COLUMN + 10, y, i / 2 + 1, 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]);
} }
}; };
+3 -4
View File
@@ -316,8 +316,6 @@ static uint32_t transpose_trims()
bool keysPollingCycle() bool keysPollingCycle()
{ {
uint32_t i;
uint32_t trims_input; uint32_t trims_input;
uint32_t keys_input = readKeys(); uint32_t keys_input = readKeys();
@@ -332,7 +330,7 @@ bool keysPollingCycle()
trims_input = readTrims(); trims_input = readTrims();
#endif #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)); event_t evt = keys[i].input(keys_input & (1 << i));
if (evt) { if (evt) {
if (i != KEY_SHIFT && evt != _MSK_KEY_REPT) { 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)); event_t evt = trim_keys[i].input(trims_input & (1 << i));
if (evt) pushTrimEvent(evt | i); if (evt) pushTrimEvent(evt | i);
} }
+7 -6
View File
@@ -488,7 +488,7 @@ static int luaModelGetFlightMode(lua_State * L)
lua_pushtableinteger(L, "fadeOut", fm->fadeOut); lua_pushtableinteger(L, "fadeOut", fm->fadeOut);
lua_pushstring(L, "trimsValues"); lua_pushstring(L, "trimsValues");
lua_newtable(L); 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, i + 1);
lua_pushinteger(L, fm->trim[i].value); lua_pushinteger(L, fm->trim[i].value);
lua_settable(L, -3); lua_settable(L, -3);
@@ -496,7 +496,7 @@ static int luaModelGetFlightMode(lua_State * L)
lua_settable(L, -3); lua_settable(L, -3);
lua_pushstring(L, "trimsModes"); lua_pushstring(L, "trimsModes");
lua_newtable(L); 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, i + 1);
lua_pushinteger(L, fm->trim[i].mode); lua_pushinteger(L, fm->trim[i].mode);
lua_settable(L, -3); lua_settable(L, -3);
@@ -530,6 +530,7 @@ static int luaModelSetFlightMode(lua_State * L)
} }
FlightModeData * fm = flightModeAddress(flightMode); FlightModeData * fm = flightModeAddress(flightMode);
luaL_checktype(L, -1, LUA_TTABLE); luaL_checktype(L, -1, LUA_TTABLE);
auto max_trims = keysGetMaxTrims();
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
luaL_checktype(L, -2, LUA_TSTRING); // key is string luaL_checktype(L, -2, LUA_TSTRING); // key is string
const char * key = luaL_checkstring(L, -2); const char * key = luaL_checkstring(L, -2);
@@ -550,13 +551,13 @@ static int luaModelSetFlightMode(lua_State * L)
luaL_checktype(L, -1, LUA_TTABLE); luaL_checktype(L, -1, LUA_TTABLE);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
int idx = luaL_checkinteger(L, -2) - 1; // key is integer 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); int16_t val = luaL_checkinteger(L, -1);
if (g_model.extendedTrims) if (g_model.extendedTrims)
val = limit<int16_t>(val, TRIM_EXTENDED_MIN, TRIM_EXTENDED_MAX); val = limit<int16_t>(val, TRIM_EXTENDED_MIN, TRIM_EXTENDED_MAX);
else else
val = limit<int16_t>(val, TRIM_MIN, TRIM_MAX); val = limit<int16_t>(val, TRIM_MIN, TRIM_MAX);
if (idx < MAX_TRIMS) if (idx < max_trims)
fm->trim[idx].value = val; fm->trim[idx].value = val;
} }
} }
@@ -564,9 +565,9 @@ static int luaModelSetFlightMode(lua_State * L)
luaL_checktype(L, -1, LUA_TTABLE); luaL_checktype(L, -1, LUA_TTABLE);
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
int idx = luaL_checkinteger(L, -2) - 1; // key is integer 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); uint16_t val = luaL_checkinteger(L, -1);
if (idx < MAX_TRIMS) if (idx < max_trims)
fm->trim[idx].mode = (val & 0x1F); fm->trim[idx].mode = (val & 0x1F);
} }
} }
+2 -3
View File
@@ -464,18 +464,17 @@ getvalue_t getValue(mixsrc_t i, bool* valid)
void evalTrims() void evalTrims()
{ {
uint8_t phase = mixerCurrentFlightMode; uint8_t phase = mixerCurrentFlightMode;
for (uint8_t i=0; i<MAX_TRIMS; i++) { for (uint8_t i = 0; i < keysGetMaxTrims(); i++) {
// do trim -> throttle trim if applicable // do trim -> throttle trim if applicable
int16_t trim = getTrimValue(phase, i); int16_t trim = getTrimValue(phase, i);
if (trimsCheckTimer > 0) { if (trimsCheckTimer > 0) {
trim = 0; trim = 0;
} }
trims[i] = trim*2; trims[i] = trim * 2;
} }
} }
// TODO: move to analogs.cpp // TODO: move to analogs.cpp
void evalInputs(uint8_t mode) void evalInputs(uint8_t mode)
{ {
+2 -2
View File
@@ -930,7 +930,7 @@ void alert(const char * title, const char * msg , uint8_t sound)
#if defined(GVARS) #if defined(GVARS)
#if MAX_TRIMS == 8 #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 #elif MAX_TRIMS == 6
int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1 }; int8_t trimGvar[MAX_TRIMS] = { -1, -1, -1, -1, -1, -1 };
#elif MAX_TRIMS == 4 #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) // reset all trims, except throttle (if throttle trim)
for (uint8_t i=0; i<MAX_TRIMS; i++) { for (uint8_t i = 0; i < keysGetMaxTrims(); i++) {
auto thrStick = g_model.getThrottleStickTrimSource() - MIXSRC_FIRST_TRIM; auto thrStick = g_model.getThrottleStickTrimSource() - MIXSRC_FIRST_TRIM;
if (i != thrStick || !g_model.thrTrim) { if (i != thrStick || !g_model.thrTrim) {
int16_t original_trim = getTrimValue(mixerCurrentFlightMode, i); int16_t original_trim = getTrimValue(mixerCurrentFlightMode, i);
@@ -1,6 +1,9 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
AddHardwareDefTarget(${HW_DESC_JSON})
AddHWGenTarget(${HW_DESC_JSON} hal_keys hal_keys.inc)
set(BOOTLOADER_SRC set(BOOTLOADER_SRC
${BOOTLOADER_SRC} ${BOOTLOADER_SRC}
../../../../../gui/${GUI_DIR}/lcd.cpp ../../../../../gui/${GUI_DIR}/lcd.cpp
@@ -22,11 +25,12 @@ set(BOOTLOADER_SRC
../../../../../${FATFS_DIR}/ffunicode.c ../../../../../${FATFS_DIR}/ffunicode.c
../../../../../targets/${TARGET_DIR}/${LCD_DRIVER} ../../../../../targets/${TARGET_DIR}/${LCD_DRIVER}
../../../../../targets/${TARGET_DIR}/backlight_driver.cpp ../../../../../targets/${TARGET_DIR}/backlight_driver.cpp
#../../../../../targets/${TARGET_DIR}/keys_driver.cpp
../../../../../targets/${TARGET_DIR}/diskio.cpp ../../../../../targets/${TARGET_DIR}/diskio.cpp
../../../../../targets/${TARGET_DIR}/board.cpp ../../../../../targets/${TARGET_DIR}/board.cpp
../pwr_driver.cpp
../../../../../targets/${TARGET_DIR}/bootloader/boot_menu.cpp ../../../../../targets/${TARGET_DIR}/bootloader/boot_menu.cpp
${CMAKE_CURRENT_BINARY_DIR}/hal_keys.inc
../../../../../hal/key_driver.cpp
../pwr_driver.cpp
../usbd_usr.cpp ../usbd_usr.cpp
../usbd_storage_msd.cpp ../usbd_storage_msd.cpp
../delays_driver.cpp ../delays_driver.cpp
@@ -135,6 +139,10 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=nano.specs")
add_executable(bootloader EXCLUDE_FROM_ALL ${BOOTLOADER_SRC}) add_executable(bootloader EXCLUDE_FROM_ALL ${BOOTLOADER_SRC})
add_dependencies(bootloader ${BITMAPS_TARGET}) add_dependencies(bootloader ${BITMAPS_TARGET})
target_include_directories(bootloader
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)
add_custom_command( add_custom_command(
TARGET bootloader POST_BUILD TARGET bootloader POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O binary bootloader.elf ${CMAKE_BINARY_DIR}/bootloader.bin COMMAND ${CMAKE_OBJCOPY} -O binary bootloader.elf ${CMAKE_BINARY_DIR}/bootloader.bin
+1 -1
View File
@@ -479,7 +479,7 @@ uint32_t readTrims()
{ {
uint32_t result = 0; uint32_t result = 0;
for (int i = 0; i < MAX_TRIMS * 2; i++) { for (int i = 0; i < keysGetMaxTrims() * 2; i++) {
if (trimsStates[i]) { if (trimsStates[i]) {
// TRACE("trim pressed %d", i); // TRACE("trim pressed %d", i);
result |= 1 << i; result |= 1 << i;
+2
View File
@@ -331,6 +331,8 @@
#define TRIMS_GPIO_PIN_LVU LL_GPIO_PIN_0 // PB.00 #define TRIMS_GPIO_PIN_LVU LL_GPIO_PIN_0 // PB.00
#define TRIMS_GPIO_REG_LVD GPIOB #define TRIMS_GPIO_REG_LVD GPIOB
#define TRIMS_GPIO_PIN_LVD LL_GPIO_PIN_1 // PB.01 #define TRIMS_GPIO_PIN_LVD LL_GPIO_PIN_1 // PB.01
#define TRIMS_VIRTUAL_RV
#define TRIMS_VIRTUAL_RH
#elif defined(RADIO_X7ACCESS) #elif defined(RADIO_X7ACCESS)
#define TRIMS_GPIO_REG_LHR GPIOD #define TRIMS_GPIO_REG_LHR GPIOD
#define TRIMS_GPIO_PIN_LHR LL_GPIO_PIN_15 // PD.15 #define TRIMS_GPIO_PIN_LHR LL_GPIO_PIN_15 // PD.15
+9 -3
View File
@@ -68,9 +68,10 @@ class Trim:
def __init__(self, name, dec, inc): def __init__(self, name, dec, inc):
self.name = name self.name = name
self.dec = dec if dec and dec.gpio and inc and inc.gpio:
self.inc = inc self.dec = dec
self.active_low = True self.inc = inc
self.active_low = True
def get_trim_switch(hw_defs, tag): def get_trim_switch(hw_defs, tag):
@@ -98,6 +99,11 @@ def parse_trims(hw_defs):
if dec and inc: if dec and inc:
trims.append(Trim(name, dec, inc)) trims.append(Trim(name, dec, inc))
elif t.get('input'):
input = t['input']
if f'TRIMS_VIRTUAL_{input}' in hw_defs:
trims.append(Trim(name, None, None))
return trims return trims
+9 -3
View File
@@ -66,13 +66,19 @@ def build_switch_gpio_port_index(switches):
def build_trim_gpio_port_index(trims): def build_trim_gpio_port_index(trims):
def index_contact(gpios, contact): def index_contact(gpios, contact):
append_to_index(gpios, contact['gpio'], contact['pin']) gpio = contact['gpio']
pin = contact['pin']
if gpio and pin:
append_to_index(gpios, contact['gpio'], contact['pin'])
i = 0 i = 0
gpios = {} gpios = {}
for trim in trims: for trim in trims:
index_contact(gpios, trim['dec']) dec = trim.get('dec')
index_contact(gpios, trim['inc']) inc = trim.get('inc')
if dec and inc:
index_contact(gpios, dec)
index_contact(gpios, inc)
return gpios return gpios
+3 -2
View File
@@ -28,6 +28,7 @@ static inline uint32_t _read_keys()
return keys; return keys;
} }
{% set hw_trims = trims | selectattr('dec', 'defined') | selectattr('inc', 'defined') | list %}
static inline void _init_trims() static inline void _init_trims()
{ {
{% for trim_gpio, pins in trim_gpios | dictsort %} {% for trim_gpio, pins in trim_gpios | dictsort %}
@@ -36,7 +37,7 @@ static inline void _init_trims()
LL_GPIO_InitTypeDef pinInit; LL_GPIO_InitTypeDef pinInit;
LL_GPIO_StructInit(&pinInit); LL_GPIO_StructInit(&pinInit);
pinInit.Mode = LL_GPIO_MODE_INPUT; pinInit.Mode = LL_GPIO_MODE_INPUT;
{% for trim in trims %} {% for trim in hw_trims %}
pinInit.Pin = {{ trim.dec.pin }}; pinInit.Pin = {{ trim.dec.pin }};
pinInit.Pull = {{ 'LL_GPIO_PULL_UP' if trim.dec.active_low else 'LL_GPIO_PULL_DOWN' }}; pinInit.Pull = {{ 'LL_GPIO_PULL_UP' if trim.dec.active_low else 'LL_GPIO_PULL_DOWN' }};
LL_GPIO_Init({{ trim.dec.gpio }}, &pinInit); LL_GPIO_Init({{ trim.dec.gpio }}, &pinInit);
@@ -49,7 +50,7 @@ static inline void _init_trims()
static inline uint32_t _read_trims() static inline uint32_t _read_trims()
{ {
uint32_t trims = 0; uint32_t trims = 0;
{% for trim in trims %} {% for trim in hw_trims %}
if ({{'!' if trim.dec.active_low }}LL_GPIO_IsInputPinSet({{ trim.dec.gpio }}, {{ trim.dec.pin }})) if ({{'!' if trim.dec.active_low }}LL_GPIO_IsInputPinSet({{ trim.dec.gpio }}, {{ trim.dec.pin }}))
trims |= (1 << {{ loop.index0 * 2 }}); trims |= (1 << {{ loop.index0 * 2 }});
if ({{'!' if trim.inc.active_low }}LL_GPIO_IsInputPinSet({{ trim.inc.gpio }}, {{ trim.inc.pin }})) if ({{'!' if trim.inc.active_low }}LL_GPIO_IsInputPinSet({{ trim.inc.gpio }}, {{ trim.inc.pin }}))