1
0
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:
Alessio Morale 2013-07-31 15:23:43 +02:00
parent 2a0533e5e3
commit d99790be71
2 changed files with 33 additions and 4 deletions

View File

@ -51,6 +51,7 @@ int32_t $(NAME)Initialize();
UAVObjHandle $(NAME)Handle(); UAVObjHandle $(NAME)Handle();
void $(NAME)SetDefaults(UAVObjHandle obj, uint16_t instId); void $(NAME)SetDefaults(UAVObjHandle obj, uint16_t instId);
$(DATASTRUCTURES)
/* /*
* Packed Object data (unaligned). * Packed Object data (unaligned).
* Should only be used where 4 byte alignment can be guaranteed * Should only be used where 4 byte alignment can be guaranteed

View File

@ -118,19 +118,39 @@ bool UAVObjectGeneratorFlight::process_object(ObjectInfo *info)
// Replace the $(DATAFIELDS) tag // Replace the $(DATAFIELDS) tag
QString type; QString type;
QString fields; QString fields;
QString dataStructures;
for (int n = 0; n < info->fields.length(); ++n) { for (int n = 0; n < info->fields.length(); ++n) {
// Determine type // Determine type
type = fieldTypeStrC[info->fields[n]->type]; type = fieldTypeStrC[info->fields[n]->type];
// Append field // Append field
// Check if it a named set and creates structures accordingly
if (info->fields[n]->numElements > 1) { 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) fields.append(QString(" %1 %2[%3];\n").arg(type)
.arg(info->fields[n]->name).arg(info->fields[n]->numElements)); .arg(info->fields[n]->name).arg(info->fields[n]->numElements));
}
} else { } else {
fields.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name)); fields.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name));
} }
} }
outInclude.replace(QString("$(DATAFIELDS)"), fields); outInclude.replace(QString("$(DATAFIELDS)"), fields);
outInclude.replace(QString("$(DATASTRUCTURES)"), dataStructures);
// Replace the $(DATAFIELDINFO) tag // Replace the $(DATAFIELDINFO) tag
QString enums; QString enums;
for (int n = 0; n < info->fields.length(); ++n) { for (int n = 0; n < info->fields.length(); ++n) {
@ -206,19 +226,27 @@ bool UAVObjectGeneratorFlight::process_object(ObjectInfo *info)
} else { } else {
// Initialize all fields in the array // Initialize all fields in the array
for (int idx = 0; idx < info->fields[n]->numElements; ++idx) { 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) { 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(info->fields[n]->name)
.arg(optIdentifier)
.arg(idx) .arg(idx)
.arg(info->fields[n]->options.indexOf(info->fields[n]->defaultValues[idx]))); .arg(info->fields[n]->options.indexOf(info->fields[n]->defaultValues[idx])));
} else if (info->fields[n]->type == FIELDTYPE_FLOAT32) { } 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(info->fields[n]->name)
.arg(optIdentifier)
.arg(idx) .arg(idx)
.arg(info->fields[n]->defaultValues[idx].toFloat(), 0, 'e', 6)); .arg(info->fields[n]->defaultValues[idx].toFloat(), 0, 'e', 6));
} else { } else {
initfields.append(QString(" data.%1[%2] = %3;\n") initfields.append(QString(" data.%1%2[%3] = %4;\n")
.arg(info->fields[n]->name) .arg(info->fields[n]->name)
.arg(optIdentifier)
.arg(idx) .arg(idx)
.arg(info->fields[n]->defaultValues[idx].toInt())); .arg(info->fields[n]->defaultValues[idx].toInt()));
} }