Skip to content

Commit

Permalink
feat: customisable switch generated source groups (#5016)
Browse files Browse the repository at this point in the history
Co-authored-by: elecpower <neilh713@tpg.com.au>
  • Loading branch information
3djc and elecpower authored Jun 7, 2024
1 parent 26c96cc commit 280d3af
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 5 deletions.
1 change: 1 addition & 0 deletions companion/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define CPN_MAX_SCRIPT_INPUTS 10
#define CPN_MAX_SPACEMOUSE 6
#define CPN_MAX_INPUTS 32 // v2.10 replaces CPN_MAX_ANALOGS - the value is abitary as radio ADC refactor is still a WIP
#define CPN_MAX_FUNCTIONSWITCH_GROUP 3

// pre v2.10
#define CPN_MAX_POTS 8
Expand Down
2 changes: 2 additions & 0 deletions companion/src/datamodels/compounditemmodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ RawSourceItemModel::RawSourceItemModel(const GeneralSettings * const generalSett
addItems(SOURCE_TYPE_PPM, RawSource::SourcesGroup, -firmware->getCapability(TrainerInputs));
addItems(SOURCE_TYPE_CYC, RawSource::SourcesGroup, -CPN_MAX_CYC);
addItems(SOURCE_TYPE_CUSTOM_SWITCH, RawSource::SwitchesGroup, -firmware->getCapability(LogicalSwitches));
addItems(SOURCE_TYPE_FUNCTIONSWITCH_GROUP, RawSource::SourcesGroup, -CPN_MAX_FUNCTIONSWITCH_GROUP);
addItems(SOURCE_TYPE_SWITCH, RawSource::SwitchesGroup, -board->getCapability(Board::Switches));
addItems(SOURCE_TYPE_MAX, RawSource::SourcesGroup, -1);
addItems(SOURCE_TYPE_MIN, RawSource::SourcesGroup, -1);
Expand All @@ -136,6 +137,7 @@ RawSourceItemModel::RawSourceItemModel(const GeneralSettings * const generalSett
addItems(SOURCE_TYPE_MIN, RawSource::SourcesGroup, 1);
addItems(SOURCE_TYPE_MAX, RawSource::SourcesGroup, 1);
addItems(SOURCE_TYPE_SWITCH, RawSource::SwitchesGroup, board->getCapability(Board::Switches));
addItems(SOURCE_TYPE_FUNCTIONSWITCH_GROUP, RawSource::SourcesGroup, CPN_MAX_FUNCTIONSWITCH_GROUP);
addItems(SOURCE_TYPE_CUSTOM_SWITCH, RawSource::SwitchesGroup, firmware->getCapability(LogicalSwitches));
addItems(SOURCE_TYPE_CYC, RawSource::SourcesGroup, CPN_MAX_CYC);
addItems(SOURCE_TYPE_PPM, RawSource::SourcesGroup, firmware->getCapability(TrainerInputs));
Expand Down
11 changes: 11 additions & 0 deletions companion/src/firmwares/edgetx/yaml_rawsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ std::string YamlRawSourceEncode(const RawSource& rhs)
case SOURCE_TYPE_VIRTUAL_INPUT:
src_str += "I" + std::to_string(sval - 1);
break;
case SOURCE_TYPE_FUNCTIONSWITCH_GROUP:
src_str += "GR" + std::to_string(sval);
break;
case SOURCE_TYPE_LUA_OUTPUT:
qr = div(sval - 1, 16);
src_str += "lua(";
Expand Down Expand Up @@ -168,7 +171,15 @@ RawSource YamlRawSourceDecode(const std::string& src_str)
if (idx >= 0) {
rhs = RawSource(SOURCE_TYPE_SWITCH, idx + 1);
}
} else if ((val_len == 3 &&
val[0] == 'G' &&
val[1] >= 'R' &&
val[2] >= '1' && val[2] <= '3')) {

int idx = std::stoi(src_str_tmp.substr(2));
if (idx >= 0) {
rhs = RawSource(SOURCE_TYPE_FUNCTIONSWITCH_GROUP, idx);
}
} else if (val_len > 4 &&
val[0] == 'l' &&
val[1] == 'u' &&
Expand Down
9 changes: 9 additions & 0 deletions companion/src/firmwares/rawsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "eeprominterface.h"
#include "radiodata.h"
#include "modeldata.h"
#include "radiodataconversionstate.h"

#include <float.h>
Expand Down Expand Up @@ -255,6 +256,9 @@ QString RawSource::toString(const ModelData * model, const GeneralSettings * con
case SOURCE_TYPE_SPACEMOUSE:
return tr("sm%1").arg(QChar('A' + (index - 1)));

case SOURCE_TYPE_FUNCTIONSWITCH_GROUP:
return tr("GR%1").arg(index);

default:
return QString(CPN_STR_UNKNOWN_ITEM);
}
Expand Down Expand Up @@ -284,9 +288,14 @@ bool RawSource::isAvailable(const ModelData * const model, const GeneralSettings
if (type == SOURCE_TYPE_CH && abs(index) > CPN_MAX_CHNOUT)
return false;

if (type == SOURCE_TYPE_FUNCTIONSWITCH_GROUP && index >= b.getCapability(Board::FunctionSwitches))
return false;

if (type == SOURCE_TYPE_CUSTOM_SWITCH && abs(index) > CPN_MAX_LOGICAL_SWITCHES)
return false;

if (type == SOURCE_TYPE_FUNCTIONSWITCH_GROUP && model->getFuncGroupSwitchCount(abs(index), CPN_MAX_SWITCHES_FUNCTION) == 0)
return false;

if (type == SOURCE_TYPE_LUA_OUTPUT && div(abs(index - 1), 16).quot >= CPN_MAX_SCRIPTS)
return false;
Expand Down
1 change: 1 addition & 0 deletions companion/src/firmwares/rawsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ enum RawSourceType {
SOURCE_TYPE_TELEMETRY,
SOURCE_TYPE_SPACEMOUSE,
SOURCE_TYPE_TIMER,
SOURCE_TYPE_FUNCTIONSWITCH_GROUP,
MAX_SOURCE_TYPE
};

Expand Down
9 changes: 5 additions & 4 deletions companion/src/modeledit/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ bool FilteredGroupSwitchesModel::filterAcceptsRow(int sourceRow, const QModelInd
return false;
return true;
}
if (m_model->getFuncSwitchGroup(sourceRow - 1) == m_group)
if ((int)m_model->getFuncSwitchGroup(sourceRow - 1) == m_group)
return true;
return false;
}
Expand Down Expand Up @@ -1503,6 +1503,7 @@ void FunctionSwitchesPanel::on_groupChanged(int value)
model->setFuncGroupSwitchStart(oldGrp, model->getFuncGroupSwitchStart(oldGrp, switchcnt), switchcnt);
update();
emit modified();
emit updateDataModels();
}
lock = false;
}
Expand All @@ -1524,12 +1525,12 @@ void FunctionSwitchesPanel::on_alwaysOnGroupChanged(int value)
model->setFuncSwitchAlwaysOnGroup(grp, (unsigned int)value);
if (value) {
for (int i = 0; i < switchcnt; i += 1) {
if ((model->getFuncSwitchGroup(i) == grp) && (model->getFuncSwitchConfig(i) == ModelData::FUNC_SWITCH_CONFIG_TOGGLE))
if (((int)model->getFuncSwitchGroup(i) == grp) && (model->getFuncSwitchConfig(i) == ModelData::FUNC_SWITCH_CONFIG_TOGGLE))
model->setFuncSwitchConfig(i, ModelData::FUNC_SWITCH_CONFIG_2POS);
}
if (model->getFuncGroupSwitchStart(grp, switchcnt) == switchcnt + 1) {
if ((int)model->getFuncGroupSwitchStart(grp, switchcnt) == switchcnt + 1) {
for (int i = 0; i < switchcnt; i += 1) {
if (model->getFuncSwitchGroup(i) == grp)
if ((int)model->getFuncSwitchGroup(i) == grp)
model->setFuncSwitchStart(i, ModelData::FUNC_SWITCH_START_PREVIOUS);
}
}
Expand Down
4 changes: 4 additions & 0 deletions radio/src/dataconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ enum MixSources {
MIXSRC_FIRST_SWITCH SKIP,
MIXSRC_LAST_SWITCH SKIP = MIXSRC_FIRST_SWITCH + MAX_SWITCHES - 1,

#if defined(FUNCTION_SWITCHES)
MIXSRC_FIRST_CUSTOMSWITCH_GROUP SKIP,
MIXSRC_LAST_CUSTOMSWITCH_GROUP SKIP = MIXSRC_FIRST_CUSTOMSWITCH_GROUP + NUM_FUNCTIONS_GROUPS - 1,
#endif
MIXSRC_FIRST_LOGICAL_SWITCH SKIP,
MIXSRC_LAST_LOGICAL_SWITCH SKIP = MIXSRC_FIRST_LOGICAL_SWITCH + MAX_LOGICAL_SWITCHES - 1,

Expand Down
10 changes: 10 additions & 0 deletions radio/src/gui/gui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ bool isSourceAvailable(int source)
if (source >= MIXSRC_FIRST_TRAINER && source <= MIXSRC_LAST_TRAINER)
return g_model.trainerData.mode > 0;

#if defined(FUNCTION_SWITCHES)
if (source >= MIXSRC_FIRST_CUSTOMSWITCH_GROUP && source <= MIXSRC_LAST_CUSTOMSWITCH_GROUP)
return getSwitchCountInFSGroup(source - MIXSRC_FIRST_CUSTOMSWITCH_GROUP + 1) > 0;
#endif

if (source >= MIXSRC_FIRST_CH && source <= MIXSRC_LAST_CH) {
return isChannelUsed(source - MIXSRC_FIRST_CH);
}
Expand Down Expand Up @@ -323,6 +328,11 @@ bool isSourceAvailableInInputs(int source)
if (source >= MIXSRC_FIRST_SWITCH && source <= MIXSRC_LAST_SWITCH)
return SWITCH_EXISTS(source - MIXSRC_FIRST_SWITCH);

#if defined(FUNCTION_SWITCHES)
if (source >= MIXSRC_FIRST_CUSTOMSWITCH_GROUP && source <= MIXSRC_LAST_CUSTOMSWITCH_GROUP)
return getSwitchCountInFSGroup(source - MIXSRC_FIRST_CUSTOMSWITCH_GROUP + 1) > 0;
#endif

if (source >= MIXSRC_FIRST_CH && source <= MIXSRC_LAST_CH)
return true;

Expand Down
24 changes: 24 additions & 0 deletions radio/src/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,30 @@ getvalue_t _getValue(mixsrc_t i, bool* valid)
return _switch_3pos_lookup[switchGetPosition(sw_idx)];
}
}
#if defined(FUNCTION_SWITCHES)
else if (i <= MIXSRC_LAST_CUSTOMSWITCH_GROUP) {
uint8_t group_idx = (uint8_t)(i - MIXSRC_FIRST_CUSTOMSWITCH_GROUP + 1);
uint8_t stepcount = getSwitchCountInFSGroup(group_idx);
if (stepcount == 0)
return 0;

if (IS_FSWITCH_GROUP_ON(group_idx))
stepcount--;

int stepsize = (2 * RESX) / stepcount;
int value = -RESX;

for (uint8_t i = 0; i < switchGetMaxFctSwitches(); i++) {
if(FSWITCH_GROUP(i) == group_idx) {
if (getFSLogicalState(i) == 1)
return value + (IS_FSWITCH_GROUP_ON(group_idx) ? 0 : stepsize);
else
value += stepsize;
}
}
return -RESX;
}
#endif

else if (i <= MIXSRC_LAST_LOGICAL_SWITCH) {
return getSwitch(SWSRC_FIRST_LOGICAL_SWITCH + i - MIXSRC_FIRST_LOGICAL_SWITCH) ? 1024 : -1024;
Expand Down
16 changes: 15 additions & 1 deletion radio/src/storage/yaml/yaml_datastructs_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,15 @@ static uint32_t r_mixSrcRaw(const YamlNode* node, const char* val, uint8_t val_l
val += 3; val_len -= 3;
// parse int and ignore closing ')'
return yaml_str2uint(val, val_len) + MIXSRC_FIRST_GVAR;

#if defined(FUNCTION_SWITCHES)
} else if (val_len > 2 &&
val[0] == 'G' &&
val[1] == 'R' &&
val[2] >= '1' &&
val[2] <= '3') {

return MIXSRC_FIRST_CUSTOMSWITCH_GROUP + (val[2] - '1');
#endif
} else if (val_len > 5 &&
val[0] == 't' &&
val[1] == 'e' &&
Expand Down Expand Up @@ -353,6 +361,12 @@ static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf,
&& val <= MIXSRC_LAST_SWITCH) {
str = switchGetCanonicalName(val - MIXSRC_FIRST_SWITCH);
}
#if defined(FUNCTION_SWITCHES)
else if (val >= MIXSRC_FIRST_CUSTOMSWITCH_GROUP
&& val <= MIXSRC_LAST_CUSTOMSWITCH_GROUP) {
str = fsSwitchGroupGetCanonicalName(val - MIXSRC_FIRST_CUSTOMSWITCH_GROUP);
}
#endif
else if (val >= MIXSRC_FIRST_LOGICAL_SWITCH
&& val <= MIXSRC_LAST_LOGICAL_SWITCH) {

Expand Down
13 changes: 13 additions & 0 deletions radio/src/strhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ char *getFlightModeString(char *dest, int8_t idx)
return dest;
}

char *getCustomSwitchesGroupName(char *dest, uint8_t idx)
{
dest = strAppendStringWithIndex(dest, "GR", idx + 1);

return dest;
}

char *getSwitchName(char *dest, uint8_t idx)
{
if (switchHasCustomName(idx)) {
Expand Down Expand Up @@ -738,6 +745,12 @@ char *getSourceString(char (&destRef)[L], mixsrc_t idx)
idx -= MIXSRC_FIRST_SWITCH;
char *pos = strAppend(dest, STR_CHAR_SWITCH, sizeof(STR_CHAR_SWITCH) - 1);
getSwitchName(pos, idx);
#if defined(FUNCTION_SWITCHES)
} else if (idx <= MIXSRC_LAST_CUSTOMSWITCH_GROUP) {
idx -= MIXSRC_FIRST_CUSTOMSWITCH_GROUP;
char *pos = strAppend(dest, STR_CHAR_SWITCH, sizeof(STR_CHAR_SWITCH) - 1);
getCustomSwitchesGroupName(pos, idx);
#endif
} else if (idx <= MIXSRC_LAST_LOGICAL_SWITCH) {
// TODO: unnecessary, use the direct way instead
idx -= MIXSRC_FIRST_LOGICAL_SWITCH;
Expand Down
1 change: 1 addition & 0 deletions radio/src/strhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const char *getMainControlLabel(uint8_t idx);
const char *getTrimLabel(uint8_t idx);
const char *getTrimSourceLabel(uint16_t src_raw, int8_t trim_src);
const char *getPotLabel(uint8_t idx);
char *getCustomSwitchesGroupName(char *dest, uint8_t idx);

template <size_t L>
char *getSourceString(char (&dest)[L], mixsrc_t idx);
Expand Down
24 changes: 24 additions & 0 deletions radio/src/switches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ uint8_t getFSPreviousPhysicalState(uint8_t index)
return (uint8_t )(bfSingleBitGet(fsPreviousState, index) >> (index));
}

uint8_t getSwitchCountInFSGroup(uint8_t index)
{
uint8_t count = 0;

for (uint8_t i = 0; i < switchGetMaxFctSwitches(); i++) {
if (FSWITCH_GROUP(i) == index)
count++;
}

return count;
}

bool isFSGroupUsed(uint8_t index)
{
return getSwitchCountInFSGroup(index) != 0;
}

void evalFunctionSwitches()
{
uint8_t fct_switches = switchGetMaxFctSwitches();
Expand Down Expand Up @@ -312,6 +329,13 @@ const char* switchGetCanonicalName(uint8_t idx)
return switchGetName(idx);
}

const char* fsSwitchGroupGetCanonicalName(uint8_t idx)
{
static const char fsgroupname[3][4] = {"GR1", "GR2", "GR3"};

return &fsgroupname[idx][0];
}

SwitchConfig switchGetMaxType(uint8_t idx)
{
auto hw_type = switchGetHwType(idx);
Expand Down
2 changes: 2 additions & 0 deletions radio/src/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ char switchGetLetter(uint8_t idx);
const char* switchGetCanonicalName(uint8_t idx);

// customizable switches supported
const char* fsSwitchGroupGetCanonicalName(uint8_t idx);
void switchSetCustomName(uint8_t idx, const char* str, size_t len);
const char* switchGetCustomName(uint8_t idx);
bool switchHasCustomName(uint8_t idx);
uint8_t getSwitchCountInFSGroup(uint8_t index);

SwitchConfig switchGetMaxType(uint8_t idx);

Expand Down
1 change: 1 addition & 0 deletions radio/src/targets/horus/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ void bluetoothDisable();

#if defined(FUNCTION_SWITCHES)
#define NUM_FUNCTIONS_SWITCHES 6
#define NUM_FUNCTIONS_GROUPS 3
#define DEFAULT_FS_CONFIG \
(SWITCH_2POS << 10) + (SWITCH_2POS << 8) + (SWITCH_2POS << 6) + \
(SWITCH_2POS << 4) + (SWITCH_2POS << 2) + (SWITCH_2POS << 0)
Expand Down
1 change: 1 addition & 0 deletions radio/src/targets/taranis/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ uint32_t isBootloaderStart(const uint8_t * buffer);
#if defined(FUNCTION_SWITCHES)

#define NUM_FUNCTIONS_SWITCHES 6
#define NUM_FUNCTIONS_GROUPS 3

#define DEFAULT_FS_CONFIG \
(SWITCH_2POS << 10) + (SWITCH_2POS << 8) + (SWITCH_2POS << 6) + \
Expand Down

0 comments on commit 280d3af

Please sign in to comment.