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
+1
View File
@@ -123,6 +123,7 @@ endif()
if(FUNCTION_SWITCHES)
add_gui_src(model/function_switches.cpp)
add_gui_src(radio/radio_cfs.cpp)
add_gui_src(radio/radio_diagcustswitches.cpp)
endif()
@@ -59,15 +59,14 @@ void SwitchWarnDialog::checkEvents()
FullScreenDialog::checkEvents();
std::string warn_txt;
swarnstate_t states = g_model.switchWarning;
for (int i = 0; i < MAX_SWITCHES; ++i) {
for (int i = 0; i < switchGetMaxAllSwitches(); ++i) {
if (SWITCH_WARNING_ALLOWED(i)) {
swarnstate_t mask = ((swarnstate_t)0x07 << (i * 3));
if (states & mask) {
if ((switches_states & mask) != (states & mask)) {
swarnstate_t state = (states >> (i * 3)) & 0x07;
uint8_t warnState = g_model.getSwitchWarning(i);
if (warnState) {
swarnstate_t swState = g_model.getSwitchStateForWarning(i);
if (warnState != swState) {
warn_txt +=
getSwitchPositionName(SWSRC_FIRST_SWITCH + i * 3 + state - 1);
getSwitchPositionName(SWSRC_FIRST_SWITCH + i * 3 + warnState - 1);
warn_txt += " ";
}
}
@@ -24,15 +24,16 @@
#include "function_switches.h"
#include "edgetx.h"
#include "hal/rgbleds.h"
#include "strhelpers.h"
#include "switches.h"
#include "color_picker.h"
#define SET_DIRTY() storageDirty(EE_MODEL)
static const char* _fct_sw_start[] = {STR_CHAR_DOWN, STR_CHAR_UP, STR_LAST};
const char* _fct_sw_start[] = {STR_CHAR_UP, STR_CHAR_DOWN, STR_LAST};
const std::string edgetx_fs_manual_url =
const char* edgetx_fs_manual_url =
"https://edgetx.gitbook.io/edgetx-user-manual/b-and-w-radios/model-select/"
"setup#function-switches";
@@ -45,65 +46,70 @@ class FunctionSwitch : public Window
padAll(PAD_TINY);
std::string s(STR_CHAR_SWITCH);
s += switchGetName(switchIndex + switchGetMaxSwitches());
s += switchGetDefaultName(switchIndex);
new StaticText(this, {PAD_LARGE, PAD_MEDIUM, SW_W, EdgeTxStyles::STD_FONT_HEIGHT}, s);
new ModelTextEdit(this, {NM_X, 0, NM_W, 0},
g_model.switchNames[switchIndex], LEN_SWITCH_NAME);
g_model.cfsName(switchIndex), LEN_SWITCH_NAME);
typeChoice = new Choice(
this, {TP_X, 0, TP_W, 0}, STR_SWTYPES, SWITCH_NONE, SWITCH_2POS,
[=]() { return FSWITCH_CONFIG(switchIndex); },
this, {TP_X, 0, TP_W, 0}, STR_SWTYPES, SWITCH_NONE, SWITCH_GLOBAL,
[=]() { return g_model.cfsType(switchIndex); },
[=](int val) {
FSWITCH_SET_CONFIG(switchIndex, val);
if (val == SWITCH_TOGGLE) {
FSWITCH_SET_STARTUP(switchIndex, FS_START_PREVIOUS);
g_model.cfsSetType(switchIndex, (SwitchConfig)val);
if (val == SWITCH_NONE) {
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
fsLedRGB(switchGetCustomSwitchIdx(switchIndex), 0);
#endif
} else if (val == SWITCH_TOGGLE) {
g_model.cfsSetStart(switchIndex, FS_START_PREVIOUS);
setFSLogicalState(switchIndex, 0);
startChoice->setValue(startChoice->getIntValue());
}
SET_DIRTY();
});
typeChoice->setAvailableHandler([=](int typ) -> bool {
int group = FSWITCH_GROUP(switchIndex);
if (group > 0 && IS_FSWITCH_GROUP_ON(group) && typ == SWITCH_TOGGLE)
if (typ == SWITCH_3POS) return false;
int group = g_model.cfsGroup(switchIndex);
if (group > 0 && g_model.cfsGroupAlwaysOn(group) && typ == SWITCH_TOGGLE)
return false;
return true;
});
groupChoice = new Choice(
this, {GR_X, 0, GR_W, 0}, STR_FUNCTION_SWITCH_GROUPS, 0, 3,
[=]() { return FSWITCH_GROUP(switchIndex); },
[=]() { return g_model.cfsGroup(switchIndex); },
[=](int group) {
int oldGroup = FSWITCH_GROUP(switchIndex);
int oldGroup = g_model.cfsGroup(switchIndex);
if (groupHasSwitchOn(group)) setFSLogicalState(switchIndex, 0);
FSWITCH_SET_GROUP(switchIndex, group);
g_model.cfsSetGroup(switchIndex, group);
if (group > 0) {
FSWITCH_SET_STARTUP(switchIndex, groupDefaultSwitch(group) == -1
g_model.cfsSetStart(switchIndex, groupDefaultSwitch(group) == -1
? FS_START_PREVIOUS
: FS_START_OFF);
if (FSWITCH_CONFIG(switchIndex) == SWITCH_TOGGLE &&
IS_FSWITCH_GROUP_ON(group))
FSWITCH_SET_CONFIG(switchIndex, SWITCH_2POS);
setGroupSwitchState(group, switchIndex);
if (g_model.cfsType(switchIndex) == SWITCH_TOGGLE &&
g_model.cfsGroupAlwaysOn(group))
g_model.cfsSetType(switchIndex, SWITCH_2POS);
setGroupSwitchState(group);
} else {
FSWITCH_SET_STARTUP(switchIndex, FS_START_PREVIOUS);
g_model.cfsSetStart(switchIndex, FS_START_PREVIOUS);
}
setGroupSwitchState(oldGroup);
SET_DIRTY();
});
groupChoice->setAvailableHandler([=](int group) -> bool {
if (FSWITCH_CONFIG(switchIndex) == SWITCH_TOGGLE && group &&
IS_FSWITCH_GROUP_ON(group))
if (g_model.cfsType(switchIndex) == SWITCH_TOGGLE && group &&
g_model.cfsGroupAlwaysOn(group))
return false;
return true;
});
startChoice = new Choice(
this, {ST_X, 0, ST_W, 0}, _fct_sw_start, 0, 2,
[=]() { return FSWITCH_STARTUP(switchIndex); },
[=]() { return g_model.cfsStart(switchIndex); },
[=](int val) {
FSWITCH_SET_STARTUP(switchIndex, val);
g_model.cfsSetStart(switchIndex, (fsStartPositionType)val);
SET_DIRTY();
});
@@ -113,23 +119,23 @@ class FunctionSwitch : public Window
new StaticText(this, rect_t{C2_X - C2_W - PAD_TINY, C2_Y + PAD_SMALL, C2_W, 0}, STR_ON_ONE_SWITCHES[0], COLOR_THEME_PRIMARY1_INDEX, FONT(XS) | RIGHT);
#endif
offValue = g_model.functionSwitchLedOFFColor[switchIndex];
onValue = g_model.functionSwitchLedONColor[switchIndex];
offValue = g_model.cfsOffColor(switchIndex);
onValue = g_model.cfsOnColor(switchIndex);
offColor = new ColorPicker(
this, {C1_X, C1_Y, C1_W, 0},
[=]() -> int { // getValue
return g_model.functionSwitchLedOFFColor[switchIndex].getColor() | RGB888_FLAG;
return g_model.cfsOffColor(switchIndex).getColor() | RGB888_FLAG;
},
[=](int newValue) { // setValue
g_model.functionSwitchLedOFFColor[switchIndex] = offValue;
g_model.functionSwitchLedONColor[switchIndex] = onValue;
g_model.cfsOffColor(switchIndex) = offValue;
g_model.cfsOnColor(switchIndex) = onValue;
// Convert color index to RGB
newValue = color32ToRGB(newValue);
g_model.functionSwitchLedOFFColor[switchIndex].setColor(newValue);
g_model.cfsOffColor(switchIndex).setColor(newValue);
offValue = g_model.functionSwitchLedOFFColor[switchIndex];
offValue = g_model.cfsOffColor(switchIndex);
SET_DIRTY();
},
[=](int newValue) { previewColor(newValue); }, ETX_RGB888);
@@ -137,20 +143,29 @@ class FunctionSwitch : public Window
onColor = new ColorPicker(
this, {C2_X, C2_Y, C2_W, 0},
[=]() -> int { // getValue
return g_model.functionSwitchLedONColor[switchIndex].getColor() | RGB888_FLAG;
return g_model.cfsOnColor(switchIndex).getColor() | RGB888_FLAG;
},
[=](int newValue) { // setValue
g_model.functionSwitchLedOFFColor[switchIndex] = offValue;
g_model.functionSwitchLedONColor[switchIndex] = onValue;
g_model.cfsOffColor(switchIndex) = offValue;
g_model.cfsOnColor(switchIndex) = onValue;
// Convert color index to RGB
newValue = color32ToRGB(newValue);
g_model.functionSwitchLedONColor[switchIndex].setColor(newValue);
g_model.cfsOnColor(switchIndex).setColor(newValue);
onValue = g_model.functionSwitchLedONColor[switchIndex];
onValue = g_model.cfsOnColor(switchIndex);
SET_DIRTY();
},
[=](int newValue) { previewColor(newValue); }, ETX_RGB888);
overrideLabel = new StaticText(this, {GR_X, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_LARGE, GR_W + ST_W - PAD_LARGE, 0},
STR_LUA_OVERRIDE, COLOR_THEME_PRIMARY1_INDEX, FONT(XS) | RIGHT);
offOverride = new ToggleSwitch(this, {C1_X - PAD_MEDIUM * 2, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE, 0, 0},
[=]() { return g_model.cfsOffColorLuaOverride(switchIndex); },
[=](bool v) { g_model.cfsSetOffColorLuaOverride(switchIndex, v); });
onOverride = new ToggleSwitch(this, {C2_X, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE, 0, 0},
[=]() { return g_model.cfsOnColorLuaOverride(switchIndex); },
[=](bool v) { g_model.cfsSetOnColorLuaOverride(switchIndex, v); });
#endif //FUNCTION_SWITCHES_RGB_LEDS
setState();
@@ -167,7 +182,7 @@ class FunctionSwitch : public Window
static constexpr coord_t ST_X = GR_X + GR_W + PAD_SMALL;
static LAYOUT_VAL_SCALED(ST_W, 60)
#if NARROW_LAYOUT
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT * 2 + PAD_OUTLINE * 3;
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT * 3 + PAD_OUTLINE * 4;
static constexpr coord_t C1_X = TP_X;
static constexpr coord_t C1_Y = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE;
static LAYOUT_VAL_SCALED(C1_W, 40)
@@ -175,7 +190,7 @@ class FunctionSwitch : public Window
static constexpr coord_t C2_Y = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE;
static LAYOUT_VAL_SCALED(C2_W, 40)
#else
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE * 2;
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT * 2 + PAD_OUTLINE * 3;
static constexpr coord_t C1_X = ST_X + ST_W + PAD_SMALL;
static constexpr coord_t C1_Y = 0;
static LAYOUT_VAL_SCALED(C1_W, 40)
@@ -206,6 +221,9 @@ class FunctionSwitch : public Window
ColorPicker* onColor = nullptr;
RGBLedColor offValue;
RGBLedColor onValue;
StaticText* overrideLabel = nullptr;
ToggleSwitch* onOverride = nullptr;
ToggleSwitch* offOverride = nullptr;
#endif
int lastType = -1;
@@ -214,29 +232,37 @@ class FunctionSwitch : public Window
{
// Convert color index to RGB
newValue = color32ToRGB(newValue);
if (getFSLogicalState(switchIndex)) {
g_model.functionSwitchLedONColor[switchIndex].setColor(newValue);
if (g_model.cfsState(switchIndex)) {
g_model.cfsOnColor(switchIndex).setColor(newValue);
} else {
g_model.functionSwitchLedOFFColor[switchIndex].setColor(newValue);
g_model.cfsOffColor(switchIndex).setColor(newValue);
}
}
#endif
void setState()
{
startChoice->show(FSWITCH_CONFIG(switchIndex) == SWITCH_2POS && FSWITCH_GROUP(switchIndex) == 0);
groupChoice->show(FSWITCH_CONFIG(switchIndex) != SWITCH_NONE);
uint8_t typ = g_model.cfsType(switchIndex);
startChoice->show(typ == SWITCH_2POS && g_model.cfsGroup(switchIndex) == 0);
groupChoice->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
offColor->show(FSWITCH_CONFIG(switchIndex) != SWITCH_NONE);
onColor->show(FSWITCH_CONFIG(switchIndex) != SWITCH_NONE);
offColor->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
onColor->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
overrideLabel->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
onOverride->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
offOverride->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
if (typ != SWITCH_NONE && typ != SWITCH_GLOBAL)
setHeight(ROW_H);
else
setHeight(ROW_H - EdgeTxStyles::UI_ELEMENT_HEIGHT - PAD_OUTLINE);
#endif
}
void checkEvents() override
{
setState();
if (lastType != (int)FSWITCH_CONFIG(switchIndex)) {
lastType = FSWITCH_CONFIG(switchIndex);
if (lastType != (int)g_model.cfsType(switchIndex)) {
lastType = g_model.cfsType(switchIndex);
typeChoice->setValue(lastType);
}
Window::checkEvents();
@@ -256,38 +282,43 @@ class SwitchGroup : public Window
auto btn = new TextButton(
this, {AO_X, 0, AO_W, 0}, STR_GROUP_ALWAYS_ON, [=]() -> int8_t {
int groupAlwaysOn = IS_FSWITCH_GROUP_ON(groupIndex);
int groupAlwaysOn = g_model.cfsGroupAlwaysOn(groupIndex);
groupAlwaysOn ^= 1;
SET_FSWITCH_GROUP_ON(groupIndex, groupAlwaysOn);
g_model.cfsSetGroupAlwaysOn(groupIndex, groupAlwaysOn);
setGroupSwitchState(groupIndex);
startChoice->setValue(startChoice->getIntValue());
SET_DIRTY();
return groupAlwaysOn;
});
btn->check(IS_FSWITCH_GROUP_ON(groupIndex));
btn->check(g_model.cfsGroupAlwaysOn(groupIndex));
new StaticText(this, {SL_X, PAD_MEDIUM, SL_W, EdgeTxStyles::STD_FONT_HEIGHT}, STR_SWITCH_STARTUP);
startChoice = new Choice(
this, {ST_X, 0, ST_W, 0}, STR_FSSWITCHES, 0,
NUM_FUNCTIONS_SWITCHES + 1,
this, {ST_X, 0, ST_W, 0}, 0, switchGetMaxSwitches() + 1,
[=]() { return groupDefaultSwitch(groupIndex) + 1; },
[=](int sw) {
for (int i = 0; i < NUM_FUNCTIONS_SWITCHES; i += 1) {
if (FSWITCH_GROUP(i) == groupIndex) {
FSWITCH_SET_STARTUP(i, sw ? FS_START_OFF : FS_START_PREVIOUS);
for (int i = 0; i < switchGetMaxSwitches(); i += 1) {
if (switchIsCustomSwitch(i) && g_model.cfsGroup(i) == groupIndex) {
g_model.cfsSetStart(i, (sw > 0) ? FS_START_OFF : FS_START_PREVIOUS);
}
}
if (sw > 0 && sw <= NUM_FUNCTIONS_SWITCHES) {
FSWITCH_SET_STARTUP(sw - 1, FS_START_ON);
if (sw > 0 && sw <= switchGetMaxSwitches()) {
g_model.cfsSetStart(sw - 1, FS_START_ON);
}
SET_DIRTY();
});
startChoice->setTextHandler([=](int sw) -> std::string {
if (sw == 0)
return "=";
if (sw == switchGetMaxSwitches() + 1)
return STR_OFF;
return switchGetDefaultName(sw - 1);
});
startChoice->setAvailableHandler([=](int sw) -> bool {
return (sw == 0) ||
(sw == NUM_FUNCTIONS_SWITCHES + 1 &&
!IS_FSWITCH_GROUP_ON(groupIndex)) ||
(FSWITCH_GROUP(sw - 1) == groupIndex);
(sw == switchGetMaxSwitches() + 1 && !g_model.cfsGroupAlwaysOn(groupIndex)) ||
(switchIsCustomSwitch(sw - 1) && g_model.cfsGroup(sw - 1) == groupIndex);
});
}
@@ -331,8 +362,9 @@ ModelFunctionSwitches::ModelFunctionSwitches() : Page(ICON_MODEL_SETUP)
new StaticText(box, rect_t{FunctionSwitch::C2_X + PAD_OUTLINE, 0, FunctionSwitch::C2_W, 0}, STR_ON_ONE_SWITCHES[0], COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
#endif
for (uint8_t i = 0; i < NUM_FUNCTIONS_SWITCHES; i += 1) {
new FunctionSwitch(body, i);
for (uint8_t i = 0; i < switchGetMaxSwitches(); i += 1) {
if (switchIsCustomSwitch(i))
new FunctionSwitch(body, i);
}
for (uint8_t i = 1; i <= 3; i += 1) {
@@ -349,8 +381,7 @@ ModelFunctionSwitches::ModelFunctionSwitches() : Page(ICON_MODEL_SETUP)
auto qr = lv_qrcode_create(box->getLvObj(), 150,
makeLvColor(COLOR_THEME_SECONDARY1),
makeLvColor(COLOR_THEME_SECONDARY3));
lv_qrcode_update(qr, edgetx_fs_manual_url.c_str(),
edgetx_fs_manual_url.length());
lv_qrcode_update(qr, edgetx_fs_manual_url, strlen(edgetx_fs_manual_url));
lv_obj_set_pos(qr, (LCD_W - 150) / 2, EdgeTxStyles::STD_FONT_HEIGHT);
#endif
@@ -38,7 +38,7 @@ class SwitchWarnMatrix : public ButtonMatrix
{
// Setup button layout & texts
uint8_t btn_cnt = 0;
for (uint8_t i = 0; i < MAX_SWITCHES; i++) {
for (uint8_t i = 0; i < switchGetMaxAllSwitches(); i++) {
if (SWITCH_WARNING_ALLOWED(i)) {
sw_idx[btn_cnt] = i;
btn_cnt++;
@@ -66,17 +66,16 @@ class SwitchWarnMatrix : public ButtonMatrix
return;
}
if (btn_id >= MAX_SWITCHES) return;
if (btn_id >= switchGetMaxAllSwitches()) return;
auto sw = sw_idx[btn_id];
swarnstate_t newstate = bfGet(g_model.switchWarning, 3 * sw, 3);
if (newstate == 1 && SWITCH_CONFIG(sw) != SWITCH_3POS)
uint8_t newstate = g_model.getSwitchWarning(sw);
if (newstate == 1 && g_model.getSwitchType(sw) != SWITCH_3POS)
newstate = 3;
else
newstate = (newstate + 1) % 4;
newstate = (newstate + 1) & 3;
g_model.switchWarning =
bfSet(g_model.switchWarning, newstate, 3 * sw, 3);
g_model.setSwitchWarning(sw, newstate);
SET_DIRTY();
setTextAndState(btn_id);
@@ -84,15 +83,15 @@ class SwitchWarnMatrix : public ButtonMatrix
bool isActive(uint8_t btn_id) override
{
if (btn_id >= MAX_SWITCHES) return false;
return bfGet(g_model.switchWarning, 3 * sw_idx[btn_id], 3) != 0;
if (btn_id >= switchGetMaxAllSwitches()) return false;
return g_model.getSwitchWarning(sw_idx[btn_id]) != 0;
}
void setTextAndState(uint8_t btn_id)
{
swsrc_t index = sw_idx[btn_id];
auto warn_pos = g_model.switchWarning >> (3 * index) & 0x07;
std::string s = std::string(switchGetName(index)) +
auto warn_pos = g_model.getSwitchWarning(index);
std::string s = std::string(switchGetDefaultName(index)) +
std::string(getSwitchWarnSymbol(warn_pos));
setText(btn_id, s.c_str());
setChecked(btn_id);
@@ -109,7 +108,7 @@ class SwitchWarnMatrix : public ButtonMatrix
void setAllState()
{
uint8_t btn_id = 0;
for (uint8_t i = 0; i < MAX_SWITCHES; i++) {
for (uint8_t i = 0; i < switchGetMaxAllSwitches(); i++) {
if (SWITCH_WARNING_ALLOWED(i)) {
setTextAndState(btn_id);
btn_id++;
@@ -220,7 +220,7 @@ void FunctionLineButton::refresh()
#if defined(FUNCTION_SWITCHES)
case FUNC_PUSH_CUST_SWITCH:
sprintf(s + strlen(s), "%s%d", STR_SWITCH, CFN_CS_INDEX(cfn) + 1);
strAppend(s + strlen(s), switchGetDefaultName(switchGetSwitchFromCustomIdx(CFN_CS_INDEX(cfn))));
break;
#endif
@@ -505,10 +505,15 @@ void FunctionEditPage::updateSpecialFunctionOneWindow()
#if defined(FUNCTION_SWITCHES)
case FUNC_PUSH_CUST_SWITCH: {
new StaticText(line, rect_t{}, STR_SWITCH);
auto choice = new Choice(line, rect_t{}, 0, NUM_FUNCTIONS_SWITCHES - 1, GET_SET_DEFAULT(CFN_CS_INDEX(cfn)), STR_SWITCH);
auto choice = new Choice(line, rect_t{}, 0, switchGetMaxSwitches() - 1,
[=]() { return switchGetSwitchFromCustomIdx(CFN_CS_INDEX(cfn)); },
[=](int n) { CFN_CS_INDEX(cfn) = switchGetCustomSwitchIdx(n); },
STR_SWITCH);
choice->setTextHandler([=](int n) {
return std::string(STR_SWITCH) + std::to_string(n + 1);
return std::string(switchGetDefaultName(n));
});
choice->setAvailableHandler(switchIsCustomSwitch);
line = specialFunctionOneWindow->newLine(grid);
auto edit = addNumberEdit(line, STR_INTERVAL, cfn, PUSH_CS_DURATION_MIN,
+7 -9
View File
@@ -171,7 +171,7 @@ class SwitchDynamicLabel : public StaticText
std::string label()
{
std::string str(switchGetCanonicalName(index));
std::string str(switchGetDefaultName(index));
return str + getSwitchPositionSymbol(lastpos);
}
@@ -206,7 +206,7 @@ class HWSwitch
HWSwitch(Window* parent, int swnum, coord_t y)
{
new SwitchDynamicLabel(parent, swnum, PAD_TINY, y + PAD_SMALL, HWSwitches::SW_CTRL_W);
new HWInputEdit(parent, (char*)switchGetCustomName(swnum), LEN_SWITCH_NAME,
new HWInputEdit(parent, g_eeGeneral.getSwitchCustomName(swnum), LEN_SWITCH_NAME,
HWSwitches::SW_CTRL_W + PAD_SMALL, y);
coord_t x = HWSwitches::SW_CTRL_W * 2 + PAD_SMALL * 2;
@@ -233,12 +233,9 @@ class HWSwitch
sw_cfg = new Choice(
parent, {x, y, HWSwitches::SW_CTRL_W, 0},
STR_SWTYPES, SWITCH_NONE, switchGetMaxType(swnum),
[=]() -> int { return SWITCH_CONFIG(swnum); },
[=]() -> int { return g_eeGeneral.switchType(swnum); },
[=](int newValue) {
swconfig_t mask = (swconfig_t)SWITCH_CONFIG_MASK(swnum);
g_eeGeneral.switchConfig =
(g_eeGeneral.switchConfig & ~mask) |
((swconfig_t(newValue) & SW_CFG_MASK) << (SW_CFG_BITS * swnum));
g_eeGeneral.switchSetType(swnum, (SwitchConfig)newValue);
SET_DIRTY();
});
@@ -266,9 +263,10 @@ HWSwitches::HWSwitches(Window* parent) :
{
padAll(PAD_TINY);
auto max_switches = switchGetMaxSwitches();
auto max_switches = switchGetMaxAllSwitches();
for (int i = 0; i < max_switches; i++) {
new HWSwitch(this, i, i * HWSwitch::SW_CTRL_H + PAD_OUTLINE);
if (!switchIsCustomSwitch(i))
new HWSwitch(this, i, i * HWSwitch::SW_CTRL_H + PAD_OUTLINE);
}
}
+318
View File
@@ -0,0 +1,318 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#if defined(FUNCTION_SWITCHES)
#include "radio_cfs.h"
#include "edgetx.h"
#include "hal/rgbleds.h"
#include "strhelpers.h"
#include "switches.h"
#include "color_picker.h"
#define SET_DIRTY() storageDirty(EE_GENERAL)
static const lv_coord_t cfs_line_col_dsc1[] = {LV_GRID_CONTENT,
LV_GRID_TEMPLATE_LAST};
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
static const lv_coord_t cfs_line_col_dsc2[] = {
LV_GRID_FR(13), LV_GRID_FR(11), LV_GRID_FR(11),
LV_GRID_FR(12), LV_GRID_FR(10), LV_GRID_FR(7), LV_GRID_FR(7),
LV_GRID_TEMPLATE_LAST};
#else
static const lv_coord_t cfs_line_col_dsc2[] = {
LV_GRID_FR(10), LV_GRID_FR(10), LV_GRID_FR(10),
LV_GRID_FR(12), LV_GRID_FR(8), LV_GRID_TEMPLATE_LAST};
#endif
static const lv_coord_t cfs_line_row_dsc[] = {LV_GRID_CONTENT,
LV_GRID_TEMPLATE_LAST};
extern const char* _fct_sw_start[];
extern const char* edgetx_fs_manual_url;
class RadioFunctionSwitch : public Window
{
public:
RadioFunctionSwitch(Window* parent, uint8_t sw) :
Window(parent, {0, 0, LCD_W - PAD_SMALL * 2, ROW_H}), switchIndex(sw)
{
padAll(PAD_TINY);
std::string s(STR_CHAR_SWITCH);
s += switchGetDefaultName(switchIndex);
new StaticText(this, {PAD_LARGE, PAD_MEDIUM, SW_W, EdgeTxStyles::STD_FONT_HEIGHT}, s);
new ModelTextEdit(this, {NM_X, 0, NM_W, 0},
g_eeGeneral.switchName(switchIndex), LEN_SWITCH_NAME);
typeChoice = new Choice(
this, {TP_X, 0, TP_W, 0}, STR_SWTYPES, SWITCH_NONE, SWITCH_GLOBAL,
[=]() { return g_eeGeneral.switchType(switchIndex); },
[=](int val) {
g_eeGeneral.switchSetType(switchIndex, (SwitchConfig)val);
if (val == SWITCH_NONE) {
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
if (g_model.getSwitchType(switchIndex) == SWITCH_NONE)
fsLedRGB(switchGetCustomSwitchIdx(switchIndex), 0);
#endif
} else if (val == SWITCH_TOGGLE) {
g_eeGeneral.switchSetStart(switchIndex, FS_START_PREVIOUS);
setFSLogicalState(switchIndex, 0);
startChoice->setValue(startChoice->getIntValue());
}
SET_DIRTY();
});
typeChoice->setAvailableHandler([=](int typ) -> bool {
if (typ == SWITCH_3POS || typ == SWITCH_GLOBAL) return false;
int group = g_eeGeneral.switchGroup(switchIndex);
if (group > 0 && g_model.cfsGroupAlwaysOn(group) && typ == SWITCH_TOGGLE)
return false;
return true;
});
startChoice = new Choice(
this, {ST_X, 0, ST_W, 0}, _fct_sw_start, 0, 2,
[=]() { return g_eeGeneral.switchStart(switchIndex); },
[=](int val) {
g_eeGeneral.switchSetStart(switchIndex, (fsStartPositionType)val);
SET_DIRTY();
});
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
#if NARROW_LAYOUT
new StaticText(this, rect_t{C1_X - C1_W - PAD_TINY, C1_Y + PAD_SMALL, C1_W, 0}, STR_OFF, COLOR_THEME_PRIMARY1_INDEX, FONT(XS) | RIGHT);
new StaticText(this, rect_t{C2_X - C2_W - PAD_TINY, C2_Y + PAD_SMALL, C2_W, 0}, STR_ON_ONE_SWITCHES[0], COLOR_THEME_PRIMARY1_INDEX, FONT(XS) | RIGHT);
#endif
offValue = g_eeGeneral.switchOffColor(switchIndex);
onValue = g_eeGeneral.switchOnColor(switchIndex);
offColor = new ColorPicker(
this, {C1_X, 0, C1_W, 0},
[=]() -> int { // getValue
return g_eeGeneral.switchOffColor(switchIndex).getColor() | RGB888_FLAG;
},
[=](int newValue) { // setValue
g_eeGeneral.switchOffColor(switchIndex) = offValue;
g_eeGeneral.switchOnColor(switchIndex) = onValue;
// Convert color index to RGB
newValue = color32ToRGB(newValue);
g_eeGeneral.switchOffColor(switchIndex).setColor(newValue);
offValue = g_eeGeneral.switchOffColor(switchIndex);
SET_DIRTY();
},
[=](int newValue) { previewColor(newValue); }, ETX_RGB888);
onColor = new ColorPicker(
this, {C2_X, 0, C2_W, 0},
[=]() -> int { // getValue
return g_eeGeneral.switchOnColor(switchIndex).getColor() | RGB888_FLAG;
},
[=](int newValue) { // setValue
g_eeGeneral.switchOffColor(switchIndex) = offValue;
g_eeGeneral.switchOnColor(switchIndex) = onValue;
// Convert color index to RGB
newValue = color32ToRGB(newValue);
g_eeGeneral.switchOnColor(switchIndex).setColor(newValue);
onValue = g_eeGeneral.switchOnColor(switchIndex);
SET_DIRTY();
},
[=](int newValue) { previewColor(newValue); }, ETX_RGB888);
overrideLabel = new StaticText(this, {GR_X, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_LARGE, GR_W + ST_W - PAD_LARGE, 0},
STR_LUA_OVERRIDE, COLOR_THEME_PRIMARY1_INDEX, FONT(XS) | RIGHT);
offOverride = new ToggleSwitch(this, {C1_X - PAD_MEDIUM * 2, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE, 0, 0},
[=]() { return g_model.cfsOffColorLuaOverride(switchIndex); },
[=](bool v) { g_model.cfsSetOffColorLuaOverride(switchIndex, v); });
onOverride = new ToggleSwitch(this, {C2_X, C1_Y + EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE, 0, 0},
[=]() { return g_model.cfsOnColorLuaOverride(switchIndex); },
[=](bool v) { g_model.cfsSetOnColorLuaOverride(switchIndex, v); });
#endif //FUNCTION_SWITCHES_RGB_LEDS
setState();
}
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
static LAYOUT_VAL_SCALED(SW_W, 70)
static constexpr coord_t NM_X = SW_W + PAD_SMALL;
static LAYOUT_VAL_SCALED(NM_W, 60)
static constexpr coord_t TP_X = NM_X + NM_W + PAD_SMALL;
static LAYOUT_VAL_SCALED(TP_W, 78)
static constexpr coord_t GR_X = TP_X + TP_W + PAD_SMALL;
static LAYOUT_VAL_SCALED(GR_W, 84)
static constexpr coord_t ST_X = GR_X + GR_W + PAD_SMALL;
static LAYOUT_VAL_SCALED(ST_W, 60)
#if NARROW_LAYOUT
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT * 3 + PAD_OUTLINE * 4;
static constexpr coord_t C1_X = TP_X;
static constexpr coord_t C1_Y = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE;
static LAYOUT_VAL_SCALED(C1_W, 40)
static constexpr coord_t C2_X = GR_X;
static constexpr coord_t C2_Y = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE;
static LAYOUT_VAL_SCALED(C2_W, 40)
#else
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT * 2 + PAD_OUTLINE * 3;
static constexpr coord_t C1_X = ST_X + ST_W + PAD_SMALL;
static constexpr coord_t C1_Y = 0;
static LAYOUT_VAL_SCALED(C1_W, 40)
static constexpr coord_t C2_X = C1_X + C1_W + PAD_SMALL;
static constexpr coord_t C2_Y = 0;
static LAYOUT_VAL_SCALED(C2_W, 40)
#endif
#else
static constexpr coord_t ROW_H = EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_OUTLINE * 2;
static constexpr coord_t SW_W = (LCD_W - PAD_SMALL * 2 - PAD_TINY * 4) / 5;
static constexpr coord_t NM_X = SW_W + PAD_TINY;
static LAYOUT_VAL_SCALED(NM_W, 80)
static constexpr coord_t TP_X = NM_X + SW_W + PAD_TINY;
static LAYOUT_VAL_SCALED(TP_W, 86)
static constexpr coord_t GR_X = TP_X + SW_W + PAD_TINY;
static LAYOUT_VAL_SCALED(GR_W, 94)
static constexpr coord_t ST_X = GR_X + SW_W + PAD_LARGE * 2 + PAD_SMALL;
static LAYOUT_VAL_SCALED(ST_W, 70)
#endif
protected:
uint8_t switchIndex;
Choice* typeChoice = nullptr;
Choice* startChoice = nullptr;
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
ColorPicker* offColor = nullptr;
ColorPicker* onColor = nullptr;
RGBLedColor offValue;
RGBLedColor onValue;
StaticText* overrideLabel = nullptr;
ToggleSwitch* onOverride = nullptr;
ToggleSwitch* offOverride = nullptr;
#endif
int lastType = -1;
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
void previewColor(int newValue)
{
// Convert color index to RGB
newValue = color32ToRGB(newValue);
if (g_model.cfsState(switchIndex)) {
g_eeGeneral.switchOnColor(switchIndex).setColor(newValue);
} else {
g_eeGeneral.switchOffColor(switchIndex).setColor(newValue);
}
}
#endif
void setState()
{
uint8_t typ = g_eeGeneral.switchType(switchIndex);
startChoice->show(typ == SWITCH_2POS && g_eeGeneral.switchGroup(switchIndex) == 0);
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
offColor->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
onColor->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
overrideLabel->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
onOverride->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
offOverride->show(typ != SWITCH_NONE && typ != SWITCH_GLOBAL);
if (typ != SWITCH_NONE && typ != SWITCH_GLOBAL)
setHeight(ROW_H);
else
setHeight(ROW_H - EdgeTxStyles::UI_ELEMENT_HEIGHT - PAD_OUTLINE);
#endif
}
void checkEvents() override
{
setState();
if (lastType != (int)g_eeGeneral.switchType(switchIndex)) {
lastType = g_eeGeneral.switchType(switchIndex);
typeChoice->setValue(lastType);
}
Window::checkEvents();
}
};
RadioFunctionSwitches::RadioFunctionSwitches() : Page(ICON_RADIO_HARDWARE)
{
header->setTitle(STR_HARDWARE);
header->setTitle2(STR_MENU_FSWITCH);
body->padAll(PAD_TINY);
body->setFlexLayout(LV_FLEX_FLOW_COLUMN, PAD_ZERO);
FlexGridLayout grid1(cfs_line_col_dsc1, cfs_line_row_dsc, PAD_TINY);
FlexGridLayout grid2(cfs_line_col_dsc2, cfs_line_row_dsc, PAD_TINY);
auto line = body->newLine(grid2);
new StaticText(line, rect_t{}, STR_SWITCHES);
new StaticText(line, rect_t{}, STR_NAME, COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
new StaticText(line, rect_t{}, STR_SWITCH_TYPE,
COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
grid2.nextColumn();
// new StaticText(line, rect_t{}, STR_GROUP, COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
startupHeader = new StaticText(line, rect_t{}, STR_SWITCH_STARTUP,
COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
new StaticText(line, rect_t{}, STR_OFF, COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
new StaticText(line, rect_t{}, STR_ON_ONE_SWITCHES[0], COLOR_THEME_PRIMARY1_INDEX, FONT(XS));
#endif
for (uint8_t i = 0; i < switchGetMaxSwitches(); i += 1) {
if (switchIsCustomSwitch(i))
new RadioFunctionSwitch(body, i);
}
setState();
line = body->newLine(grid1);
new StaticText(line, rect_t{}, STR_MORE_INFO);
line = body->newLine(grid1);
line->padBottom(PAD_LARGE);
line->padLeft((width() - 150) / 2);
auto qr = lv_qrcode_create(line->getLvObj(), 150,
makeLvColor(COLOR_THEME_SECONDARY1),
makeLvColor(COLOR_THEME_SECONDARY3));
lv_qrcode_update(qr, edgetx_fs_manual_url, strlen(edgetx_fs_manual_url));
}
void RadioFunctionSwitches::setState()
{
// int cnt = 0;
// for (int i = 1; i <= 3; i += 1) {
// cnt += getSwitchCountInFSGroup(i);
// }
// startupHeader->show(cnt != NUM_FUNCTIONS_SWITCHES);
}
void RadioFunctionSwitches::checkEvents()
{
setState();
Page::checkEvents();
}
#endif
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#if defined(FUNCTION_SWITCHES)
#include "page.h"
class RadioFunctionSwitches : public Page
{
public:
RadioFunctionSwitches();
protected:
BitmapBuffer* qrcode = nullptr;
StaticText* startupHeader = nullptr;
void setState();
void checkEvents() override;
};
#endif
@@ -65,36 +65,40 @@ class RadioCustSwitchesDiagsWindow : public Window
"Log");
new StaticText(this, {FS_3RD_COLUMN, PAD_SMALL, FS_LBL_WIDTH, LV_SIZE_CONTENT},
"Led");
for (uint8_t i = 0; i < NUM_FUNCTIONS_SWITCHES; i += 1) {
coord_t y = (i + 2) * EdgeTxStyles::STD_FONT_HEIGHT;
std::string s(STR_CHAR_SWITCH);
s += switchGetName(i + switchGetMaxSwitches());
new StaticText(this, {PAD_LARGE, y, FS_LBL_WIDTH, LV_SIZE_CONTENT}, s);
new DynamicText(
this, {FS_1ST_COLUMN + PAD_LARGE, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() {
return getFSPhysicalState(i) ? STR_CHAR_DOWN : STR_CHAR_UP;
});
new DynamicText(
this, {FS_2ND_COLUMN + PAD_LARGE, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() { return getFSLogicalState(i) ? STR_CHAR_DOWN : STR_CHAR_UP; });
for (uint8_t i = 0, r = 0; i < switchGetMaxSwitches(); i += 1) {
if (switchIsCustomSwitch(i)) {
coord_t y = (r + 2) * EdgeTxStyles::STD_FONT_HEIGHT;
std::string s(STR_CHAR_SWITCH);
s += switchGetDefaultName(i);
new StaticText(this, {PAD_LARGE, y, FS_LBL_WIDTH, LV_SIZE_CONTENT}, s);
new DynamicText(
this, {FS_1ST_COLUMN + PAD_LARGE, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() {
return getFSPhysicalState(i) ? STR_CHAR_DOWN : STR_CHAR_UP;
});
new DynamicText(
this, {FS_2ND_COLUMN + 10, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() { return g_model.cfsState(i) ? STR_CHAR_DOWN : STR_CHAR_UP; });
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
colorBox[i] = new ColorSwatch(this, {FS_3RD_COLUMN, y, FS_COLOR_WIDTH,
FS_COLOR_HEIGHT}, getLedColor(i));
colorBox[r] = new ColorSwatch(this, {FS_3RD_COLUMN, y, FS_COLOR_WIDTH,
FS_COLOR_HEIGHT}, getLedColor(r));
#else
new DynamicText(this,
{FS_3RD_COLUMN, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() { return STR_OFFON[getFSLedState(i)]; });
new DynamicText(this,
{FS_3RD_COLUMN, y, FS_LBL_WIDTH, LV_SIZE_CONTENT},
[=]() { return STR_OFFON[getFSLedState(i)]; });
#endif
r += 1;
}
}
}
#if defined(FUNCTION_SWITCHES_RGB_LEDS)
void checkEvents() override {
Window::checkEvents();
for (uint8_t i = 0; i < NUM_FUNCTIONS_SWITCHES; i += 1) {
colorBox[i]->setColor(getLedColor(i));
if (colorBox[i])
colorBox[i]->setColor(getLedColor(i));
}
}
#endif
@@ -125,14 +125,14 @@ class RadioKeyDiagsWindow : public Window
void addSwitches(Window *form)
{
switchValues = new lv_obj_t *[switchGetMaxSwitches()];
switchValues = new lv_obj_t *[switchGetMaxAllSwitches()];
lv_obj_t *obj = form->getLvObj();
uint8_t i;
uint8_t row = 0;
// SWITCHES
for (i = 0; i < switchGetMaxSwitches(); i++) {
if (SWITCH_EXISTS(i)) {
for (i = 0; i < switchGetMaxAllSwitches(); i++) {
if (SWITCH_EXISTS(i) && !switchIsCustomSwitch(i)) {
auto lbl = lv_label_create(obj);
lv_label_set_text(lbl, "");
lv_obj_set_pos(lbl, 0, row * EdgeTxStyles::STD_FONT_HEIGHT);
@@ -197,8 +197,8 @@ class RadioKeyDiagsWindow : public Window
{
uint8_t i;
for (i = 0; i < switchGetMaxSwitches(); i++) {
if (SWITCH_EXISTS(i)) {
for (i = 0; i < switchGetMaxAllSwitches(); i++) {
if (SWITCH_EXISTS(i) && !switchIsCustomSwitch(i)) {
getvalue_t val = getValue(MIXSRC_FIRST_SWITCH + i);
getvalue_t sw =
((val < 0) ? 3 * i + 1 : ((val == 0) ? 3 * i + 2 : 3 * i + 3));
@@ -34,6 +34,7 @@
#include "radio_setup.h"
#if defined(FUNCTION_SWITCHES)
#include "radio_cfs.h"
#include "radio_diagcustswitches.h"
#endif
@@ -191,6 +192,9 @@ void RadioHardwarePage::build(Window* window)
{STR_STICKS, []() { new HWInputDialog<HWSticks>(STR_STICKS); }},
{STR_POTS, []() { new HWInputDialog<HWPots>(STR_POTS, HWPots::POTS_WINDOW_WIDTH); }},
{STR_SWITCHES, []() { new HWInputDialog<HWSwitches>(STR_SWITCHES, HWSwitches::SW_WINDOW_WIDTH); }},
#if defined(FUNCTION_SWITCHES)
{STR_FUNCTION_SWITCHES, []() { new RadioFunctionSwitches(); }},
#endif
});
// Debugs
@@ -32,7 +32,11 @@ class RadioHardwarePage : public PageTab
void build(Window* window) override;
#if defined(FUNCTION_SWITCHES)
static LAYOUT_SIZE(BTN_COLS, 2, 2)
#else
static LAYOUT_SIZE(BTN_COLS, 4, 3)
#endif
static LAYOUT_SIZE(FS_BTN_COLS, 2, 2)
protected: