Improve custom functions

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:02 +01:00
parent 3da2aee1ff
commit b5e9c68056
31 changed files with 481 additions and 132 deletions
@@ -210,23 +210,7 @@ uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs)
return 0;
}
uint8_t select_custom_fn(void* user, uint8_t* data, uint32_t bitoffs)
{
data += bitoffs >> 3UL;
data -= offsetof(CustomFunctionData, all);
auto cf = reinterpret_cast<CustomFunctionData*>(data);
switch(cf->func) {
case FUNC_PLAY_TRACK:
case FUNC_BACKGND_MUSIC:
case FUNC_PLAY_SCRIPT:
// play: 0
return 0;
}
// all: 1
return 1;
}
uint8_t select_script_input(void* user, uint8_t* data, uint32_t bitoffs)
{
@@ -698,3 +682,144 @@ bool w_flightModes(const YamlNode* node, uint32_t val,
}
return true;
}
#define r_customFn nullptr
static const char* _func_reset_param_lookup[] = {
"Tmr1","Tmr2","Tmr3","All","Tele"
};
static const char* _func_failsafe_lookup[] = {
"Int","Ext"
};
// used in read routine as well
const char* _func_sound_lookup[] = {
"Bp1","Bp2","Bp3","Wrn1","Wrn2",
"Chee","Rata","Tick","Sirn","Ring",
"SciF","Robt","Chrp","Tada","Crck","Alrm"
};
// force external linkage
extern const uint8_t _func_sound_lookup_size = sizeof(_func_sound_lookup);
bool w_customFn(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
data += bitoffs >> 3UL;
data -= offsetof(CustomFunctionData, all);
auto cfn = reinterpret_cast<CustomFunctionData*>(data);
uint8_t func = CFN_FUNC(cfn);
const char* str = nullptr;
switch (func) {
case FUNC_OVERRIDE_CHANNEL:
str = yaml_unsigned2str(CFN_CH_INDEX(cfn)); // CH index
if (!wf(opaque, str, strlen(str))) return false;
if (!wf(opaque, ",", 1)) return false;
str = yaml_unsigned2str(CFN_PARAM(cfn)); // value
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_TRAINER: {
int16_t value = CFN_CH_INDEX(cfn);
switch(value) {
case 0:
if (!wf(opaque, "sticks", 6)) return false;
break;
case NUM_STICKS + 1:
if (!wf(opaque, "chans", 5)) return false;
break;
default:
if (value > 0 && value < NUM_STICKS + 1) {
str = yaml_output_enum(value - 1 + MIXSRC_FIRST_STICK, enum_MixSources);
if (str && !wf(opaque, str, strlen(str))) return false;
}
}
} break;
case FUNC_RESET:
if (CFN_PARAM(cfn) < FUNC_RESET_PARAM_FIRST_TELEM) {
// Tmr1,Tmr2,Tmr3,All
str = _func_reset_param_lookup[CFN_PARAM(cfn)];
} else {
// sensor index
str = yaml_unsigned2str(CFN_PARAM(cfn) - FUNC_RESET_PARAM_FIRST_TELEM);
}
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_VOLUME:
case FUNC_BACKLIGHT:
case FUNC_PLAY_VALUE:
if (!w_mixSrcRaw(nullptr, CFN_PARAM(cfn), wf, opaque)) return false;
break;
case FUNC_PLAY_SOUND:
// Bp1,Bp2,Bp3,Wrn1,Wrn2,Chee,Rata,Tick,Sirn,Ring,SciF,Robt,Chrp,Tada,Crck,Alrm
str = _func_sound_lookup[CFN_PARAM(cfn)];
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_PLAY_TRACK:
case FUNC_BACKGND_MUSIC:
case FUNC_PLAY_SCRIPT:
if (!wf(opaque, cfn->play.name, strnlen(cfn->play.name, LEN_FUNCTION_NAME)))
return false;
break;
case FUNC_SET_TIMER:
// Tmr1,Tmr2,Tmr3
str = _func_reset_param_lookup[CFN_TIMER_INDEX(cfn)];
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_SET_FAILSAFE:
// Int,Ext
str = _func_failsafe_lookup[CFN_PARAM(cfn)];
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_HAPTIC:
case FUNC_LOGS: // 10th of seconds
str = yaml_unsigned2str(CFN_PARAM(cfn));
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_ADJUST_GVAR:
switch(CFN_GVAR_MODE(cfn)) {
case FUNC_ADJUST_GVAR_CONSTANT:
case FUNC_ADJUST_GVAR_INCDEC:
str = yaml_signed2str(CFN_PARAM(cfn));
if (!wf(opaque, str, strlen(str))) return false;
break;
case FUNC_ADJUST_GVAR_SOURCE:
if (!w_mixSrcRaw(nullptr, CFN_PARAM(cfn), wf, opaque)) return false;
break;
case FUNC_ADJUST_GVAR_GVAR:
if (!w_mixSrcRaw(nullptr, CFN_PARAM(cfn) + MIXSRC_FIRST_GVAR, wf, opaque)) return false;
break;
}
}
if (HAS_ENABLE_PARAM(func)) {
// ",0/1"
if (!wf(opaque,CFN_ACTIVE(cfn) ? ",1":",0",2)) return false;
} else if (HAS_REPEAT_PARAM(func)) {
// ","
if (!wf(opaque,",",1)) return false;
if (CFN_PLAY_REPEAT(cfn) == 0) {
// "1x"
if (!wf(opaque,"1x",2)) return false;
} else if (CFN_PLAY_REPEAT(cfn) == CFN_PLAY_REPEAT_NOSTART) {
// "!1x"
if (!wf(opaque,"!1x",3)) return false;
} else {
// repeat time in seconds
str = yaml_unsigned2str(CFN_PLAY_REPEAT(cfn) * CFN_PLAY_REPEAT_MUL);
if (!wf(opaque, str, strlen(str))) return false;
}
}
return true;
}
@@ -300,12 +300,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "spare", 16 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "val2", 16 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -318,8 +318,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 48, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 48 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -293,12 +293,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -311,8 +311,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -293,12 +293,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -311,8 +311,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -293,12 +293,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -311,8 +311,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -293,12 +293,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -311,8 +311,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -321,12 +321,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "spare", 16 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "val2", 16 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -339,8 +339,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 48, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 48 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -321,12 +321,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "spare", 16 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 16 ),
YAML_SIGNED( "val2", 16 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -339,8 +339,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 48, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 48 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -293,12 +293,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -311,8 +311,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -300,12 +300,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -318,8 +318,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -339,12 +339,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -357,8 +357,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -280,12 +280,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -298,8 +298,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -288,12 +288,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -306,8 +306,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -285,12 +285,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -303,8 +303,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {
@@ -287,12 +287,12 @@ static const struct YamlNode struct_anonymous_2[] = {
YAML_SIGNED( "val", 16 ),
YAML_UNSIGNED( "mode", 8 ),
YAML_UNSIGNED( "param", 8 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "spare", 32 ),
YAML_END
};
static const struct YamlNode struct_anonymous_3[] = {
YAML_SIGNED( "val1", 32 ),
YAML_PADDING( 32 ),
YAML_SIGNED( "val2", 32 ),
YAML_END
};
static const struct YamlNode union_anonymous_0_elmts[] = {
@@ -305,8 +305,9 @@ static const struct YamlNode struct_CustomFunctionData[] = {
YAML_IDX,
YAML_SIGNED_CUST( "swtch", 9, r_swtchSrc, w_swtchSrc ),
YAML_ENUM("func", 7, enum_Functions),
YAML_UNION("fp", 64, union_anonymous_0_elmts, select_custom_fn),
YAML_UNSIGNED( "active", 8 ),
YAML_CUSTOM("def",r_customFn,w_customFn),
YAML_PADDING( 64 ),
YAML_PADDING( 8 ),
YAML_END
};
static const struct YamlNode struct_string_24[] = {