Skip to content

Commit

Permalink
fix(yaml): flex switch warnings (#4949)
Browse files Browse the repository at this point in the history
Co-authored-by: raphaelcoeffic <[email protected]>
Co-authored-by: 3djc <[email protected]>
Co-authored-by: elecpower <[email protected]>
  • Loading branch information
4 people authored and pfeerick committed May 3, 2024
1 parent 5d437fc commit 0db8a92
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 101 deletions.
104 changes: 98 additions & 6 deletions companion/src/firmwares/edgetx/yaml_modeldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,88 @@ struct YamlBeepANACenter {
}
};

// modeldata: uint64_t switchWarningStates
// Yaml switchWarning:
// SA:
// pos: mid
// SB:
// pos: up
// FL1:
// pos: down
struct YamlSwitchWarning {

static constexpr size_t MASK_LEN = 2;
static constexpr size_t MASK = (1 << MASK_LEN) - 1;

unsigned int enabled;

YamlSwitchWarning() = default;

YamlSwitchWarning(YAML::Node& node, uint64_t cpn_value, unsigned int switchWarningEnable)
: enabled(~switchWarningEnable)
{
uint64_t states = cpn_value;

for (int i = 0; i < Boards::getCapability(getCurrentBoard(), Board::Switches); i++) {
if (!Boards::isSwitchFunc(i) && (enabled & (1 << i))) {
std::string posn;

switch(states & MASK) {
case 0:
posn = "up";
break;
case 1:
posn = "mid";
break;
case 2:
posn = "down";
break;
}

node[Boards::getSwitchTag(i).toStdString()]["pos"] = posn;
}

states >>= MASK_LEN;
}
}

uint64_t toCpn(const YAML::Node &warn)
{
uint64_t states = 0;
enabled = 0;

if (warn.IsMap()) {
for (const auto& sw : warn) {
std::string tag;
sw.first >> tag;
int index = Boards::getSwitchIndex(tag.c_str(), Board::LVT_NAME);

if (index < 0)
continue;

std::string posn;
if (warn[tag]["pos"])
warn[tag]["pos"] >> posn;

int value = 0;

if (posn == "up")
value = 0;
else if (posn == "mid")
value = 1;
else if (posn == "down")
value = 2;

states |= ((uint64_t)value << (index * MASK_LEN));
enabled |= (1 << index);
}
}

return states;
}
};

