Skip to content

Commit

Permalink
More work on function switches support in Companion (opentx#8847)
Browse files Browse the repository at this point in the history
  • Loading branch information
3djc authored Jan 29, 2022
1 parent 2cb66f1 commit 744c4fe
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 71 deletions.
7 changes: 5 additions & 2 deletions companion/src/datamodels/compounditemmodels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ RawSourceItemModel::RawSourceItemModel(const GeneralSettings * const generalSett
addItems(SOURCE_TYPE_TRIM, RawSource::TrimsGroup, board->getCapability(Board::NumTrims));
addItems(SOURCE_TYPE_MAX, RawSource::SourcesGroup, 1);
addItems(SOURCE_TYPE_SWITCH, RawSource::SwitchesGroup, board->getCapability(Board::Switches));
addItems(SOURCE_TYPE_FUNCTIONSWITCH, RawSource::SwitchesGroup, board->getCapability(Board::FunctionSwitches));
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 Expand Up @@ -163,7 +164,7 @@ RawSwitchItemModel::RawSwitchItemModel(const GeneralSettings * const generalSett
AbstractDynamicItemModel(generalSettings, modelData, firmware, board, boardType)
{
setId(IMID_RawSwitch);
setUpdateMask(IMUE_FlightModes | IMUE_LogicalSwitches | IMUE_TeleSensors);
setUpdateMask(IMUE_FlightModes | IMUE_LogicalSwitches | IMUE_TeleSensors | IMUE_FunctionSwitches);

// Descending switch direction: NOT (!) switches
addItems(SWITCH_TYPE_ACT, -1);
Expand All @@ -173,12 +174,14 @@ RawSwitchItemModel::RawSwitchItemModel(const GeneralSettings * const generalSett
addItems(SWITCH_TYPE_VIRTUAL, -firmware->getCapability(LogicalSwitches));
addItems(SWITCH_TYPE_TRIM, -board->getCapability(Board::NumTrimSwitches));
addItems(SWITCH_TYPE_MULTIPOS_POT, -(board->getCapability(Board::MultiposPots) * board->getCapability(Board::MultiposPotsPositions)));
addItems(SWITCH_TYPE_FUNCTIONSWITCH, -board->getCapability(Board::NumFunctionSwitchesPositions));
addItems(SWITCH_TYPE_SWITCH, -board->getCapability(Board::SwitchPositions));

// Ascending switch direction (including zero)
addItems(SWITCH_TYPE_TIMER_MODE, 5);
addItems(SWITCH_TYPE_NONE, 1);
addItems(SWITCH_TYPE_SWITCH, board->getCapability(Board::SwitchPositions));
addItems(SWITCH_TYPE_FUNCTIONSWITCH, board->getCapability(Board::NumFunctionSwitchesPositions));
addItems(SWITCH_TYPE_MULTIPOS_POT, board->getCapability(Board::MultiposPots) * board->getCapability(Board::MultiposPotsPositions));
addItems(SWITCH_TYPE_TRIM, board->getCapability(Board::NumTrimSwitches));
addItems(SWITCH_TYPE_VIRTUAL, firmware->getCapability(LogicalSwitches));
Expand All @@ -203,7 +206,7 @@ void RawSwitchItemModel::addItems(const RawSwitchType & type, int count)
// handle exceptions in RawSwitch() index values
const short rawIdxAdj = rawSwitchIndexBaseZeroTypes.contains(type) ? -1 : 0;

// determine cotext flags
// determine context flags
int context = RawSwitch::AllSwitchContexts;
switch (type) {
case SWITCH_TYPE_FLIGHT_MODE:
Expand Down
29 changes: 15 additions & 14 deletions companion/src/datamodels/compounditemmodels.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ class AbstractItemModel: public QStandardItemModel
Q_ENUM(ItemModelDataGroups)

enum ItemModelUpdateEvent {
IMUE_None = 0,
IMUE_SystemRefresh = 1 << 0,
IMUE_Channels = 1 << 1,
IMUE_Curves = 1 << 2,
IMUE_FlightModes = 1 << 3,
IMUE_GVars = 1 << 4,
IMUE_Inputs = 1 << 5,
IMUE_LogicalSwitches = 1 << 6,
IMUE_Scripts = 1 << 7,
IMUE_TeleSensors = 1 << 8,
IMUE_Timers = 1 << 9,
IMUE_Modules = 1 << 10,
IMUE_All = IMUE_SystemRefresh | IMUE_Channels | IMUE_Curves | IMUE_FlightModes | IMUE_GVars | IMUE_Inputs |
IMUE_LogicalSwitches | IMUE_Scripts | IMUE_TeleSensors | IMUE_Timers | IMUE_Modules
IMUE_None = 0,
IMUE_SystemRefresh = 1 << 0,
IMUE_Channels = 1 << 1,
IMUE_Curves = 1 << 2,
IMUE_FlightModes = 1 << 3,
IMUE_GVars = 1 << 4,
IMUE_Inputs = 1 << 5,
IMUE_LogicalSwitches = 1 << 6,
IMUE_Scripts = 1 << 7,
IMUE_TeleSensors = 1 << 8,
IMUE_Timers = 1 << 9,
IMUE_Modules = 1 << 10,
IMUE_FunctionSwitches = 1 << 11,
IMUE_All = IMUE_SystemRefresh | IMUE_Channels | IMUE_Curves | IMUE_FlightModes | IMUE_GVars | IMUE_Inputs |
IMUE_LogicalSwitches | IMUE_Scripts | IMUE_TeleSensors | IMUE_Timers | IMUE_Modules | IMUE_FunctionSwitches
};
Q_ENUM(ItemModelUpdateEvent)

Expand Down
9 changes: 6 additions & 3 deletions companion/src/firmwares/boards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ int Boards::getCapability(Board::Type board, Board::Capability capability)
else
return 7;

case FunctionSwitches:
return (IS_JUMPER_TPRO(board) ? 6 : 0);

case FactoryInstalledSwitches:
if (IS_TARANIS_X9E(board))
return 8;
Expand All @@ -443,6 +446,9 @@ int Boards::getCapability(Board::Type board, Board::Capability capability)
else
return 9;

case NumFunctionSwitchesPositions:
return getCapability(board, Board::FunctionSwitches) * 3;

case NumTrims:
if (IS_FAMILY_HORUS_OR_T16(board))
return 6;
Expand All @@ -458,9 +464,6 @@ int Boards::getCapability(Board::Type board, Board::Capability capability)
case HasColorLcd:
return IS_FAMILY_HORUS_OR_T16(board);

case NumFunctionSwitches:
return IS_JUMPER_TPRO(board) ? 6 : 0;

default:
return 0;
}
Expand Down
5 changes: 3 additions & 2 deletions companion/src/firmwares/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ namespace Board {
MultiposPots,
MultiposPotsPositions,
Switches,
FunctionSwitches,
SwitchPositions,
NumFunctionSwitchesPositions,
FactoryInstalledSwitches,
NumTrims,
NumTrimSwitches,
HasRTC,
HasColorLcd,
NumFunctionSwitches
HasColorLcd
};

struct SwitchInfo
Expand Down
3 changes: 1 addition & 2 deletions companion/src/firmwares/eeprominterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ enum Capability {
HasBluetooth,
HasAntennaChoice,
HasADCJitterFilter,
HasTelemetryBaudrate,
FunctionSwitches
HasTelemetryBaudrate
};

class EEPROMInterface
Expand Down
150 changes: 147 additions & 3 deletions companion/src/firmwares/modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ void ModelData::setDefaultFunctionSwitches(int functionSwitchCount)
return;

for (int i = 0; i < functionSwitchCount; i++) {
functionSwitchConfig |= (Board::SWITCH_2POS << 2 * i);
functionSwitchGroup |= (1 << 2 * i);
setFuncSwitchConfig(i, Board::SWITCH_2POS);
setFuncSwitchGroup(i, 1);
}
}

Expand All @@ -230,7 +230,7 @@ void ModelData::setDefaultValues(unsigned int id, const GeneralSettings & settin
moduleData[i].modelId = id + 1;
}
setDefaultMixes(settings);
setDefaultFunctionSwitches(getCurrentFirmware()->getCapability(FunctionSwitches));
setDefaultFunctionSwitches(Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches));
}

int ModelData::getTrimValue(int phaseIdx, int trimIdx)
Expand Down Expand Up @@ -382,6 +382,27 @@ int ModelData::getChannelsMax(bool forceExtendedLimits) const
return 100;
}

bool ModelData::isFunctionSwitchPositionAvailable(int index) const
{
if (index == 0)
return true;

div_t qr = div(abs(index) - 1, 3);
int fs = getFuncSwitchConfig(qr.quot);

if (qr.rem == 1) {
return false;
}
else {
return fs != Board::SWITCH_NOT_AVAILABLE;
}
}

bool ModelData::isFunctionSwitchSourceAllowed(int index) const
{
return (int)getFuncSwitchConfig(index) != Board::SWITCH_NOT_AVAILABLE;
}

bool ModelData::isAvailable(const RawSwitch & swtch) const
{
unsigned index = abs(swtch.index) - 1;
Expand All @@ -395,6 +416,9 @@ bool ModelData::isAvailable(const RawSwitch & swtch) const
else if (swtch.type == SWITCH_TYPE_SENSOR) {
return strlen(sensorData[index].label) > 0;
}
else if (swtch.type == SWITCH_TYPE_FUNCTIONSWITCH) {
return isFunctionSwitchPositionAvailable(index + 1);
}
else {
return true;
}
Expand Down Expand Up @@ -1647,3 +1671,123 @@ AbstractStaticItemModel * ModelData::trainerModeItemModel(const GeneralSettings
mdl->loadItemList();
return mdl;
}

unsigned int ModelData::getFuncSwitchConfig(unsigned int index) const
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
return Helpers::getBitmappedValue(functionSwitchConfig, index, 2);
else
return FUNC_SWITCH_CONFIG_NONE;
}

void ModelData::setFuncSwitchConfig(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
Helpers::setBitmappedValue(functionSwitchConfig, value, index, 2);
}

// static
QString ModelData::funcSwitchConfigToString(unsigned int value)
{
switch (value) {
case FUNC_SWITCH_CONFIG_NONE:
return tr("NONE");
case FUNC_SWITCH_CONFIG_TOGGLE:
return tr("TOGGLE");
case FUNC_SWITCH_CONFIG_2POS:
return tr("2POS");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}

// static
AbstractStaticItemModel * ModelData::funcSwitchConfigItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_MODELDATA_FUNCSWITCHCONFIG);

for (unsigned int i = FUNC_SWITCH_CONFIG_FIRST; i <= FUNC_SWITCH_CONFIG_LAST; i++) {
mdl->appendToItemList(funcSwitchConfigToString(i), i);
}

mdl->loadItemList();
return mdl;
}

