Merge switches / pots / sliders names into their config struct

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:00 +01:00
parent b5aca8f22e
commit 21bf580f9a
18 changed files with 142 additions and 52 deletions
+4 -4
View File
@@ -753,8 +753,8 @@ PACK(struct TrainerData {
swconfig_t switchConfig ARRAY(2,struct_switchConfig,nullptr); \
uint16_t potsConfig ARRAY(2,struct_potConfig,nullptr); /* two bits per pot */ \
uint8_t slidersConfig ARRAY(1,struct_sliderConfig,nullptr); /* 1 bit per slider */ \
NOBACKUP(char switchNames[STORAGE_NUM_SWITCHES][LEN_SWITCH_NAME]); \
NOBACKUP(char anaNames[NUM_STICKS + STORAGE_NUM_POTS + STORAGE_NUM_SLIDERS][LEN_ANA_NAME]); \
NOBACKUP(char switchNames[STORAGE_NUM_SWITCHES][LEN_SWITCH_NAME] SKIP); \
NOBACKUP(char anaNames[NUM_STICKS + STORAGE_NUM_POTS + STORAGE_NUM_SLIDERS][LEN_ANA_NAME] SKIP); \
NOBACKUP(char currModelFilename[LEN_MODEL_FILENAME+1]); \
NOBACKUP(uint8_t spare5:1 SKIP); \
NOBACKUP(uint8_t blOffBright:7); \
@@ -774,8 +774,8 @@ PACK(struct TrainerData {
uint8_t backlightColor; \
swarnstate_t switchUnlockStates; \
swconfig_t switchConfig ARRAY(2,struct_switchConfig,nullptr); \
char switchNames[STORAGE_NUM_SWITCHES][LEN_SWITCH_NAME]; \
char anaNames[NUM_STICKS+STORAGE_NUM_POTS+STORAGE_NUM_SLIDERS][LEN_ANA_NAME]; \
char switchNames[STORAGE_NUM_SWITCHES][LEN_SWITCH_NAME] SKIP; \
char anaNames[NUM_STICKS+STORAGE_NUM_POTS+STORAGE_NUM_SLIDERS][LEN_ANA_NAME] SKIP; \
BLUETOOTH_FIELDS
#else
#define EXTRA_GENERAL_FIELDS
@@ -218,6 +218,29 @@ static bool sw_write(uint32_t idx, yaml_writer_func wf, void* opaque)
return str ? wf(opaque, str, strlen(str)) : true;
}
static void sw_name_read(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, const char* val, uint8_t val_len)
{
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
strncpy(rd->switchNames[idx], val, std::min<uint8_t>(val_len, LEN_SWITCH_NAME));
}
static bool sw_name_write(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, yaml_writer_func wf, void* opaque)
{
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, switchConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
const char* str = rd->switchNames[idx];
return wf(opaque, str, strnlen(str, LEN_SWITCH_NAME));
}
static const struct YamlIdStr enum_SwitchConfig[] = {
{ SWITCH_NONE, "none" },
{ SWITCH_TOGGLE, "toggle" },
@@ -229,6 +252,7 @@ static const struct YamlIdStr enum_SwitchConfig[] = {
static const struct YamlNode struct_switchConfig[] = {
YAML_IDX_CUST( "sw", sw_read, sw_write ),
YAML_ENUM( "type", 2, enum_SwitchConfig),
YAML_CUSTOM( "name", sw_name_read, sw_name_write),
YAML_END
};
@@ -247,6 +271,31 @@ static bool pot_write(uint32_t idx, yaml_writer_func wf, void* opaque)
return str ? wf(opaque, str, strlen(str)) : true;
}
static void pot_name_read(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, const char* val, uint8_t val_len)
{
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, potsConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS;
strncpy(rd->anaNames[idx], val, std::min<uint8_t>(val_len, LEN_ANA_NAME));
}
static bool pot_name_write(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, yaml_writer_func wf, void* opaque)
{
// data / bitoffs already incremented
data -= ((idx + 1) * 2) / 8;
data -= offsetof(RadioData, potsConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS;
const char* str = rd->anaNames[idx];
return wf(opaque, str, strnlen(str, LEN_ANA_NAME));
}
static const struct YamlIdStr enum_PotConfig[] = {
{ POT_NONE, "none" },
{ POT_WITH_DETENT, "with_detent" },
@@ -258,6 +307,7 @@ static const struct YamlIdStr enum_PotConfig[] = {
static const struct YamlNode struct_potConfig[] = {
YAML_IDX_CUST( "pot", pot_read, pot_write ),
YAML_ENUM( "type", 2, enum_PotConfig),
YAML_CUSTOM( "name", pot_name_read, pot_name_write),
YAML_END
};
@@ -276,6 +326,31 @@ static bool slider_write(uint32_t idx, yaml_writer_func wf, void* opaque)
return str ? wf(opaque, str, strlen(str)) : true;
}
static void sl_name_read(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, const char* val, uint8_t val_len)
{
// data / bitoffs already incremented
data -= (idx + 1) / 8;
data -= offsetof(RadioData, slidersConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS + STORAGE_NUM_POTS;
strncpy(rd->anaNames[idx], val, std::min<uint8_t>(val_len, LEN_ANA_NAME));
}
static bool sl_name_write(const YamlNode* node, uint8_t* data, uint32_t bitoffs,
uint16_t idx, yaml_writer_func wf, void* opaque)
{
// data / bitoffs already incremented
data -= (idx + 1) / 8;
data -= offsetof(RadioData, slidersConfig);
RadioData* rd = reinterpret_cast<RadioData*>(data);
idx += NUM_STICKS + STORAGE_NUM_POTS;
const char* str = rd->anaNames[idx];
return wf(opaque, str, strnlen(str, LEN_ANA_NAME));
}
static const struct YamlIdStr enum_SliderConfig[] = {
{ SLIDER_NONE, "none" },
{ SLIDER_WITH_DETENT, "with_detent" },
@@ -285,6 +360,7 @@ static const struct YamlIdStr enum_SliderConfig[] = {
static const struct YamlNode struct_sliderConfig[] = {
YAML_IDX_CUST( "sl", slider_read, slider_write ),
YAML_ENUM( "type", 1, enum_SliderConfig),
YAML_CUSTOM( "name", sl_name_read, sl_name_write),
YAML_END
};
@@ -371,8 +371,8 @@ static const struct YamlNode struct_RadioData[] = {
YAML_ARRAY("switchConfig", 2, 16, struct_switchConfig, nullptr),
YAML_ARRAY("potsConfig", 2, 8, struct_potConfig, nullptr),
YAML_ARRAY("slidersConfig", 1, 8, struct_sliderConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 9, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 216 ),
YAML_STRING("currModelFilename", 17),
YAML_PADDING( 1 ),
YAML_UNSIGNED( "blOffBright", 7 ),
@@ -346,13 +346,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -346,13 +346,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -346,13 +346,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -346,13 +346,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -388,8 +388,8 @@ static const struct YamlNode struct_RadioData[] = {
YAML_ARRAY("switchConfig", 2, 16, struct_switchConfig, nullptr),
YAML_ARRAY("potsConfig", 2, 8, struct_potConfig, nullptr),
YAML_ARRAY("slidersConfig", 1, 8, struct_sliderConfig, nullptr),
YAML_ARRAY("switchNames", 24, 10, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 13, struct_string_24, NULL),
YAML_PADDING( 240 ),
YAML_PADDING( 312 ),
YAML_STRING("currModelFilename", 17),
YAML_PADDING( 1 ),
YAML_UNSIGNED( "blOffBright", 7 ),
@@ -388,8 +388,8 @@ static const struct YamlNode struct_RadioData[] = {
YAML_ARRAY("switchConfig", 2, 16, struct_switchConfig, nullptr),
YAML_ARRAY("potsConfig", 2, 8, struct_potConfig, nullptr),
YAML_ARRAY("slidersConfig", 1, 8, struct_sliderConfig, nullptr),
YAML_ARRAY("switchNames", 24, 10, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 13, struct_string_24, NULL),
YAML_PADDING( 240 ),
YAML_PADDING( 312 ),
YAML_STRING("currModelFilename", 17),
YAML_PADDING( 1 ),
YAML_UNSIGNED( "blOffBright", 7 ),
@@ -346,13 +346,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 8, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 192 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -353,13 +353,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 32 ),
YAML_ARRAY("switchConfig", 2, 16, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 9, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 9, struct_string_24, NULL),
YAML_PADDING( 216 ),
YAML_PADDING( 216 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -392,13 +392,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 64 ),
YAML_ARRAY("switchConfig", 2, 32, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 18, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 12, struct_string_24, NULL),
YAML_PADDING( 432 ),
YAML_PADDING( 288 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -333,11 +333,10 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 5, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 5, struct_string_24, NULL),
YAML_PADDING( 120 ),
YAML_PADDING( 120 ),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -341,13 +341,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 7, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 5, struct_string_24, NULL),
YAML_PADDING( 168 ),
YAML_PADDING( 120 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -338,13 +338,12 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 6, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 144 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
@@ -340,15 +340,14 @@ static const struct YamlNode struct_RadioData[] = {
YAML_UNSIGNED( "backlightColor", 8 ),
YAML_UNSIGNED( "switchUnlockStates", 16 ),
YAML_ARRAY("switchConfig", 2, 8, struct_switchConfig, nullptr),
YAML_ARRAY("switchNames", 24, 6, struct_string_24, NULL),
YAML_ARRAY("anaNames", 24, 6, struct_string_24, NULL),
YAML_PADDING( 144 ),
YAML_PADDING( 144 ),
YAML_PADDING( 8 ),
YAML_STRING("bluetoothName", 10),
YAML_STRING("ownerRegistrationID", 8),
YAML_SIGNED( "gyroMax", 8 ),
YAML_SIGNED( "gyroOffset", 8 ),
YAML_SIGNED( "uartSampleMode", 2 ),
YAML_STRING("currModelFilename", 17),
YAML_END
};
static const struct YamlNode struct_unsigned_8[] = {
+18 -2
View File
@@ -38,7 +38,8 @@ enum YamlDataType {
YDT_ARRAY,
YDT_ENUM,
YDT_UNION,
YDT_PADDING
YDT_PADDING,
YDT_CUSTOM
};
struct YamlIdStr
@@ -62,7 +63,14 @@ struct YamlNode
typedef uint32_t (*cust_idx_read_func)(const char* val, uint8_t val_len);
typedef bool (*cust_idx_write_func)(uint32_t idx, yaml_writer_func wf, void* opaque);
typedef void (*cust_read_func)(const YamlNode* node, uint8_t* data,
uint32_t bitoffs, uint16_t idx,
const char* val, uint8_t val_len);
typedef bool (*cust_write_func)(const YamlNode* node, uint8_t* data,
uint32_t bitoffs, uint16_t idx,
yaml_writer_func wf, void* opaque);
uint8_t type;
uint32_t size; // bits
uint8_t tag_len;
@@ -92,6 +100,11 @@ struct YamlNode
cust_idx_read_func read;
cust_idx_write_func write;
} _cust_idx;
struct {
cust_read_func read;
cust_write_func write;
} _cust_attr;
} u;
};
@@ -135,6 +148,9 @@ struct YamlNode
#define YAML_PADDING(bits) \
{ .type=YDT_PADDING, .size=(bits) }
#define YAML_CUSTOM(tag, f_read, f_write) \
{ .type=YDT_CUSTOM, .size=0, YAML_TAG(tag), .u={._cust_attr={.read=(f_read), .write=(f_write) }} }
#define YAML_END \
{ .type=YDT_NONE }
+16 -7
View File
@@ -50,7 +50,7 @@ uint32_t yaml_parse_enum(const struct YamlIdStr* choices, const char* val, uint8
}
static void yaml_set_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
const char* val, uint8_t val_len)
uint16_t idx, const char* val, uint8_t val_len)
{
uint32_t i = 0;
@@ -78,6 +78,10 @@ static void yaml_set_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
case YDT_ENUM:
i = yaml_parse_enum(node->u._enum.choices, val, val_len);
break;
case YDT_CUSTOM:
if (node->u._cust_attr.read)
node->u._cust_attr.read(node, ptr, bit_ofs, idx, val, val_len);
return;
default:
break;
}
@@ -127,7 +131,7 @@ static bool yaml_output_string(const char* str, uint32_t max_len,
}
static bool yaml_output_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* node,
yaml_writer_func wf, void* opaque)
uint16_t idx, yaml_writer_func wf, void* opaque)
{
if (node->type == YDT_NONE)
return false;
@@ -153,6 +157,11 @@ static bool yaml_output_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* nod
if (!yaml_output_string((const char*)ptr, (node->size)>>3UL, wf, opaque))
return false;
}
else if (node->type == YDT_CUSTOM) {
if (node->u._cust_attr.write
&& !node->u._cust_attr.write(node, ptr, bit_ofs, idx, wf, opaque))
return false;
}
else {
unsigned int i = yaml_get_bits(ptr, bit_ofs, node->size);
@@ -435,7 +444,7 @@ void YamlTreeWalker::setAttrValue(char* buf, uint8_t len)
idx_invalid = true;
}
else {
yaml_set_attr(data, getBitOffset(), attr, buf, len);
yaml_set_attr(data, getBitOffset(), attr, getElmts(), buf, len);
//walker.dump_stack();
}
}
@@ -500,7 +509,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
for(int i=2; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(NULL, 0, node, wf, opaque))
if (!yaml_output_attr(NULL, 0, node, getElmts(), wf, opaque))
return false; // TODO: error handling???
// grab attr idx...
@@ -512,7 +521,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
for(int i=1; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(data, getBitOffset(), attr, wf, opaque))
if (!yaml_output_attr(data, getBitOffset(), attr, getElmts(), wf, opaque))
return false; // TODO: error handling???
if (attr->type != YDT_ARRAY
@@ -545,7 +554,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
for(int i=2; i < getLevel(); i++)
if (!wf(opaque, " ", 3))
return false;
if (!yaml_output_attr(NULL, 0, getNode(), wf, opaque))
if (!yaml_output_attr(NULL, 0, getNode(), getElmts(), wf, opaque))
return false; // TODO: error handling???
continue;
}
@@ -596,7 +605,7 @@ bool YamlTreeWalker::generate(yaml_writer_func wf, void* opaque)
if (!wf(opaque, "\r\n", 2))
return false;
}
else if (!yaml_output_attr(data, getBitOffset(), attr, wf, opaque))
else if (!yaml_output_attr(data, getBitOffset(), attr, getElmts(), wf, opaque))
return false; // TODO: error handling???
toNextAttr();