// Depreciated - only used for decoding refer YamlSwitchWarning for replacement
// modeldata: uint64_t switchWarningStates
// Yaml switchWarningState: AuBuEuFuG-IuJu
struct YamlSwitchWarningState {
Expand Down Expand Up @@ -985,8 +1067,11 @@ Node convert<ModelData>::encode(const ModelData& rhs)
YamlThrTrace thrTrace(rhs.thrTraceSrc);
node["thrTraceSrc"] = thrTrace.src;

YamlSwitchWarningState switchWarningState(rhs.switchWarningStates, rhs.switchWarningEnable);
node["switchWarningState"] = switchWarningState.src_str;
Node sw_warn;
YamlSwitchWarning switchWarning(sw_warn, rhs.switchWarningStates, rhs.switchWarningEnable);
if (sw_warn && sw_warn.IsMap()) {
node["switchWarning"] = sw_warn;
}

node["thrTrimSw"] = rhs.thrTrimSwitch;
node["potsWarnMode"] = potsWarningModeLut << rhs.potsWarningMode;
Expand Down Expand Up @@ -1239,10 +1324,17 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
node["thrTraceSrc"] >> thrTrace.src;
rhs.thrTraceSrc = thrTrace.toCpn();

YamlSwitchWarningState switchWarningState;
node["switchWarningState"] >> switchWarningState.src_str;
rhs.switchWarningStates = switchWarningState.toCpn();
rhs.switchWarningEnable = ~switchWarningState.enabled;
if (node["switchWarning"]) {
YamlSwitchWarning switchWarning;
rhs.switchWarningStates = switchWarning.toCpn(node["switchWarning"]);
rhs.switchWarningEnable = ~switchWarning.enabled;
}
else if (node["switchWarningState"]) { // depreciated
YamlSwitchWarningState switchWarningState;
node["switchWarningState"] >> switchWarningState.src_str;
rhs.switchWarningStates = switchWarningState.toCpn();
rhs.switchWarningEnable = ~switchWarningState.enabled;
}

node["thrTrimSw"] >> rhs.thrTrimSwitch;
node["potsWarnMode"] >> potsWarningModeLut >> rhs.potsWarningMode;
Expand Down
4 changes: 2 additions & 2 deletions radio/src/datastructs_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ PACK(struct ModelData {
FlightModeData flightModeData[MAX_FLIGHT_MODES] FUNC(fmd_is_active);

NOBACKUP(uint8_t thrTraceSrc CUST(r_thrSrc,w_thrSrc));
CUST_ATTR(switchWarningState, r_swtchWarn, w_swtchWarn);
NOBACKUP(swarnstate_t switchWarningState SKIP);
CUST_ATTR(switchWarningState, r_swtchWarn, nullptr);
NOBACKUP(swarnstate_t switchWarning ARRAY(3, struct_swtchWarn, nullptr));

GVarData gvars[MAX_GVARS];

Expand Down
10 changes: 5 additions & 5 deletions radio/src/gui/128x64/model_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ void menuModelSetup(event_t event)
break;
}

swarnstate_t states = g_model.switchWarningState;
swarnstate_t states = g_model.switchWarning;

lcdDrawTextAlignedLeft(y, STR_SWITCHWARNING);
#if defined(PCBXLITE)
Expand Down Expand Up @@ -1003,10 +1003,10 @@ void menuModelSetup(event_t event)
swarnstate_t sw_mask = 0;
for(uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
if (SWITCH_WARNING_ALLOWED(i))
if (g_model.switchWarningState & (0x07 << (3 * i)))
if (g_model.switchWarning & (0x07 << (3 * i)))
sw_mask |= (0x07 << (3 * i));
}
g_model.switchWarningState = switches_states & sw_mask;
g_model.switchWarning = switches_states & sw_mask;
AUDIO_WARNING1();
storageDirty(EE_MODEL);
}
Expand All @@ -1024,10 +1024,10 @@ void menuModelSetup(event_t event)
l_posHorz == current && old_posHorz >= 0) {
uint8_t curr_state = (states & 0x07);
// remove old setting
g_model.switchWarningState &= ~(0x07 << (3 * i));
g_model.switchWarning &= ~(0x07 << (3 * i));
// add the new one (if switch UP and 2POS, jump directly to DOWN)
curr_state += (curr_state != 1 || IS_CONFIG_3POS(i) ? 1 : 2);
g_model.switchWarningState |= (curr_state & 0x03) << (3 * i);
g_model.switchWarning |= (curr_state & 0x03) << (3 * i);
storageDirty(EE_MODEL);
#if defined(PCBXLITE)
s_editMode = 0;
Expand Down
10 changes: 5 additions & 5 deletions radio/src/gui/212x64/model_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ void menuModelSetup(event_t event)
}
#endif
lcdDrawTextAlignedLeft(y, STR_SWITCHWARNING);
swarnstate_t states = g_model.switchWarningState;
swarnstate_t states = g_model.switchWarning;

