From c9b20063ec201bf8006e2d7eb3c336b9ddad131a Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sat, 23 Oct 2021 15:39:04 +0200 Subject: [PATCH] Use YAML output functions from version 220 as long as possible This allows to save some flash memory. --- .../yaml/yaml_datastructs_funcs.cpp | 110 ++--- .../storage/yaml/yaml_datastructs_funcs.cpp | 391 +++++------------- 2 files changed, 150 insertions(+), 351 deletions(-) diff --git a/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp index 333242058..ef1c22b4f 100644 --- a/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/conversions/yaml/yaml_datastructs_funcs.cpp @@ -28,21 +28,21 @@ #define in_read_weight nullptr -static bool in_write_weight(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool in_write_weight(const YamlNode* node, uint32_t val, yaml_writer_func wf, + void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - - if (sval > GVAR_SMALL-11 && sval < GVAR_SMALL-1) { - char n = GVAR_SMALL - sval + '0'; - return wf(opaque, "-GV", 3) && wf(opaque, &n, 1); - } - else if (sval < -GVAR_SMALL+11 && sval > -GVAR_SMALL+1) { - char n = val - GVAR_SMALL + '1'; - return wf(opaque, "GV", 2) && wf(opaque, &n, 1); - } + int32_t sval = yaml_to_signed(val, node->size); - char* s = yaml_signed2str(sval); - return wf(opaque, s, strlen(s)); + if (sval > GVAR_SMALL - 11 && sval < GVAR_SMALL - 1) { + char n = GVAR_SMALL - sval + '0'; + return wf(opaque, "-GV", 3) && wf(opaque, &n, 1); + } else if (sval < -GVAR_SMALL + 11 && sval > -GVAR_SMALL + 1) { + char n = val - GVAR_SMALL + '1'; + return wf(opaque, "GV", 2) && wf(opaque, &n, 1); + } + + char* s = yaml_signed2str(sval); + return wf(opaque, s, strlen(s)); } extern const struct YamlIdStr enum_MixSources[]; @@ -60,7 +60,7 @@ static bool output_source_1_param(const char* src_prefix, size_t src_len, uint32 return true; } -static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { const char* str = nullptr; @@ -148,22 +148,22 @@ static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf, #define r_vbat_min nullptr -static bool w_vbat_min(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_vbat_min(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 90); - return wf(opaque, s, strlen(s)); + char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 90); + return wf(opaque, s, strlen(s)); } #define r_vbat_max nullptr -static bool w_vbat_max(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_vbat_max(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 120); - return wf(opaque, s, strlen(s)); + char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 120); + return wf(opaque, s, strlen(s)); } #if defined(COLORLCD) -static uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs) { data += bitoffs >> 3UL; data -= sizeof(ZoneOptionValueEnum); @@ -174,7 +174,7 @@ static uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs) } #endif -static uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) { data += bitoffs >> 3UL; data -= offsetof(ModuleData, ppm); @@ -210,7 +210,7 @@ static uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) return 0; } -static uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs) { data += bitoffs >> 3UL; data -= offsetof(CustomFunctionData, all); @@ -228,31 +228,31 @@ static uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs) return 1; } -static uint8_t select_script_input(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_script_input(void* user, uint8_t* data, uint32_t bitoffs) { // always use 'value' return 0; } -static uint8_t select_id1(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_id1(void* user, uint8_t* data, uint32_t bitoffs) { // always use 'id' return 0; } -static uint8_t select_id2(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_id2(void* user, uint8_t* data, uint32_t bitoffs) { // always use 'instance' return 0; } -static uint8_t select_sensor_cfg(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_sensor_cfg(void* user, uint8_t* data, uint32_t bitoffs) { // always use 'param' return 5; } -static bool sw_write(void* user, yaml_writer_func wf, void* opaque) +bool sw_write(void* user, yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(); @@ -262,8 +262,8 @@ static bool sw_write(void* user, yaml_writer_func wf, void* opaque) return str ? wf(opaque, str, strlen(str)) : true; } -static bool w_stick_name(void* user, uint8_t* data, uint32_t bitoffs, - yaml_writer_func wf, void* opaque) +bool w_stick_name(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(1); @@ -274,7 +274,7 @@ static bool w_stick_name(void* user, uint8_t* data, uint32_t bitoffs, strnlen(rd->anaNames[idx], LEN_ANA_NAME)); } -static bool stick_name_valid(void* user, uint8_t* data, uint32_t bitoffs) +bool stick_name_valid(void* user, uint8_t* data, uint32_t bitoffs) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(); @@ -288,8 +288,8 @@ static const struct YamlNode struct_sticksConfig[] = { YAML_END }; -static bool sw_name_write(void* user, uint8_t* data, uint32_t bitoffs, - yaml_writer_func wf, void* opaque) +bool sw_name_write(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(1); @@ -318,7 +318,7 @@ static const struct YamlNode struct_switchConfig[] = { YAML_END }; -static bool pot_write(void* user, yaml_writer_func wf, void* opaque) +bool pot_write(void* user, yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(); @@ -327,8 +327,8 @@ static bool pot_write(void* user, yaml_writer_func wf, void* opaque) return str ? wf(opaque, str, strlen(str)) : true; } -static bool pot_name_write(void* user, uint8_t* data, uint32_t bitoffs, - yaml_writer_func wf, void* opaque) +bool pot_name_write(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(1); @@ -358,7 +358,7 @@ static const struct YamlNode struct_potConfig[] = { YAML_END }; -static bool slider_write(void* user, yaml_writer_func wf, void* opaque) +bool slider_write(void* user, yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(); @@ -368,8 +368,8 @@ static bool slider_write(void* user, yaml_writer_func wf, void* opaque) return str ? wf(opaque, str, strlen(str)) : true; } -static bool sl_name_write(void* user, uint8_t* data, uint32_t bitoffs, - yaml_writer_func wf, void* opaque) +bool sl_name_write(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(1); @@ -410,7 +410,7 @@ extern const struct YamlIdStr enum_SwitchSources[]; #define r_swtchSrc nullptr -static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { int32_t sval = yaml_to_signed(val, node->size); if (sval < 0) { @@ -461,19 +461,19 @@ static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, return wf(opaque, str, strlen(str)); } -static bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs) +bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs) { data += bitoffs >> 3UL; return ((CustomFunctionData*)data)->swtch; } -static bool gvar_is_active(void* user, uint8_t* data, uint32_t bitoffs) +bool gvar_is_active(void* user, uint8_t* data, uint32_t bitoffs) { gvar_t* gvar = (gvar_t*)(data + (bitoffs>>3UL)); return *gvar != GVAR_MAX+1; } -static bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs) +bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs) { uint32_t data_ofs = bitoffs >> 3UL; if (data_ofs == offsetof(ModelData, flightModeData)) { @@ -494,7 +494,7 @@ static bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs) #define r_swtchWarn nullptr -static bool w_swtchWarn(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_swtchWarn(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { for (int i = 0; i < STORAGE_NUM_SWITCHES; i++) { //if (SWITCH_EXISTS(i)) { @@ -542,7 +542,7 @@ extern const struct YamlIdStr enum_BeeperMode[]; #define r_beeperMode nullptr -static bool w_beeperMode(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_beeperMode(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { int32_t sval = yaml_to_signed(val,node->size); const char* str = yaml_output_enum(sval, enum_BeeperMode); @@ -551,7 +551,7 @@ static bool w_beeperMode(const YamlNode* node, uint32_t val, yaml_writer_func wf #define r_5pos nullptr -static bool w_5pos(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_5pos(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { int32_t sval = yaml_to_signed(val,node->size); char* s = yaml_signed2str(sval + 2); @@ -560,7 +560,7 @@ static bool w_5pos(const YamlNode* node, uint32_t val, yaml_writer_func wf, void #define r_vol nullptr -static bool w_vol(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_vol(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { int32_t sval = yaml_to_signed(val,node->size); char* s = yaml_signed2str(sval + VOLUME_LEVEL_DEF); @@ -569,7 +569,7 @@ static bool w_vol(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* #define r_spPitch nullptr -static bool w_spPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_spPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { char* s = yaml_signed2str(val * 15); return wf(opaque, s, strlen(s)); @@ -577,7 +577,7 @@ static bool w_spPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, v #define r_vPitch nullptr -static bool w_vPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +bool w_vPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { int32_t sval = yaml_to_signed(val,node->size); char* s = yaml_signed2str(sval * 10); @@ -605,8 +605,8 @@ const struct YamlIdStr enum_TrainerMode[] = { #define r_trainerMode nullptr -static bool w_trainerMode(const YamlNode* node, uint32_t val, - yaml_writer_func wf, void* opaque) +bool w_trainerMode(const YamlNode* node, uint32_t val, + yaml_writer_func wf, void* opaque) { const char* str = nullptr; str = yaml_output_enum(val, enum_TrainerMode); @@ -621,15 +621,15 @@ static bool w_trainerMode(const YamlNode* node, uint32_t val, #if !defined(COLORLCD) #define r_tele_screen_type nullptr -static const char* _tele_screen_type_lookup[] = { +const char* _tele_screen_type_lookup[] = { "NONE", "VALUES", "BARS", "SCRIPT", }; -static bool w_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, - yaml_writer_func wf, void* opaque) +bool w_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(1); @@ -641,7 +641,7 @@ static bool w_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, return wf(opaque, str, strlen(str)); } -static uint8_t select_tele_screen_data(void* user, uint8_t* data, uint32_t bitoffs) +uint8_t select_tele_screen_data(void* user, uint8_t* data, uint32_t bitoffs) { auto tw = reinterpret_cast(user); uint16_t idx = tw->getElmts(2); diff --git a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp index fa7dac400..9cc1efda9 100644 --- a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp @@ -23,54 +23,86 @@ #include "yaml_bits.h" #include "yaml_tree_walker.h" +// Use definitions from v220 conversions as long as nothing changes +#include "../conversions/datastructs_220.h" + +namespace yaml_conv_220 { + bool in_write_weight(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_vbat_min(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_vbat_max(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_script_input(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_id1(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_id2(void* user, uint8_t* data, uint32_t bitoffs); + uint8_t select_sensor_cfg(void* user, uint8_t* data, uint32_t bitoffs); + bool sw_write(void* user, yaml_writer_func wf, void* opaque); + bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs); + bool gvar_is_active(void* user, uint8_t* data, uint32_t bitoffs); + bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs); + bool w_beeperMode(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_5pos(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_vol(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_spPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + bool w_vPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque); + + extern const struct YamlIdStr enum_TrainerMode[]; + bool w_trainerMode(const YamlNode* node, uint32_t val, + yaml_writer_func wf, void* opaque); + + extern const char* _tele_screen_type_lookup[]; + bool w_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, + yaml_writer_func wf, void* opaque); +}; + +static_assert(sizeof(CustomFunctionData) == sizeof(bin_storage_220::CustomFunctionData),""); +static_assert(sizeof(ModuleData) == sizeof(bin_storage_220::ModuleData),""); +static_assert(sizeof(FlightModeData) == sizeof(bin_storage_220::FlightModeData),""); + +#if !defined(COLORLCD) +static_assert(sizeof(TelemetryScreenData) == sizeof(bin_storage_220::TelemetryScreenData),""); +#endif + #define GVAR_SMALL 128 static uint32_t in_read_weight(const YamlNode* node, const char* val, uint8_t val_len) { - if ((val_len == 4) - && (val[0] == '-') - && (val[1] == 'G') - && (val[2] == 'V') - && (val[3] >= '1') - && (val[3] <= '9')) { + if ((val_len == 4) + && (val[0] == '-') + && (val[1] == 'G') + && (val[2] == 'V') + && (val[3] >= '1') + && (val[3] <= '9')) { - TRACE("%.*s -> %i\n", val_len, val, GVAR_SMALL - (val[3] - '0')); - return GVAR_SMALL - (val[3] - '0'); // -GVx => 128 - x - } + TRACE("%.*s -> %i\n", val_len, val, GVAR_SMALL - (val[3] - '0')); + return GVAR_SMALL - (val[3] - '0'); // -GVx => 128 - x + } - if ((val_len == 3) - && (val[0] == 'G') - && (val[1] == 'V') - && (val[2] >= '1') - && (val[2] <= '9')) { + if ((val_len == 3) + && (val[0] == 'G') + && (val[1] == 'V') + && (val[2] >= '1') + && (val[2] <= '9')) { - TRACE("%.*s -> %i\n", val_len, val, -GVAR_SMALL + (val[2] - '1')); - return -GVAR_SMALL + (val[2] - '1'); // GVx => -128 + (x-1) - } + TRACE("%.*s -> %i\n", val_len, val, -GVAR_SMALL + (val[2] - '1')); + return -GVAR_SMALL + (val[2] - '1'); // GVx => -128 + (x-1) + } - return (uint32_t)yaml_str2int(val, val_len); + return (uint32_t)yaml_str2int(val, val_len); } -static bool in_write_weight(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) +static bool in_write_weight(const YamlNode* node, uint32_t val, + yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - - if (sval > GVAR_SMALL-11 && sval < GVAR_SMALL-1) { - char n = GVAR_SMALL - sval + '0'; - return wf(opaque, "-GV", 3) && wf(opaque, &n, 1); - } - else if (sval < -GVAR_SMALL+11 && sval > -GVAR_SMALL+1) { - char n = val - GVAR_SMALL + '1'; - return wf(opaque, "GV", 2) && wf(opaque, &n, 1); - } - - char* s = yaml_signed2str(sval); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::in_write_weight(node, val, wf, opaque); } extern const struct YamlIdStr enum_MixSources[]; -// TODO sources: parse/output +// sources: parse/output // - lua(script#,n): LUA mix outputs // - ls(n): logical switches // - tr(n): trainer input @@ -161,101 +193,9 @@ static uint32_t r_mixSrcRaw(const YamlNode* node, const char* val, uint8_t val_l return yaml_parse_enum(enum_MixSources, val, val_len); } -static constexpr char closing_parenthesis[] = ")"; - -static bool output_source_1_param(const char* src_prefix, size_t src_len, uint32_t n, - yaml_writer_func wf, void* opaque) -{ - if (!wf(opaque, src_prefix, src_len)) return false; - const char* str = yaml_unsigned2str(n); - if (!wf(opaque, str, strlen(str))) return false; - return true; -} - static bool w_mixSrcRaw(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - const char* str = nullptr; - - if (val >= MIXSRC_FIRST_INPUT - && val <= MIXSRC_LAST_INPUT) { - - if (!wf(opaque, "I", 1)) - return false; - - str = yaml_unsigned2str(val - MIXSRC_FIRST_INPUT); - } -#if defined(LUA_INPUTS) - else if (val >= MIXSRC_FIRST_LUA - && val <= MIXSRC_LAST_LUA) { - - if (!wf(opaque, "lua(", 4)) return false; - - val -= MIXSRC_FIRST_LUA; - uint32_t script = val / MAX_SCRIPT_OUTPUTS; - - if (!output_source_1_param("lua(", 4, script, wf, opaque)) return false; - if (!wf(opaque, ",", 1)) return false; - - val = val % MAX_SCRIPT_OUTPUTS; - str = yaml_unsigned2str(val); - - if (!wf(opaque, str, strlen(str))) return false; - str = closing_parenthesis; - } -#endif - else if (val >= MIXSRC_FIRST_LOGICAL_SWITCH - && val <= MIXSRC_LAST_LOGICAL_SWITCH) { - - val -= MIXSRC_FIRST_LOGICAL_SWITCH; - if (!output_source_1_param("ls(", 3, val, wf, opaque)) return false; - str = closing_parenthesis; - } - else if (val >= MIXSRC_FIRST_TRAINER - && val <= MIXSRC_LAST_TRAINER) { - - val -= MIXSRC_FIRST_TRAINER; - if (!output_source_1_param("tr(", 3, val, wf, opaque)) return false; - str = closing_parenthesis; - } - else if (val >= MIXSRC_FIRST_CH - && val <= MIXSRC_LAST_CH) { - - val -= MIXSRC_FIRST_CH; - if (!output_source_1_param("ch(", 3, val, wf, opaque)) return false; - str = closing_parenthesis; - } - else if (val >= MIXSRC_FIRST_GVAR - && val <= MIXSRC_LAST_GVAR) { - - val -= MIXSRC_FIRST_GVAR; - if (!output_source_1_param("gv(", 3, val, wf, opaque)) return false; - str = closing_parenthesis; - } - else if (val >= MIXSRC_FIRST_TELEM - && val <= MIXSRC_LAST_TELEM) { - - val -= MIXSRC_FIRST_TELEM; - uint8_t sign = val % 3; - val = val / 3; - if (!wf(opaque, "tele(", 5)) return false; - if (sign == 1) { - if (!wf(opaque, "-", 1)) return false; - } else if (sign == 2) { - if (!wf(opaque, "+", 1)) return false; - } - str = yaml_unsigned2str(val); - if (!wf(opaque, str, strlen(str))) return false; - str = closing_parenthesis; - } - else { - str = yaml_output_enum(val, enum_MixSources); - } - - if (str) { - return wf(opaque, str, strlen(str)); - } - - return true; + return yaml_conv_220::w_mixSrcRaw(node, val, wf, opaque); } static uint32_t r_vbat_min(const YamlNode* node, const char* val, uint8_t val_len) @@ -266,99 +206,55 @@ static uint32_t r_vbat_min(const YamlNode* node, const char* val, uint8_t val_le static bool w_vbat_min(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 90); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_vbat_min(node, val, wf, opaque); } static uint32_t r_vbat_max(const YamlNode* node, const char* val, uint8_t val_len) { - int32_t v = yaml_str2int(val, val_len); - return (uint32_t)(v - 120); + int32_t v = yaml_str2int(val, val_len); + return (uint32_t)(v - 120); } static bool w_vbat_max(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - char* s = yaml_signed2str(yaml_to_signed(val,node->size) + 120); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_vbat_max(node, val, wf, opaque); } #if defined(COLORLCD) static uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs) { - data += bitoffs >> 3UL; - data -= sizeof(ZoneOptionValueEnum); - ZoneOptionValueEnum* p_zovt = (ZoneOptionValueEnum*)data; - if (*p_zovt > ZOV_String) - return 0; - return *p_zovt; + return yaml_conv_220::select_zov(user, data, bitoffs); } #endif static uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) { - data += bitoffs >> 3UL; - data -= offsetof(ModuleData, ppm); - - ModuleData* mod_data = reinterpret_cast(data); - switch(mod_data->type) { - case MODULE_TYPE_NONE: - case MODULE_TYPE_PPM: - case MODULE_TYPE_DSM2: - case MODULE_TYPE_CROSSFIRE: - case MODULE_TYPE_GHOST: - return 1; - case MODULE_TYPE_MULTIMODULE: - return 2; - case MODULE_TYPE_XJT_PXX1: - case MODULE_TYPE_R9M_PXX1: - case MODULE_TYPE_R9M_LITE_PXX1: - return 3; - case MODULE_TYPE_SBUS: - return 4; - case MODULE_TYPE_ISRM_PXX2: - case MODULE_TYPE_R9M_PXX2: - case MODULE_TYPE_R9M_LITE_PXX2: - case MODULE_TYPE_R9M_LITE_PRO_PXX2: - case MODULE_TYPE_XJT_LITE_PXX2: - return 5; - case MODULE_TYPE_FLYSKY: - if (mod_data->subType == FLYSKY_SUBTYPE_AFHDS2A) - return 6; - if (mod_data->subType == FLYSKY_SUBTYPE_AFHDS3) - return 7; - } - return 0; + return yaml_conv_220::select_mod_type(user, data, bitoffs); } static uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs) { - // always use 'all' - //TODO - return 1; + return yaml_conv_220::select_custom_fn(user, data, bitoffs); } static uint8_t select_script_input(void* user, uint8_t* data, uint32_t bitoffs) { - // always use 'value' - return 0; + return yaml_conv_220::select_script_input(user, data, bitoffs); } static uint8_t select_id1(void* user, uint8_t* data, uint32_t bitoffs) { - // always use 'id' - return 0; + return yaml_conv_220::select_id1(user, data, bitoffs); } static uint8_t select_id2(void* user, uint8_t* data, uint32_t bitoffs) { - // always use 'instance' - return 0; + return yaml_conv_220::select_id2(user, data, bitoffs); } static uint8_t select_sensor_cfg(void* user, uint8_t* data, uint32_t bitoffs) { - // always use 'param' - return 5; + return yaml_conv_220::select_sensor_cfg(user, data, bitoffs); } static uint32_t sw_read(void* user, const char* val, uint8_t val_len) @@ -370,14 +266,9 @@ static uint32_t sw_read(void* user, const char* val, uint8_t val_len) return -1; } -static bool sw_write(void* user, yaml_writer_func wf, void* opaque) +bool sw_write(void* user, yaml_writer_func wf, void* opaque) { - auto tw = reinterpret_cast(user); - uint16_t idx = tw->getElmts(); - - const char* str = - yaml_output_enum(idx + MIXSRC_FIRST_SWITCH, enum_MixSources); - return str ? wf(opaque, str, strlen(str)) : true; + return yaml_conv_220::sw_write(user, wf, opaque); } static void r_stick_name(void* user, uint8_t* data, uint32_t bitoffs, @@ -659,84 +550,22 @@ static uint32_t r_swtchSrc(const YamlNode* node, const char* val, uint8_t val_le static bool w_swtchSrc(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val, node->size); - if (sval < 0) { - wf(opaque, "!", 1); - sval = abs(sval); - } - - const char* str = NULL; - if (sval >= SWSRC_FIRST_LOGICAL_SWITCH - && sval <= SWSRC_LAST_LOGICAL_SWITCH) { - - wf(opaque, "L", 1); - str = yaml_unsigned2str(sval - SWSRC_FIRST_LOGICAL_SWITCH + 1); - return wf(opaque,str, strlen(str)); - } -#if NUM_XPOTS > 0 - else if (sval >= SWSRC_FIRST_MULTIPOS_SWITCH - && sval <= SWSRC_LAST_MULTIPOS_SWITCH) { - - wf(opaque, "6P", 2); - - // pot #: start with 6P1 - sval -= SWSRC_FIRST_MULTIPOS_SWITCH; - str = yaml_unsigned2str(sval / XPOTS_MULTIPOS_COUNT); - wf(opaque,str, strlen(str)); - - // position - str = yaml_unsigned2str(sval % XPOTS_MULTIPOS_COUNT); - return wf(opaque,str, strlen(str)); - } -#endif - else if (sval >= SWSRC_FIRST_FLIGHT_MODE - && sval <= SWSRC_LAST_FLIGHT_MODE) { - - wf(opaque, "FM", 2); - str = yaml_unsigned2str(sval - SWSRC_FIRST_FLIGHT_MODE); - return wf(opaque,str, strlen(str)); - } - else if (sval >= SWSRC_FIRST_SENSOR - && sval <= SWSRC_LAST_SENSOR) { - - wf(opaque, "T", 2); - str = yaml_unsigned2str(sval - SWSRC_FIRST_SENSOR + 1); - return wf(opaque,str, strlen(str)); - } - - str = yaml_output_enum(sval, enum_SwitchSources); - return wf(opaque, str, strlen(str)); + return yaml_conv_220::w_swtchSrc(node, val, wf, opaque); } -static bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs) +bool cfn_is_active(void* user, uint8_t* data, uint32_t bitoffs) { - data += bitoffs >> 3UL; - return ((CustomFunctionData*)data)->swtch; + return yaml_conv_220::cfn_is_active(user, data, bitoffs); } static bool gvar_is_active(void* user, uint8_t* data, uint32_t bitoffs) { - gvar_t* gvar = (gvar_t*)(data + (bitoffs>>3UL)); - return *gvar != GVAR_MAX+1; + return yaml_conv_220::gvar_is_active(user, data, bitoffs); } static bool fmd_is_active(void* user, uint8_t* data, uint32_t bitoffs) { - uint32_t data_ofs = bitoffs >> 3UL; - if (data_ofs == offsetof(ModelData, flightModeData)) { - return !yaml_is_zero(data, bitoffs, sizeof(FlightModeData)*8); - } - - bool is_active = !yaml_is_zero(data, bitoffs, - (sizeof(FlightModeData) - - sizeof(FlightModeData::gvars))*8); - - FlightModeData* fmd = (FlightModeData*)(data + data_ofs); - for (uint8_t i=0; igvars[i] != GVAR_MAX+1; - } - - return is_active; + return yaml_conv_220::fmd_is_active(user, data, bitoffs); } static uint32_t r_swtchWarn(const YamlNode* node, const char* val, uint8_t val_len) @@ -833,21 +662,17 @@ static uint32_t r_beeperMode(const YamlNode* node, const char* val, uint8_t val_ static bool w_beeperMode(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - const char* str = yaml_output_enum(sval, enum_BeeperMode); - return wf(opaque, str, strlen(str)); + return yaml_conv_220::w_beeperMode(node, val, wf, opaque); } static uint32_t r_5pos(const YamlNode* node, const char* val, uint8_t val_len) { - return (uint32_t)(yaml_str2int(val, val_len) - 2); + return (uint32_t)(yaml_str2int(val, val_len) - 2); } static bool w_5pos(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - char* s = yaml_signed2str(sval + 2); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_5pos(node, val, wf, opaque); } static uint32_t r_vol(const YamlNode* node, const char* val, uint8_t val_len) @@ -857,32 +682,27 @@ static uint32_t r_vol(const YamlNode* node, const char* val, uint8_t val_len) static bool w_vol(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - char* s = yaml_signed2str(sval + VOLUME_LEVEL_DEF); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_vol(node, val, wf, opaque); } static uint32_t r_spPitch(const YamlNode* node, const char* val, uint8_t val_len) { - return (uint32_t)(yaml_str2int(val, val_len) / 15); + return (uint32_t)(yaml_str2int(val, val_len) / 15); } static bool w_spPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - char* s = yaml_signed2str(val * 15); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_spPitch(node, val, wf, opaque); } static uint32_t r_vPitch(const YamlNode* node, const char* val, uint8_t val_len) { - return (uint32_t)(yaml_str2int(val, val_len) / 10); + return (uint32_t)(yaml_str2int(val, val_len) / 10); } static bool w_vPitch(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - int32_t sval = yaml_to_signed(val,node->size); - char* s = yaml_signed2str(sval * 10); - return wf(opaque, s, strlen(s)); + return yaml_conv_220::w_vPitch(node, val, wf, opaque); } const struct YamlIdStr enum_TrainerMode[] = { @@ -912,31 +732,17 @@ static uint32_t r_trainerMode(const YamlNode* node, const char* val, uint8_t val static bool w_trainerMode(const YamlNode* node, uint32_t val, yaml_writer_func wf, void* opaque) { - const char* str = nullptr; - str = yaml_output_enum(val, enum_TrainerMode); - - if (str) { - return wf(opaque, str, strlen(str)); - } - - return true; + return yaml_conv_220::w_trainerMode(node, val, wf, opaque); } #if !defined(COLORLCD) -static const char* _tele_screen_type_lookup[] = { - "NONE", - "VALUES", - "BARS", - "SCRIPT", -}; - static void r_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, const char* val, uint8_t val_len) { uint8_t type = 0; for (uint8_t i = 0; i < 4; i++) { - if (!strncmp(val, _tele_screen_type_lookup[i], val_len)) { + if (!strncmp(val, yaml_conv_220::_tele_screen_type_lookup[i], val_len)) { type = i; break; } @@ -954,14 +760,7 @@ static void r_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, static bool w_tele_screen_type(void* user, uint8_t* data, uint32_t bitoffs, yaml_writer_func wf, void* opaque) { - auto tw = reinterpret_cast(user); - uint16_t idx = tw->getElmts(1); - - data -= sizeof(TelemetryScreenData) * idx + 1; - uint8_t type = ((*data) >> (2 * idx)) & 0x03; - - const char* str = _tele_screen_type_lookup[type]; - return wf(opaque, str, strlen(str)); + return yaml_conv_220::w_tele_screen_type(user, data, bitoffs, wf, opaque); } static uint8_t select_tele_screen_data(void* user, uint8_t* data, uint32_t bitoffs)