feat(radio): refactor of switch handling to better support customisable switches (#6095)

Co-authored-by: philmoz <phil.a.mitchell@gmail.com>
Co-authored-by: 3djc <3djc@gh.com>
Co-authored-by: elecpower <elecpower@users.noreply.github.com>
This commit is contained in:
philmoz
2025-07-28 12:59:22 +10:00
committed by GitHub
co-authored by philmoz 3djc elecpower
parent 28833f42e6
commit 0c8c39b134
155 changed files with 4538 additions and 2306 deletions
+31 -4
View File
@@ -25,32 +25,59 @@
#include "dataconstants.h"
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
static bool hasLedOverride[NUM_FUNCTIONS_SWITCHES] = { false };
static RGBLedColor ledOverride[NUM_FUNCTIONS_SWITCHES];
void setFSLedOverride(uint8_t index, bool state, uint8_t r, uint8_t g, uint8_t b)
{
hasLedOverride[index] = state;
ledOverride[index].r = r;
ledOverride[index].g = g;
ledOverride[index].b = b;
}
void setFSLedOFF(uint8_t index) {
fsLedRGB(index, g_model.functionSwitchLedOFFColor[index].getColor());
if (g_model.getSwitchType(index) != SWITCH_NONE) {
uint8_t cfsIdx = switchGetCustomSwitchIdx(index);
if (hasLedOverride[cfsIdx] && g_model.getSwitchOffColorLuaOverride(index))
fsLedRGB(cfsIdx, ledOverride[cfsIdx].getColor());
else
fsLedRGB(cfsIdx, g_model.getSwitchOffColor(index).getColor());
}
}
void setFSLedON(uint8_t index) {
fsLedRGB(index, g_model.functionSwitchLedONColor[index].getColor());
if (g_model.getSwitchType(index) != SWITCH_NONE) {
uint8_t cfsIdx = switchGetCustomSwitchIdx(index);
if (hasLedOverride[cfsIdx] && g_model.getSwitchOnColorLuaOverride(index))
fsLedRGB(cfsIdx, ledOverride[cfsIdx].getColor());
else
fsLedRGB(cfsIdx, g_model.getSwitchOnColor(index).getColor());
}
}
bool getFSLedState(uint8_t index) {
return fsGetLedRGB(index) == g_model.functionSwitchLedONColor[index].getColor();
uint8_t cfsIdx = switchGetCustomSwitchIdx(index);
return rgbGetLedColor(cfsIdx) == g_model.getSwitchOnColor(index).getColor();
}
uint32_t getFSLedRGBColor(uint8_t index)
{
return fsGetLedRGB(index);
return fsGetLedRGB(index);
}
#else
void setFSLedOFF(uint8_t index) {
index = switchGetCustomSwitchIdx(index);
fsLedOff(index);
}
void setFSLedON(uint8_t index) {
index = switchGetCustomSwitchIdx(index);
fsLedOn(index);
}
bool getFSLedState(uint8_t index) {
index = switchGetCustomSwitchIdx(index);
return fsLedState(index);
}
#endif
+1
View File
@@ -25,6 +25,7 @@
// "Off", "White", "Red", "Green", "Yellow", "Orange", "Blue", "Pink"
constexpr uint32_t colorTable[] = {0x000000, 0xFFFFFF, 0xFF0000, 0x00FF00, 0xFFFF00, 0xFF4000, 0x0000FF, 0xFF00FF};
void setFSLedOverride(uint8_t index, bool state, uint8_t r, uint8_t g, uint8_t b);
void setFSLedOFF(uint8_t index);
void setFSLedON(uint8_t index);
bool getFSLedState(uint8_t index);
+60 -34
View File
@@ -31,15 +31,18 @@
// Board API
void boardInitSwitches();
SwitchHwPos boardSwitchGetPosition(SwitchCategory cat, uint8_t idx);
SwitchHwPos boardSwitchGetPosition(uint8_t idx);
const char* boardSwitchGetName(SwitchCategory cat, uint8_t idx);
SwitchHwType boardSwitchGetType(SwitchCategory cat, uint8_t idx);
const char* boardSwitchGetName(uint8_t idx);
SwitchHwType boardSwitchGetType(uint8_t idx);
#if defined(FUNCTION_SWITCHES)
bool boardIsCustomSwitch(uint8_t idx);
uint8_t boardGetCustomSwitchIdx(uint8_t idx);
#endif
uint8_t boardGetMaxSwitches();
uint8_t boardGetMaxFctSwitches();
swconfig_t boardSwitchGetDefaultConfig();
SwitchConfig boardSwitchGetDefaultConfig(uint8_t n);
constexpr uint8_t _INVALID_ADC_CH = 0xFF;
@@ -51,14 +54,39 @@ void switchInit()
boardInitSwitches();
}
swconfig_t switchGetDefaultConfig() { return boardSwitchGetDefaultConfig(); }
SwitchConfig switchGetDefaultConfig(uint8_t n) { return boardSwitchGetDefaultConfig(n); }
#if defined(FUNCTION_SWITCHES)
bool switchIsCustomSwitch(int sw)
{
return boardIsCustomSwitch(sw);
}
uint8_t switchGetCustomSwitchIdx(uint8_t sw)
{
return boardGetCustomSwitchIdx(sw);
}
uint8_t switchGetSwitchFromCustomIdx(uint8_t idx)
{
for (uint8_t i = 0; i < switchGetMaxSwitches(); i += 1)
if (switchIsCustomSwitch(i) && switchGetCustomSwitchIdx(i) == idx)
return i;
return switchGetMaxSwitches();
}
#else
bool switchIsCustomSwitch(int sw) { return false; }
#endif
uint8_t switchGetMaxSwitches()
{
return boardGetMaxSwitches() + MAX_FLEX_SWITCHES;
return boardGetMaxSwitches();
}
uint8_t switchGetMaxFctSwitches() { return boardGetMaxFctSwitches(); }
uint8_t switchGetMaxAllSwitches()
{
return boardGetMaxSwitches() + MAX_FLEX_SWITCHES;
}
uint32_t switchState(uint8_t pos_idx)
{
@@ -72,9 +100,9 @@ uint32_t switchState(uint8_t pos_idx)
SwitchHwPos switchGetPosition(uint8_t sw_idx)
{
auto idx = (int)sw_idx;
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) {
return boardSwitchGetPosition(SWITCH_PHYSICAL, idx);
return boardSwitchGetPosition(idx);
}
idx -= max_switches;
@@ -95,12 +123,6 @@ SwitchHwPos switchGetPosition(uint8_t sw_idx)
return ret;
}
idx -= MAX_FLEX_SWITCHES;
}
if (idx < boardGetMaxFctSwitches()) {
return boardSwitchGetPosition(SWITCH_FUNCTION, idx);
}
return SWITCH_HW_UP;
@@ -113,12 +135,12 @@ static const char * const _flex_sw_canon_names[] = {
static_assert(DIM(_flex_sw_canon_names) >= MAX_FLEX_SWITCHES,
"Missing canonical name for flex switches");
const char* switchGetName(uint8_t sw_idx)
const char* switchGetDefaultName(uint8_t sw_idx)
{
auto idx = (int)sw_idx;
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) {
return boardSwitchGetName(SWITCH_PHYSICAL, idx);
return boardSwitchGetName(idx);
}
idx -= max_switches;
@@ -129,20 +151,25 @@ const char* switchGetName(uint8_t sw_idx)
idx -= MAX_FLEX_SWITCHES;
}
if (idx < boardGetMaxFctSwitches()) {
return boardSwitchGetName(SWITCH_FUNCTION, idx);
}
return nullptr;
}
int8_t switchGetIndexFromName(const char* name)
{
for (uint8_t i = 0; i < switchGetMaxSwitches(); i += 1)
if (strcmp(name, boardSwitchGetName(i)) == 0)
return i;
return -1;
}
SwitchHwType switchGetHwType(uint8_t sw_idx)
{
auto idx = (int)sw_idx;
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) {
return boardSwitchGetType(SWITCH_PHYSICAL, idx);
return boardSwitchGetType(idx);
}
idx -= max_switches;
@@ -155,8 +182,8 @@ SwitchHwType switchGetHwType(uint8_t sw_idx)
bool switchIsFlex(uint8_t idx)
{
auto max_switches = boardGetMaxSwitches();
return idx >= max_switches && idx < max_switches + MAX_FLEX_SWITCHES;
auto max_switches = switchGetMaxSwitches();
return idx >= max_switches && idx < switchGetMaxAllSwitches();
}
void switchConfigFlex_raw(uint8_t idx, int8_t channel)
@@ -179,7 +206,7 @@ uint8_t switchGetFlexChannel(uint8_t idx)
void switchConfigFlex(uint8_t idx, int8_t channel)
{
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) return;
idx -= max_switches;
switchConfigFlex_raw(idx, channel);
@@ -193,7 +220,7 @@ int8_t switchGetFlexConfig_raw(uint8_t idx)
int8_t switchGetFlexConfig(uint8_t idx)
{
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) return _INVALID_ADC_CH;
idx -= max_switches;
return switchGetFlexConfig_raw(idx);
@@ -208,7 +235,7 @@ bool switchIsFlexValid_raw(uint8_t sw_idx)
bool switchIsFlexValid(uint8_t idx)
{
auto max_switches = boardGetMaxSwitches();
auto max_switches = switchGetMaxSwitches();
if (idx < max_switches) return false;
idx -= max_switches;
return switchIsFlexValid_raw(idx);
@@ -229,7 +256,7 @@ bool switchIsFlexInputAvailable(uint8_t idx, uint8_t channel)
if (MAX_FLEX_SWITCHES == 0) return false;
if (POT_CONFIG(channel) != FLEX_SWITCH) return false;
auto phy_switches = boardGetMaxSwitches();
auto phy_switches = switchGetMaxSwitches();
if (idx < phy_switches) return false;
return is_flex_input_available(idx - phy_switches, channel);
@@ -237,9 +264,8 @@ bool switchIsFlexInputAvailable(uint8_t idx, uint8_t channel)
static void invalidate_flex_config(uint8_t flex_idx)
{
auto sw_idx = flex_idx + boardGetMaxSwitches();
swconfig_t mask = (swconfig_t)SWITCH_CONFIG_MASK(sw_idx);
g_eeGeneral.switchConfig &= ~mask;
auto sw_idx = flex_idx + switchGetMaxSwitches();
g_eeGeneral.switchSetType(sw_idx, SWITCH_NONE);
_flex_switches[flex_idx] = _INVALID_ADC_CH;
}
+11 -20
View File
@@ -23,6 +23,7 @@
#include <stdint.h>
#include "edgetx_types.h"
#include "edgetx_constants.h"
enum SwitchHwType {
SWITCH_HW_2POS = 0,
@@ -36,21 +37,22 @@ enum SwitchHwPos {
SWITCH_HW_DOWN,
};
enum SwitchCategory {
SWITCH_PHYSICAL=0,
SWITCH_FUNCTION,
};
#define SWITCH_HW_INVERTED 1
// init hardware for switches
void switchInit();
// returns the factory configuration for all switches
swconfig_t switchGetDefaultConfig();
SwitchConfig switchGetDefaultConfig(uint8_t n);
// returns the maximum number of regular switches supported in hardware (customizable switches NOT included)
bool switchIsCustomSwitch(int sw);
uint8_t switchGetCustomSwitchIdx(uint8_t sw);
uint8_t switchGetSwitchFromCustomIdx(uint8_t sw);
// returns the maximum number of physical + custom switches supported in hardware
uint8_t switchGetMaxSwitches();
// returns the maximum number of physical + custom + flex switches supported in hardware
uint8_t switchGetMaxAllSwitches();
// returns the number of configured switches
uint8_t getSwitchCount();
@@ -58,9 +60,6 @@ uint8_t getSwitchCount();
// returns the highest row for a specified column
uint8_t switchGetMaxRow(uint8_t col);
// returns the maximum number of customizable switches supported in hardware
uint8_t switchGetMaxFctSwitches();
struct switch_display_pos_t {
uint8_t col;
uint8_t row;
@@ -83,20 +82,12 @@ void switchConfigFlex_raw(uint8_t idx, int8_t channel);
int8_t switchGetFlexConfig_raw(uint8_t idx);
bool switchIsFlexValid_raw(uint8_t idx);
// The functions bellow support regular as well as customizable switches.
//
// Regular switches are indexed from `0` to `switchGetMaxSwitches() - 1`
// Customizable switches are indexed above the previous range.
//
// The total number of switches (and max index + 1) is:
// switchGetMaxSwitches() + switchGetMaxFctSwitches()
//
// returns state (0 / 1) of a specific switch position
uint32_t switchState(uint8_t pos_idx);
// returns a position for a switch index
SwitchHwPos switchGetPosition(uint8_t idx);
const char* switchGetName(uint8_t idx);
const char* switchGetDefaultName(uint8_t idx);
SwitchHwType switchGetHwType(uint8_t idx);
int8_t switchGetIndexFromName(const char* name);