Skip to content

Commit

Permalink
fix: enforce customisable switch rules when configuring (#4994)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz committed Jun 1, 2024
1 parent 6579905 commit 9a7e1db
Show file tree
Hide file tree
Showing 32 changed files with 772 additions and 207 deletions.
255 changes: 172 additions & 83 deletions companion/src/firmwares/modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,106 +1697,195 @@ AbstractStaticItemModel * ModelData::trainerModeItemModel(const GeneralSettings
return mdl;
}

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

unsigned int ModelData::getFuncSwitchConfig(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
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_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchConfig, value, index, 2);
}

void ModelData::setFuncSwitchConfig(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
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
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;
}

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

for (unsigned int i = FUNC_SWITCH_CONFIG_FIRST; i <= FUNC_SWITCH_CONFIG_LAST; i++) {
mdl->appendToItemList(funcSwitchConfigToString(i), i);
}
mdl->appendToItemList(tr("Restore"), 0);
for (unsigned int i = 1; i <= switchcnt; i += 1) {
mdl->appendToItemList(tr("SW") + QString::number(i), i);
}
mdl->appendToItemList(tr("Off"), switchcnt + 1);

mdl->loadItemList();
return mdl;
}
mdl->loadItemList();
return mdl;
}

unsigned int ModelData::getFuncSwitchGroup(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchGroup, index, 2);
else
return 0;
}
AbstractStaticItemModel * ModelData::funcSwitchGroupsModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_MODELDATA_FUNCSWITCHGROUPS);

void ModelData::setFuncSwitchGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchGroup, value, index, 2);
}
mdl->appendToItemList(tr("---"), 0);
for (unsigned int i = 1; i <= 3; i += 1) {
mdl->appendToItemList(tr("Group ") + QString::number(i), i);
}

unsigned int ModelData::getFuncSwitchAlwaysOnGroup(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION) {
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;
}
mdl->loadItemList();
return mdl;
}

void ModelData::setFuncSwitchAlwaysOnGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION) {
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::getFuncSwitchGroup(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchGroup, index, 2);
else
return 0;
}

unsigned int ModelData::getFuncSwitchStart(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchStartConfig, index, 2);
else
return FUNC_SWITCH_START_INACTIVE;
}
void ModelData::setFuncSwitchGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchGroup, value, index, 2);
}

void ModelData::setFuncSwitchStart(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchStartConfig, value, index, 2);
}
unsigned int ModelData::getFuncSwitchAlwaysOnGroup(unsigned int group) const
{
if (group > 0 && group < 4) {
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
return Helpers::getBitmappedValue(functionSwitchGroup, group, 1, 2 * switchcnt);
}
else
return 0;
}

unsigned int ModelData::getFuncSwitchAlwaysOnGroupForSwitch(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return getFuncSwitchAlwaysOnGroup(getFuncSwitchGroup(index));
else
return 0;
}

void ModelData::setFuncSwitchAlwaysOnGroup(unsigned int group, unsigned int value)
{
if (group > 0 && group < 4) {
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
Helpers::setBitmappedValue(functionSwitchGroup, value, group, 1, 2 * switchcnt);
}
}

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

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

int ModelData::getFuncGroupSwitchCount(unsigned int group, int switchcnt) const
{
int grpSwitchCount = 0;
for (int i = 0; i < switchcnt; i += 1) {
if (getFuncSwitchGroup(i) == group) {
grpSwitchCount += 1;
}
}
return grpSwitchCount;
}

unsigned int ModelData::getFuncGroupSwitchStart(unsigned int group, int switchcnt) const
{
bool allDown = true;
for (int i = 0; i < switchcnt; i += 1) {
if (getFuncSwitchGroup(i) == group) {
if (getFuncSwitchStart(i) == FUNC_SWITCH_START_ON)
return i + 1;
if (getFuncSwitchStart(i) != FUNC_SWITCH_START_OFF)
allDown = false;
}
}
if (allDown && getFuncGroupSwitchCount(group, switchcnt) > 0) return switchcnt + 1;
return 0;
}