unsigned int ModelData::getFuncSwitchGroup(unsigned int index) const
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
return Helpers::getBitmappedValue(functionSwitchGroup, index, 2);
else
return 0;
}

void ModelData::setFuncSwitchGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
Helpers::setBitmappedValue(functionSwitchGroup, value, index, 2);
}

unsigned int ModelData::getFuncSwitchAlwaysOnGroup(unsigned int index) const
{
if (index < CPN_MAX_FUNCTION_SWITCHES) {
unsigned int grp = getFuncSwitchGroup(index);
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
return Helpers::getBitmappedValue(functionSwitchGroup, grp, 1, 2 * switchcnt);
}
else
return 0;
}

void ModelData::setFuncSwitchAlwaysOnGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_FUNCTION_SWITCHES) {
unsigned int grp = getFuncSwitchGroup(index);
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
Helpers::setBitmappedValue(functionSwitchGroup, value, grp, 1, 2 * switchcnt);
}
}

unsigned int ModelData::getFuncSwitchStart(unsigned int index) const
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
return Helpers::getBitmappedValue(functionSwitchStartConfig, index, 2);
else
return FUNC_SWITCH_START_INACTIVE;
}

void ModelData::setFuncSwitchStart(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_FUNCTION_SWITCHES)
Helpers::setBitmappedValue(functionSwitchStartConfig, value, index, 2);
}

