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
+37 -34
View File
@@ -19,21 +19,26 @@
* GNU General Public License for more details.
*/
#include <stdint.h>
#include "edgetx.h"
#include "hal/rgbleds.h"
#include "definitions.h"
#include "board.h"
bool usbChargerLed() { return true; }
void ledRed() {}
void ledGreen() {}
void ledBlue() {}
void ledOff() {}
void rgbSetLedColor(uint8_t, uint8_t, uint8_t, uint8_t) {}
void rgbLedColorApply() {}
bool fsLedState(uint8_t i)
{
#if defined(FUNCTION_SWITCHES)
return g_model.customSwitches[i].state;
#else
return false;
#endif
}
uint8_t getRGBColorIndex(uint32_t color)
{
for (uint8_t i = 0; i < DIM(colorTable); i++) {
@@ -43,39 +48,37 @@ uint8_t getRGBColorIndex(uint32_t color)
return 0; // Custom value set with Companion
}
#if NUM_FUNCTIONS_SWITCHES > 0
static uint32_t _fs_switch_colors[NUM_FUNCTIONS_SWITCHES] = {0};
static uint32_t _fs_switch_color_mask = 0;
static uint32_t _fs_switch_mask = 0;
// RGB
#define WS2812_BYTES_PER_LED 3
void fsLedRGB(uint8_t index, uint32_t col)
// Maximum number of supported LEDs
#if !defined(WS2812_MAX_LEDS)
# define WS2812_MAX_LEDS 48
#endif
// Pixel values
static uint8_t _led_colors[WS2812_BYTES_PER_LED * WS2812_MAX_LEDS];
void rgbSetLedColor(uint8_t led, uint8_t r, uint8_t g, uint8_t b)
{
_fs_switch_color_mask |= (1 << index);
_fs_switch_colors[index] = col;
uint8_t* pixel = &_led_colors[led * WS2812_BYTES_PER_LED];
pixel[0] = g;
pixel[1] = r;
pixel[2] = b;
}
uint32_t rgbGetLedColor(uint8_t led)
{
uint8_t* pixel = &_led_colors[led * WS2812_BYTES_PER_LED];
return (pixel[1] << 16) + (pixel[0] << 8) + pixel[2];
}
void fsLedRGB(uint8_t idx, uint32_t color)
{
rgbSetLedColor(idx, (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
}
uint32_t fsGetLedRGB(uint8_t index)
{
return _fs_switch_colors[index];
return rgbGetLedColor(index);
}
bool fsLedIsColorSet(uint8_t index)
{
return _fs_switch_color_mask & (1 << index);
}
bool fsLedState(uint8_t index)
{
return _fs_switch_mask & (1 << index);
}
void fsLedOn(uint8_t index)
{
_fs_switch_mask |= (1 << index);
}
void fsLedOff(uint8_t index)
{
_fs_switch_mask &= ~(1 << index);
}
#endif
@@ -30,6 +30,7 @@
#include "hal/adc_driver.h"
#include "hal/rotary_encoder.h"
#include "os/time.h"
#include "boards/generic_stm32/rgb_leds.h"
#include <QDebug>
#include <QElapsedTimer>
@@ -90,6 +91,16 @@ void firmwareTraceCb(const char * text)
}
}
void fsLedOn(uint8_t idx)
{
rgbSetLedColor(idx, 0xFF, 0xFF, 0xFF);
}
void fsLedOff(uint8_t idx)
{
rgbSetLedColor(idx, 0, 0, 0);
}
// Serial port handling needs to know about OpenTxSimulator, so we we
// need to update what's in simpgmspace.cpp when we have a simulator
// to point at.
@@ -751,6 +762,7 @@ void OpenTxSimulator::run()
++loops;
checkLcdChanged();
checkFuncSwitchChanged();
if (!(loops % 5)) {
checkOutputsChanged();
@@ -783,6 +795,19 @@ bool OpenTxSimulator::checkLcdChanged()
return false;
}
void OpenTxSimulator::checkFuncSwitchChanged()
{
#if defined(FUNCTION_SWITCHES)
for (int i = 0; i < CPN_MAX_SWITCHES; i += 1) {
if (switchIsCustomSwitch(i)) {
uint8_t cfs = switchGetCustomSwitchIdx(i);
uint32_t c = rgbGetLedColor(cfs);
emit fsColorChange(i, c);
}
}
#endif
}
void OpenTxSimulator::checkOutputsChanged()
{
static TxOutputs lastOutputs;
+1
View File
@@ -102,6 +102,7 @@ class DLLEXPORT OpenTxSimulator : public SimulatorInterface
void setStopRequested(bool stop);
bool checkLcdChanged();
void checkOutputsChanged();
void checkFuncSwitchChanged();
uint8_t getStickMode();
const char * getPhaseName(unsigned int phase);
const QString getCurrentPhaseName();
+44 -37
View File
@@ -65,6 +65,7 @@
#include "simpgmspace.h"
#include "hal/key_driver.h"
#include "hal/switch_driver.h"
#include "switches.h"
#include "audio.h"
@@ -316,26 +317,28 @@ static void draw_switches()
int sw_idx = 0;
for (int i = 0; i < switchGetMaxSwitches(); i++) {
if (sw_idx) ImGui::SameLine();
if (++sw_idx >= MAX_SWITCHES / 2) sw_idx = 0;
if (!SWITCH_EXISTS(i)) {
switches[i] = 0;
ImGui::Dummy(sw_size);
} else {
ImGui::PushID(i);
ImGui::VSliderInt("##sw", sw_size,
&switches[i], IS_CONFIG_3POS(i) ? 2 : 1,
0, "", ImGuiSliderFlags_NoInput);
if (ImGui::IsItemActive() || ImGui::IsItemHovered()) {
ImGui::SetTooltip("%s", switchGetCanonicalName(i));
if (!switchIsCustomSwitch(i)) {
if (++sw_idx >= MAX_SWITCHES / 2) sw_idx = 0;
if (!SWITCH_EXISTS(i)) {
switches[i] = 0;
ImGui::Dummy(sw_size);
} else {
ImGui::PushID(i);
ImGui::VSliderInt("##sw", sw_size,
&switches[i], IS_CONFIG_3POS(i) ? 2 : 1,
0, "", ImGuiSliderFlags_NoInput);
if (ImGui::IsItemActive() || ImGui::IsItemHovered()) {
ImGui::SetTooltip("%s", switchGetDefaultName(i));
}
ImGui::PopID();
}
if (IS_CONFIG_3POS(i)) {
simuSetSwitch(i, switches[i] == 0 ? -1 : switches[i] == 1 ? 0 : 1);
} else {
simuSetSwitch(i, switches[i] == 0 ? -1 : 1);
}
ImGui::PopID();
}
if (IS_CONFIG_3POS(i)) {
simuSetSwitch(i, switches[i] == 0 ? -1 : switches[i] == 1 ? 0 : 1);
} else {
simuSetSwitch(i, switches[i] == 0 ? -1 : 1);
}
}
@@ -346,8 +349,6 @@ static void draw_switches()
}
#if defined(FUNCTION_SWITCHES)
extern bool fsLedIsColorSet(uint8_t index);
static ImVec4 rgb2rgba(uint32_t col)
{
float r = (float)((col >> 16) & 0xff) / 255.0f;
@@ -365,9 +366,10 @@ static inline ImVec4 blend(const ImVec4& a, const ImVec4& b)
static void push_custom_switch_styles(int index)
{
bool sw_on = getFSLogicalState(index);
bool rgb_set = fsLedIsColorSet(index);
ImVec4 rgb = rgb2rgba(fsGetLedRGB(index));
int cfsIdx = switchGetCustomSwitchIdx(index);
bool sw_on = fsLedState(cfsIdx);
bool rgb_set = switchIsCustomSwitch(index);
ImVec4 rgb = rgb2rgba(fsGetLedRGB(cfsIdx));
ImVec4 btn, border;
if (sw_on) {
@@ -408,24 +410,29 @@ static void draw_custom_switches()
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 4.0f);
ImGui::PushStyleVar(ImGuiStyleVar_GrabRounding, 4.0f);
int sw_idx = switchGetMaxSwitches();
for (int i = 0; i < switchGetMaxFctSwitches(); i++, sw_idx++) {
if (i) ImGui::SameLine();
ImGui::PushID(i);
for (int i = 0, n = 0; i < switchGetMaxSwitches(); i += 1) {
if (switchIsCustomSwitch(i)) {
int cfsIdx = switchGetCustomSwitchIdx(i);
push_custom_switch_styles(i);
ImGui::Button("##csw", sw_size);
pop_custom_switch_styles();
if (n) ImGui::SameLine();
ImGui::PushID(i);
bool active = ImGui::IsItemActive();
if (active || ImGui::IsItemHovered()) {
const char* name = switchGetCanonicalName(sw_idx);
const char* on_off = getFSLogicalState(i) ? "on" : "off";
ImGui::SetTooltip("%s (%s)", name, on_off);
push_custom_switch_styles(i);
ImGui::Button("##csw", sw_size);
pop_custom_switch_styles();
bool active = ImGui::IsItemActive();
if (active || ImGui::IsItemHovered()) {
const char* name = switchGetDefaultName(i);
const char* on_off = fsLedState(cfsIdx) ? "on" : "off";
ImGui::SetTooltip("%s (%s)", name, on_off);
}
simuSetSwitch(i, active ? 1 : -1);
ImGui::PopID();
}
simuSetSwitch(sw_idx, active ? 1 : -1);
ImGui::PopID();
n += 1;
}
ImGui::PopStyleVar(3);
+20 -37
View File
@@ -29,17 +29,22 @@
#include <string.h>
struct hw_switch_def {
const char* name;
SwitchHwType type;
const char* name;
SwitchHwType type;
SwitchConfig defaultType;
#if defined(FUNCTION_SWITCHES)
bool isCustomSwitch;
uint8_t customSwitchIdx;
#endif
};
#include "simu_switches.inc"
int8_t switchesStates[MAX_SWITCHES] = { 0 };
int8_t switchesStates[MAX_SWITCHES];
void simuSetSwitch(uint8_t swtch, int8_t state)
{
assert(swtch < switchGetMaxSwitches() + switchGetMaxFctSwitches());
assert(swtch < switchGetMaxAllSwitches());
switchesStates[swtch] = state;
}
@@ -47,34 +52,8 @@ void boardInitSwitches() {
memset(switchesStates, -1, sizeof(switchesStates));
}
static uint8_t get_switch_index(uint8_t cat, uint8_t idx)
SwitchHwPos boardSwitchGetPosition(uint8_t idx)
{
switch(cat) {
case SWITCH_PHYSICAL:
assert(idx < n_switches);
return idx;
case SWITCH_FUNCTION:
assert(idx < n_fct_switches);
return idx + n_switches;
default:
assert(0);
return 0;
}
}
SwitchHwPos boardSwitchGetPosition(SwitchCategory cat, uint8_t idx)
{
idx = get_switch_index(cat, idx);
#if defined(FUNCTION_SWITCHES)
if (IS_SWITCH_FS(idx)) {
if (bfSingleBitGet(functionSwitchFunctionState, idx - n_switches))
return SWITCH_HW_DOWN;
}
#endif
if (switchesStates[idx] < 0)
return SWITCH_HW_UP;
else if (switchesStates[idx] == 0)
@@ -83,26 +62,30 @@ SwitchHwPos boardSwitchGetPosition(SwitchCategory cat, uint8_t idx)
return SWITCH_HW_DOWN;
}
const char* boardSwitchGetName(SwitchCategory cat, uint8_t idx)
const char* boardSwitchGetName(uint8_t idx)
{
idx = get_switch_index(cat, idx);
return _switch_defs[idx].name;
}
SwitchHwType boardSwitchGetType(SwitchCategory cat, uint8_t idx)
SwitchHwType boardSwitchGetType(uint8_t idx)
{
idx = get_switch_index(cat, idx);
return _switch_defs[idx].type;
}
uint8_t boardGetMaxSwitches() { return n_switches; }
uint8_t boardGetMaxFctSwitches() { return n_fct_switches; }
swconfig_t boardSwitchGetDefaultConfig() { return _switch_default_config; }
SwitchConfig boardSwitchGetDefaultConfig(uint8_t idx) { return _switch_defs[idx].defaultType; }
#if defined(FUNCTION_SWITCHES)
bool boardIsCustomSwitch(uint8_t idx) { return _switch_defs[idx].isCustomSwitch; }
uint8_t boardGetCustomSwitchIdx(uint8_t idx) { return _switch_defs[idx].customSwitchIdx; }
#endif
#if !defined(COLORLCD)
switch_display_pos_t switchGetDisplayPosition(uint8_t idx)
{
if (idx >= DIM(_switch_display)) return {0, 0};
return _switch_display[idx];
}
#endif