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
+25 -42
View File
@@ -219,12 +219,7 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
// Startup switches warnings
for (int i = 0; i < Boards::getBoardCapability(board, Board::Switches); i++) {
GeneralSettings::SwitchConfig &swcfg = generalSettings.switchConfig[i];
if (Boards::isSwitchFunc(i, board) || !generalSettings.isSwitchAvailable(i) || swcfg.type == Board::SWITCH_TOGGLE) {
model.switchWarningEnable |= (1 << i);
continue;
}
Board::SwitchType swType = model.getSwitchType(i, generalSettings);
RawSource src(RawSourceType::SOURCE_TYPE_SWITCH, i + 1);
QLabel * label = new QLabel(this);
@@ -242,7 +237,7 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
slider->setPageStep(1);
slider->setTickInterval(1);
label->setText(src.toString(&model, &generalSettings));
slider->setMaximum(swcfg.type == Board::SWITCH_3POS ? 2 : 1);
slider->setMaximum(swType == Board::SWITCH_3POS ? 2 : 1);
cb->setProperty("index", i);
ui->switchesStartupLayout->addWidget(label, 0, i + 1);
ui->switchesStartupLayout->setAlignment(label, Qt::AlignCenter);
@@ -252,6 +247,7 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge
ui->switchesStartupLayout->setAlignment(cb, Qt::AlignCenter);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(startupSwitchEdited(int)));
connect(cb, SIGNAL(toggled(bool)), this, SLOT(startupSwitchToggled(bool)));
startupSwitchesLabels << label;
startupSwitchesSliders << slider;
startupSwitchesCheckboxes << cb;
QWidget::setTabOrder(prevFocus, slider);
@@ -338,6 +334,12 @@ SetupPanel::~SetupPanel()
delete panelItemModels;
}
bool SetupPanel::canSwitchHaveWarning(int sw)
{
Board::SwitchType type = model->getSwitchType(sw, generalSettings);
return (type == Board::SWITCH_2POS) || (type == Board::SWITCH_3POS);
}
void SetupPanel::on_extendedLimits_toggled(bool checked)
{
model->extendedLimits = checked;
@@ -510,23 +512,20 @@ void SetupPanel::updateStartupSwitches()
uint64_t value;
for (int i = 0; i < startupSwitchesSliders.size(); i++) {
QLabel * label = startupSwitchesLabels[i];
QSlider * slider = startupSwitchesSliders[i];
QCheckBox * cb = startupSwitchesCheckboxes[i];
int index = slider->property("index").toInt();
bool enabled = !(model->switchWarningEnable & (1 << index));
if (IS_HORUS_OR_TARANIS(firmware->getBoard())) {
value = (switchStates >> (2 * index)) & 0x03;
if (generalSettings.switchConfig[index].type != Board::SWITCH_3POS && value == 2) {
value = 1;
}
}
else {
value = (i == 0 ? switchStates & 0x3 : switchStates & 0x1);
switchStates >>= (i == 0 ? 2 : 1);
}
slider->setValue(value);
value = (switchStates >> (2 * index)) & 0x03;
bool enabled = value != 0;
if (model->getSwitchType(i, generalSettings) == Board::SWITCH_2POS && value == 3) value = 2;
slider->setValue((value > 0) ? value - 1 : 0);
slider->setEnabled(enabled);
cb->setChecked(enabled);
bool vis = canSwitchHaveWarning(i);
label->setVisible(vis);
slider->setVisible(vis);
cb->setVisible(vis);
}
lock = false;
@@ -539,31 +538,14 @@ void SetupPanel::startupSwitchEdited(int value)
uint64_t mask;
int index = sender()->property("index").toInt();
if (IS_HORUS_OR_TARANIS(firmware->getBoard())) {
shift = index * 2;
mask = 0x03ull << shift;
}
else {
if (index == 0) {
mask = 0x03;
}
else {
shift = index + 1;
mask = 0x01ull << shift;
}
}
shift = index * 2;
mask = 0x03ull << shift;
model->switchWarningStates &= ~mask;
if (IS_HORUS_OR_TARANIS(firmware->getBoard()) && generalSettings.switchConfig[index].type != Board::SWITCH_3POS) {
if (value == 1) {
value = 2;
}
}
if (model->getSwitchType(index, generalSettings) == Board::SWITCH_2POS && value == 1) value = 2;
if (value) {
model->switchWarningStates |= ((uint64_t)value << shift);
}
model->switchWarningStates |= ((uint64_t)(value + 1) << shift);
updateStartupSwitches();
emit modified();
@@ -576,9 +558,9 @@ void SetupPanel::startupSwitchToggled(bool checked)
int index = sender()->property("index").toInt();
if (checked)
model->switchWarningEnable &= ~(1 << index);
model->switchWarningStates |= (1 << (index * 2));
else
model->switchWarningEnable |= (1 << index);
model->switchWarningStates &= ~(3 << (index * 2));
updateStartupSwitches();
emit modified();
@@ -866,5 +848,6 @@ void SetupPanel::onModuleUpdateItemModels()
void SetupPanel::onFunctionSwitchesUpdateItemModels()
{
updateStartupSwitches();
sharedItemModels->update(AbstractItemModel::IMUE_FunctionSwitches);
}