From 710677a1f7512adb17b09df3e6dd83756087c99d Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Fri, 17 Dec 2021 11:02:51 +0100 Subject: [PATCH] Use existing function to generate switch name in YAML switch warning field --- .../yaml/yaml_datastructs_funcs.cpp | 2 +- .../storage/yaml/yaml_datastructs_funcs.cpp | 6 +-- radio/src/strhelpers.cpp | 54 ++++++++++++++----- radio/src/strhelpers.h | 2 + 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp index f18d3d8c2..e2b39d0b6 100644 --- a/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp @@ -539,7 +539,7 @@ bool w_swtchWarn(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* // state == 1 -> UP // state == 2 -> MIDDLE // state == 3 -> DOWN - char swtchWarn[2] = {(char)('A' + i), 0}; + char swtchWarn[2] = {getRawSwitchFromIdx(i), 0}; switch (state) { case 0: diff --git a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp index ab4ee3d72..396d1496d 100644 --- a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp @@ -845,8 +845,8 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, // swtchWarn = 0; while (val_len--) { - signed swtch = *(val++) - 'A'; - if (swtch >= NUM_SWITCHES) break; + signed swtch = getRawSwitchIdx(*(val++)); + if (swtch < 0) break; unsigned state = 0; switch (*(val++)) { @@ -887,7 +887,7 @@ static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, // state == 1 -> UP // state == 2 -> MIDDLE // state == 3 -> DOWN - char swtchWarn[2] = {(char)('A' + i), 0}; + char swtchWarn[2] = {getRawSwitchFromIdx(i), 0}; switch (state) { case 0: diff --git a/radio/src/strhelpers.cpp b/radio/src/strhelpers.cpp index ffd3c0b42..b1774da19 100644 --- a/radio/src/strhelpers.cpp +++ b/radio/src/strhelpers.cpp @@ -421,6 +421,45 @@ char *getFlightModeString(char *dest, int8_t idx) // } #if !defined(PCBSKY9X) +int getRawSwitchIdx(char sw) +{ + if (sw < 'A' || sw > 'Z') + return -1; + +#if defined(PCBX7) && !defined(RADIO_TX12) + if (sw >= 'H') + return sw - 'H' + 5; +#if defined(RADIO_T12) + else if (sw == 'G') +#else + else if (sw == 'F') +#endif + return 4; + else + return sw - 'A'; +#else + return sw - 'A'; +#endif +} + +char getRawSwitchFromIdx(int idx) +{ +#if defined(PCBX7) && !defined(RADIO_TX12) + if (idx >= 5) + return 'H' + idx - 5; + else if (idx == 4) +#if defined(RADIO_T12) + return 'G'; +#else + return 'F'; +#endif + else + return 'A' + idx; +#else + return 'A' + idx; +#endif +} + char *getSwitchName(char *dest, swsrc_t idx) { div_t swinfo = switchInfo(idx); @@ -429,20 +468,7 @@ char *getSwitchName(char *dest, swsrc_t idx) strAppend(dest, g_eeGeneral.switchNames[swinfo.quot], LEN_SWITCH_NAME); } else { *dest++ = 'S'; -#if defined(PCBX7) && !defined(RADIO_TX12) - if (swinfo.quot >= 5) - *dest++ = 'H' + swinfo.quot - 5; - else if (swinfo.quot == 4) -#if defined(RADIO_T12) - *dest++ = 'G'; -#else - *dest++ = 'F'; -#endif - else - *dest++ = 'A' + swinfo.quot; -#else - *dest++ = 'A' + swinfo.quot; -#endif + *dest++ = getRawSwitchFromIdx(swinfo.quot); } return dest; } diff --git a/radio/src/strhelpers.h b/radio/src/strhelpers.h index 0ca7ad443..683de26d4 100644 --- a/radio/src/strhelpers.h +++ b/radio/src/strhelpers.h @@ -74,6 +74,8 @@ char *getGVarString(int idx); char *getSwitchPositionName(char *dest, swsrc_t idx); char *getSwitchName(char *dest, swsrc_t idx); char *getSourceString(char *dest, mixsrc_t idx); +int getRawSwitchIdx(char sw); +char getRawSwitchFromIdx(int sw); #endif char *getFlightModeString(char *dest, int8_t idx);