mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-11-29 07:24:13 +01:00
OP-1058: Implement explicit item accessor for MultiElement fields.
This implementation uses a union containing the Array implementing the MultiElement field data and a struct made from element names. it allow to replace the following sintax: settings.PitchRatePID[STABILIZATIONSETTINGS_PITCHRATEPID_KP] with a more concise and less error prone settings.PitchRatePID.fields.Kp while allowing the direct array access using the notation settings.PitchRatePID.data[n]
This commit is contained in:
parent
2a0533e5e3
commit
d99790be71
@ -51,6 +51,7 @@ int32_t $(NAME)Initialize();
|
||||
UAVObjHandle $(NAME)Handle();
|
||||
void $(NAME)SetDefaults(UAVObjHandle obj, uint16_t instId);
|
||||
|
||||
$(DATASTRUCTURES)
|
||||
/*
|
||||
* Packed Object data (unaligned).
|
||||
* Should only be used where 4 byte alignment can be guaranteed
|
||||
|
@ -118,19 +118,39 @@ bool UAVObjectGeneratorFlight::process_object(ObjectInfo *info)
|
||||
// Replace the $(DATAFIELDS) tag
|
||||
QString type;
|
||||
QString fields;
|
||||
QString dataStructures;
|
||||
for (int n = 0; n < info->fields.length(); ++n) {
|
||||
// Determine type
|
||||
type = fieldTypeStrC[info->fields[n]->type];
|
||||
// Append field
|
||||
// Check if it a named set and creates structures accordingly
|
||||
if (info->fields[n]->numElements > 1) {
|
||||
if(info->fields[n]->elementNames[0].compare(QString("0")) != 0){
|
||||
QString unionTypeName = QString("%1%2Data").arg(info->name).arg(info->fields[n]->name);
|
||||
QString unionType = QString("typedef union {\n");
|
||||
unionType.append(QString(" %1 data[%2];\n").arg(type).arg(info->fields[n]->numElements));
|
||||
unionType.append(QString(" struct __attribute__ ((__packed__)) {\n"));
|
||||
for(int f =0; f < info->fields[n]->elementNames.count(); f++){
|
||||
unionType.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->elementNames[f]));
|
||||
}
|
||||
unionType.append(QString(" } fields;\n"));
|
||||
unionType.append(QString("} %1 ;\n\n").arg(unionTypeName));
|
||||
|
||||
dataStructures.append(unionType);
|
||||
|
||||
fields.append(QString(" %1 %2;\n").arg(unionTypeName)
|
||||
.arg(info->fields[n]->name));
|
||||
|
||||
} else {
|
||||
fields.append(QString(" %1 %2[%3];\n").arg(type)
|
||||
.arg(info->fields[n]->name).arg(info->fields[n]->numElements));
|
||||
}
|
||||
} else {
|
||||
fields.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name));
|
||||
}
|
||||
}
|
||||
outInclude.replace(QString("$(DATAFIELDS)"), fields);
|
||||
|
||||
outInclude.replace(QString("$(DATASTRUCTURES)"), dataStructures);
|
||||
// Replace the $(DATAFIELDINFO) tag
|
||||
QString enums;
|
||||
for (int n = 0; n < info->fields.length(); ++n) {
|
||||
@ -206,19 +226,27 @@ bool UAVObjectGeneratorFlight::process_object(ObjectInfo *info)
|
||||
} else {
|
||||
// Initialize all fields in the array
|
||||
for (int idx = 0; idx < info->fields[n]->numElements; ++idx) {
|
||||
QString optIdentifier;
|
||||
if(info->fields[n]->elementNames[0].compare(QString("0")) != 0){
|
||||
optIdentifier = QString(".data");
|
||||
}
|
||||
|
||||
if (info->fields[n]->type == FIELDTYPE_ENUM) {
|
||||
initfields.append(QString(" data.%1[%2] = %3;\n")
|
||||
initfields.append(QString(" data.%1%2[%3] = %4;\n")
|
||||
.arg(info->fields[n]->name)
|
||||
.arg(optIdentifier)
|
||||
.arg(idx)
|
||||
.arg(info->fields[n]->options.indexOf(info->fields[n]->defaultValues[idx])));
|
||||
} else if (info->fields[n]->type == FIELDTYPE_FLOAT32) {
|
||||
initfields.append(QString(" data.%1[%2] = %3f;\n")
|
||||
initfields.append(QString(" data.%1%2[%3] = %4f;\n")
|
||||
.arg(info->fields[n]->name)
|
||||
.arg(optIdentifier)
|
||||
.arg(idx)
|
||||
.arg(info->fields[n]->defaultValues[idx].toFloat(), 0, 'e', 6));
|
||||
} else {
|
||||
initfields.append(QString(" data.%1[%2] = %3;\n")
|
||||
initfields.append(QString(" data.%1%2[%3] = %4;\n")
|
||||
.arg(info->fields[n]->name)
|
||||
.arg(optIdentifier)
|
||||
.arg(idx)
|
||||
.arg(info->fields[n]->defaultValues[idx].toInt()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user