if (attr) {
s_editMode = 0;
Expand All @@ -889,10 +889,10 @@ void menuModelSetup(event_t event)
swarnstate_t sw_mask = 0;
for(uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
if (SWITCH_WARNING_ALLOWED(i))
if (g_model.switchWarningState & (0x07 << (3 * i)))
if (g_model.switchWarning & (0x07 << (3 * i)))
sw_mask |= (0x07 << (3 * i));
}
g_model.switchWarningState = switches_states & sw_mask;
g_model.switchWarning = switches_states & sw_mask;
AUDIO_WARNING1();
storageDirty(EE_MODEL);
}
Expand All @@ -912,10 +912,10 @@ void menuModelSetup(event_t event)
l_posHorz == current) {
uint8_t curr_state = (states & 0x07);
// remove old setting
g_model.switchWarningState &= ~(0x07 << (3 * i));
g_model.switchWarning &= ~(0x07 << (3 * i));
// add the new one (if switch UP and 2POS, jump directly to DOWN)
curr_state += (curr_state != 1 || IS_CONFIG_3POS(i) ? 1 : 2);
g_model.switchWarningState |= (curr_state & 0x03) << (3 * i);
g_model.switchWarning |= (curr_state & 0x03) << (3 * i);
storageDirty(EE_MODEL);
}
lcdDrawChar(
Expand Down
10 changes: 5 additions & 5 deletions radio/src/gui/colorlcd/preflight_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ PreflightChecks::PreflightChecks() : Page(ICON_MODEL_SETUP)

static std::string switchWarninglabel(swsrc_t index)
{
auto warn_pos = g_model.switchWarningState >> (3 * index) & 0x07;
auto warn_pos = g_model.switchWarning >> (3 * index) & 0x07;
return std::string(switchGetName(index)) +
std::string(getSwitchWarnSymbol(warn_pos));
}
Expand Down Expand Up @@ -270,14 +270,14 @@ void SwitchWarnMatrix::onPress(uint8_t btn_id)
if (btn_id >= MAX_SWITCHES) return;
auto sw = sw_idx[btn_id];

swarnstate_t newstate = bfGet(g_model.switchWarningState, 3 * sw, 3);
swarnstate_t newstate = bfGet(g_model.switchWarning, 3 * sw, 3);
if (newstate == 1 && SWITCH_CONFIG(sw) != SWITCH_3POS)
newstate = 3;
else
newstate = (newstate + 1) % 4;

g_model.switchWarningState =
bfSet(g_model.switchWarningState, newstate, 3 * sw, 3);
g_model.switchWarning =
bfSet(g_model.switchWarning, newstate, 3 * sw, 3);
SET_DIRTY();

setTextAndState(btn_id);
Expand All @@ -286,7 +286,7 @@ void SwitchWarnMatrix::onPress(uint8_t btn_id)
bool SwitchWarnMatrix::isActive(uint8_t btn_id)
{
if (btn_id >= MAX_SWITCHES) return false;
return bfGet(g_model.switchWarningState, 3 * sw_idx[btn_id], 3) != 0;
return bfGet(g_model.switchWarning, 3 * sw_idx[btn_id], 3) != 0;
}

PotWarnMatrix::PotWarnMatrix(Window* parent, const rect_t& r) :
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/switch_warn_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void SwitchWarnDialog::paint(BitmapBuffer * dc)
FullScreenDialog::paint(dc);

std::string warn_txt;
swarnstate_t states = g_model.switchWarningState;
swarnstate_t states = g_model.switchWarning;
for (int i = 0; i < MAX_SWITCHES; ++i) {
if (SWITCH_WARNING_ALLOWED(i)) {
swarnstate_t mask = ((swarnstate_t)0x07 << (i*3));
Expand Down
4 changes: 2 additions & 2 deletions radio/src/model_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ void applyDefaultTemplate()
// enable switch warnings
for (int i = 0; i < MAX_SWITCHES; i++) {
if (SWITCH_EXISTS(i)) {
g_model.switchWarningState |= (1 << (3 * i));
g_model.switchWarning |= (1 << (3 * i));
}
}
#else
// enable switch warnings
for (int i = 0; i < MAX_SWITCHES; i++) {
if (SWITCH_WARNING_ALLOWED(i))
g_model.switchWarningState |= (1 << (3 * i));
g_model.switchWarning |= (1 << (3 * i));
}
#endif

Expand Down
5 changes: 3 additions & 2 deletions radio/src/storage/yaml/yaml_datastructs_128x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ static const struct YamlNode struct_ModelData[] = {
YAML_STRUCT("swashR", 64, struct_SwashRingData, swash_is_active),
YAML_ARRAY("flightModeData", 320, 9, struct_FlightModeData, fmd_is_active),
YAML_UNSIGNED_CUST( "thrTraceSrc", 8, r_thrSrc, w_thrSrc ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,w_swtchWarn),
YAML_PADDING( 64 ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,nullptr),
YAML_ARRAY("switchWarning", 3, 21, struct_swtchWarn, nullptr),
YAML_PADDING(1),
YAML_ARRAY("gvars", 56, 9, struct_GVarData, NULL),
YAML_STRUCT("varioData", 40, struct_VarioData, NULL),
YAML_UNSIGNED_CUST( "rssiSource", 8, r_tele_sensor, w_tele_sensor ),
Expand Down
61 changes: 12 additions & 49 deletions radio/src/storage/yaml/yaml_datastructs_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1147,56 +1147,19 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,

}

static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
data += (bitoffs >> 3UL);

swarnstate_t states;
memcpy(&states, data, sizeof(states));

for (uint8_t i = 0; i < switchGetMaxSwitches(); i++) {
// TODO: SWITCH_EXISTS() uses the g_eeGeneral stucture, which might not be
// avail
if (SWITCH_EXISTS(i)) {
// decode check state
// -> 3 bits per switch
auto state = (states >> (3 * i)) & 0x07;

// state == 0 -> no check
// state == 1 -> UP
// state == 2 -> MIDDLE
// state == 3 -> DOWN
char swtchWarn[2] = {switchGetLetter(i), 0};

switch (state) {
case 0:
break;
case 1:
swtchWarn[1] = 'u';
break;
case 2:
swtchWarn[1] = '-';
break;
case 3:
swtchWarn[1] = 'd';
break;
default:
// this should never happen
swtchWarn[1] = 'x';
break;
}

if (swtchWarn[0] >= 'A' && swtchWarn[1] != 0) {
if (!wf(opaque, swtchWarn, 2)) {
return false;
}
}
}
}
static const struct YamlIdStr enum_SwitchWarnPos[] = {
{ 0, "none" },
{ 1, "up" },
{ 2, "mid" },
{ 3, "down" },
{ 0, nullptr },
};

return true;
}
static const struct YamlNode struct_swtchWarn[] {
YAML_IDX_CUST( "sw", sw_read, sw_write ),
YAML_ENUM( "pos", 2, enum_SwitchWarnPos ),
YAML_END,
};

extern const struct YamlIdStr enum_BeeperMode[];

Expand Down
5 changes: 3 additions & 2 deletions radio/src/storage/yaml/yaml_datastructs_nv14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,9 @@ static const struct YamlNode struct_ModelData[] = {
YAML_STRUCT("swashR", 64, struct_SwashRingData, swash_is_active),
YAML_ARRAY("flightModeData", 352, 9, struct_FlightModeData, fmd_is_active),
YAML_UNSIGNED_CUST( "thrTraceSrc", 8, r_thrSrc, w_thrSrc ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,w_swtchWarn),
YAML_PADDING( 64 ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,nullptr),
YAML_ARRAY("switchWarning", 3, 21, struct_swtchWarn, nullptr),
YAML_PADDING(1),
YAML_ARRAY("gvars", 56, 9, struct_GVarData, NULL),
YAML_STRUCT("varioData", 40, struct_VarioData, NULL),
YAML_UNSIGNED_CUST( "rssiSource", 8, r_tele_sensor, w_tele_sensor ),
Expand Down
5 changes: 3 additions & 2 deletions radio/src/storage/yaml/yaml_datastructs_pl18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,9 @@ static const struct YamlNode struct_ModelData[] = {
YAML_STRUCT("swashR", 64, struct_SwashRingData, swash_is_active),
YAML_ARRAY("flightModeData", 384, 9, struct_FlightModeData, fmd_is_active),
YAML_UNSIGNED_CUST( "thrTraceSrc", 8, r_thrSrc, w_thrSrc ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,w_swtchWarn),
YAML_PADDING( 64 ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,nullptr),
YAML_ARRAY("switchWarning", 3, 21, struct_swtchWarn, nullptr),
YAML_PADDING(1),
YAML_ARRAY("gvars", 56, 9, struct_GVarData, NULL),
YAML_STRUCT("varioData", 40, struct_VarioData, NULL),
YAML_UNSIGNED_CUST( "rssiSource", 8, r_tele_sensor, w_tele_sensor ),
Expand Down
5 changes: 3 additions & 2 deletions radio/src/storage/yaml/yaml_datastructs_t20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ static const struct YamlNode struct_ModelData[] = {
YAML_STRUCT("swashR", 64, struct_SwashRingData, swash_is_active),
YAML_ARRAY("flightModeData", 352, 9, struct_FlightModeData, fmd_is_active),
YAML_UNSIGNED_CUST( "thrTraceSrc", 8, r_thrSrc, w_thrSrc ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,w_swtchWarn),
YAML_PADDING( 64 ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,nullptr),
YAML_ARRAY("switchWarning", 3, 21, struct_swtchWarn, nullptr),
YAML_PADDING(1),
YAML_ARRAY("gvars", 56, 9, struct_GVarData, NULL),
YAML_STRUCT("varioData", 40, struct_VarioData, NULL),
YAML_UNSIGNED_CUST( "rssiSource", 8, r_tele_sensor, w_tele_sensor ),
Expand Down
5 changes: 3 additions & 2 deletions radio/src/storage/yaml/yaml_datastructs_tpro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,9 @@ static const struct YamlNode struct_ModelData[] = {
YAML_STRUCT("swashR", 64, struct_SwashRingData, swash_is_active),
YAML_ARRAY("flightModeData", 320, 9, struct_FlightModeData, fmd_is_active),
YAML_UNSIGNED_CUST( "thrTraceSrc", 8, r_thrSrc, w_thrSrc ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,w_swtchWarn),
YAML_PADDING( 64 ),
YAML_CUSTOM("switchWarningState",r_swtchWarn,nullptr),
YAML_ARRAY("switchWarning", 3, 21, struct_swtchWarn, nullptr),
YAML_PADDING(1),
YAML_ARRAY("gvars", 56, 9, struct_GVarData, NULL),
YAML_STRUCT("varioData", 40, struct_VarioData, NULL),
YAML_UNSIGNED_CUST( "rssiSource", 8, r_tele_sensor, w_tele_sensor ),
Expand Down
Loading

0 comments on commit 0db8a92

Please sign in to comment.