void ModelData::setFuncGroupSwitchStart(unsigned int group, unsigned int value, int switchcnt)
{
for (int i = 0; i < switchcnt; i += 1) {
if (getFuncSwitchGroup(i) == group)
setFuncSwitchStart(i, value ? ModelData::FUNC_SWITCH_START_OFF : ModelData::FUNC_SWITCH_START_PREVIOUS);
}
if (value > 0 && value <= switchcnt) {
setFuncSwitchStart(value - 1, ModelData::FUNC_SWITCH_START_ON);
}
}

void ModelData::setGroupSwitchState(uint8_t group, int switchcnt)
{
// Check rules for always on group
// - Toggle switch type not valid, change all switches to 2POS
// - One switch must be turned on, turn on first switch if needed
if (getFuncSwitchAlwaysOnGroup(group)) {
for (int j = 0; j < switchcnt; j += 1) {
if (getFuncSwitchGroup(j) == group) {
setFuncSwitchConfig(j, FUNC_SWITCH_CONFIG_2POS); // Toggle not valid
}
}
if (getFuncGroupSwitchStart(group, switchcnt) == switchcnt + 1) {
// Start state for all switches is off - set all to 'last'
for (int j = 0; j < switchcnt; j += 1)
if (getFuncSwitchGroup(j) == group)
setFuncSwitchStart(j, FUNC_SWITCH_START_PREVIOUS);
}
}
}

// 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_OFF:
return tr("Off");
case FUNC_SWITCH_START_ON:
return tr("On");
case FUNC_SWITCH_START_PREVIOUS:
return tr("Restore");
default:
Expand Down
22 changes: 15 additions & 7 deletions companion/src/firmwares/modeldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class AbstractStaticItemModel;
constexpr char AIM_MODELDATA_TRAINERMODE[] {"modeldata.trainermode"};
constexpr char AIM_MODELDATA_FUNCSWITCHCONFIG[] {"modeldata.funcswitchconfig"};
constexpr char AIM_MODELDATA_FUNCSWITCHSTART[] {"modeldata.funcswitchstart"};
constexpr char AIM_MODELDATA_FUNCSWITCHGROUPSTARTSWITCH[] = {"modeldata.funcswitchgroupstartswitch"};
constexpr char AIM_MODELDATA_FUNCSWITCHGROUPS[] = {"modeldata.funcswitchgroups"};

class RSSIAlarmData {
public:
Expand Down Expand Up @@ -214,10 +216,9 @@ class ModelData {
};

enum FunctionSwitchStart {
FUNC_SWITCH_START_ACTIVE,
FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_ACTIVE,
FUNC_SWITCH_START_INACTIVE,
//FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_INACTIVE,
FUNC_SWITCH_START_ON,
FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_ON,
FUNC_SWITCH_START_OFF,
FUNC_SWITCH_START_PREVIOUS,
FUNC_SWITCH_START_LAST = FUNC_SWITCH_START_PREVIOUS
};
Expand Down Expand Up @@ -331,15 +332,22 @@ class ModelData {
void setFuncSwitchConfig(unsigned int index, unsigned int value);
static QString funcSwitchConfigToString(unsigned int value);
static AbstractStaticItemModel * funcSwitchConfigItemModel();

static AbstractStaticItemModel * funcSwitchGroupStartSwitchModel(int switchcnt);
static AbstractStaticItemModel * funcSwitchGroupsModel();

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 getFuncSwitchAlwaysOnGroup(unsigned int group) const;
unsigned int getFuncSwitchAlwaysOnGroupForSwitch(unsigned int index) const;
void setFuncSwitchAlwaysOnGroup(unsigned int group, unsigned int value);
void setGroupSwitchState(uint8_t group, int switchcnt);

unsigned int getFuncSwitchStart(unsigned int index) const;
void setFuncSwitchStart(unsigned int index, unsigned int value);
int getFuncGroupSwitchCount(unsigned int group, int switchcnt) const;
unsigned int getFuncGroupSwitchStart(unsigned int group, int switchcnt) const;
void setFuncGroupSwitchStart(unsigned int group, unsigned int value, int switchcnt);
static QString funcSwitchStartToString(unsigned int value);
static AbstractStaticItemModel * funcSwitchStartItemModel();

Expand Down
Loading

0 comments on commit 9a7e1db

Please sign in to comment.