// static
QString ModelData::funcSwitchStartToString(unsigned int value)
{
switch (value) {
case FUNC_SWITCH_START_INACTIVE:
return tr("Inactive");
case FUNC_SWITCH_START_ACTIVE:
return tr("Active");
case FUNC_SWITCH_START_PREVIOUS:
return tr("Restore");
default:
return CPN_STR_UNKNOWN_ITEM;
}
}

// static
AbstractStaticItemModel * ModelData::funcSwitchStartItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_MODELDATA_FUNCSWITCHSTART);

for (unsigned int i = FUNC_SWITCH_START_FIRST; i <= FUNC_SWITCH_START_LAST; i++) {
mdl->appendToItemList(funcSwitchStartToString(i), i);
}

mdl->loadItemList();
return mdl;
}
36 changes: 36 additions & 0 deletions companion/src/firmwares/modeldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class RadioDataConversionState;
class AbstractStaticItemModel;

constexpr char AIM_MODELDATA_TRAINERMODE[] {"modeldata.trainermode"};
constexpr char AIM_MODELDATA_FUNCSWITCHCONFIG[] {"modeldata.funcswitchconfig"};
constexpr char AIM_MODELDATA_FUNCSWITCHSTART[] {"modeldata.funcswitchstart"};

#define CHAR_FOR_NAMES " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-."
#define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,]*"
Expand Down Expand Up @@ -197,6 +199,22 @@ class ModelData {

char registrationId[8+1];

enum FunctionSwitchConfig {
FUNC_SWITCH_CONFIG_NONE,
FUNC_SWITCH_CONFIG_FIRST = FUNC_SWITCH_CONFIG_NONE,
FUNC_SWITCH_CONFIG_TOGGLE,
FUNC_SWITCH_CONFIG_2POS,
FUNC_SWITCH_CONFIG_LAST = FUNC_SWITCH_CONFIG_2POS
};

enum FunctionSwitchStart {
FUNC_SWITCH_START_INACTIVE,
FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_INACTIVE,
FUNC_SWITCH_START_ACTIVE,
FUNC_SWITCH_START_PREVIOUS,
FUNC_SWITCH_START_LAST = FUNC_SWITCH_START_PREVIOUS
};

// Function switches
unsigned int functionSwitchConfig;
unsigned int functionSwitchGroup;
Expand Down Expand Up @@ -238,6 +256,8 @@ class ModelData {
int getChannelsMax(bool forceExtendedLimits=false) const;

bool isAvailable(const RawSwitch & swtch) const;
bool isFunctionSwitchPositionAvailable(int index) const;
bool isFunctionSwitchSourceAllowed(int index) const;

enum ReferenceUpdateAction {
REF_UPD_ACT_CLEAR,
Expand Down Expand Up @@ -294,6 +314,22 @@ class ModelData {
static bool isTrainerModeAvailable(const GeneralSettings & generalSettings, const Firmware * firmware, const int value);
static AbstractStaticItemModel * trainerModeItemModel(const GeneralSettings & generalSettings, const Firmware * firmware);

unsigned int getFuncSwitchConfig(unsigned int index) const;
void setFuncSwitchConfig(unsigned int index, unsigned int value);
static QString funcSwitchConfigToString(unsigned int value);
static AbstractStaticItemModel * funcSwitchConfigItemModel();

unsigned int getFuncSwitchGroup(unsigned int index) const;
void setFuncSwitchGroup(unsigned int index, unsigned int value);

unsigned int getFuncSwitchAlwaysOnGroup(unsigned int index) const;
void setFuncSwitchAlwaysOnGroup(unsigned int index, unsigned int value);

unsigned int getFuncSwitchStart(unsigned int index) const;
void setFuncSwitchStart(unsigned int index, unsigned int value);
static QString funcSwitchStartToString(unsigned int value);
static AbstractStaticItemModel * funcSwitchStartItemModel();

protected:
void removeGlobalVar(int & var);

Expand Down
Loading

0 comments on commit 744c4fe

Please sign in to comment.