2013-04-05 22:46:56 +02:00
/**
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @ file uavobjectfield . cpp
* @ author The OpenPilot Team , http : //www.openpilot.org Copyright (C) 2010.
* @ see The GNU Public License ( GPL ) Version 3
* @ addtogroup GCSPlugins GCS Plugins
* @ {
* @ addtogroup UAVObjectsPlugin UAVObjects Plugin
* @ {
* @ brief The UAVUObjects GCS plugin
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
*/
# include "uavobjectfield.h"
# include <QtEndian>
# include <QDebug>
2013-05-19 16:37:30 +02:00
UAVObjectField : : UAVObjectField ( const QString & name , const QString & units , FieldType type , quint32 numElements , const QStringList & options , const QString & limits )
2013-04-05 22:46:56 +02:00
{
QStringList elementNames ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Set element names
2013-05-19 16:37:30 +02:00
for ( quint32 n = 0 ; n < numElements ; + + n ) {
2013-04-05 22:46:56 +02:00
elementNames . append ( QString ( " %1 " ) . arg ( n ) ) ;
}
// Initialize
2013-05-19 16:37:30 +02:00
constructorInitialize ( name , units , type , elementNames , options , limits ) ;
2013-04-05 22:46:56 +02:00
}
2013-05-19 16:37:30 +02:00
UAVObjectField : : UAVObjectField ( const QString & name , const QString & units , FieldType type , const QStringList & elementNames , const QStringList & options , const QString & limits )
2013-04-05 22:46:56 +02:00
{
2013-05-19 16:37:30 +02:00
constructorInitialize ( name , units , type , elementNames , options , limits ) ;
2013-04-05 22:46:56 +02:00
}
2013-05-19 16:37:30 +02:00
void UAVObjectField : : constructorInitialize ( const QString & name , const QString & units , FieldType type , const QStringList & elementNames , const QStringList & options , const QString & limits )
2013-04-05 22:46:56 +02:00
{
// Copy params
2013-05-19 16:37:30 +02:00
this - > name = name ;
this - > units = units ;
this - > type = type ;
this - > options = options ;
this - > numElements = elementNames . length ( ) ;
this - > offset = 0 ;
this - > data = NULL ;
2013-04-05 22:46:56 +02:00
this - > obj = NULL ;
this - > elementNames = elementNames ;
// Set field size
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
numBytesPerElement = sizeof ( qint8 ) ;
break ;
case INT16 :
numBytesPerElement = sizeof ( qint16 ) ;
break ;
case INT32 :
numBytesPerElement = sizeof ( qint32 ) ;
break ;
case UINT8 :
numBytesPerElement = sizeof ( quint8 ) ;
break ;
case UINT16 :
numBytesPerElement = sizeof ( quint16 ) ;
break ;
case UINT32 :
numBytesPerElement = sizeof ( quint32 ) ;
break ;
case FLOAT32 :
numBytesPerElement = sizeof ( quint32 ) ;
break ;
case ENUM :
numBytesPerElement = sizeof ( quint8 ) ;
break ;
case BITFIELD :
numBytesPerElement = sizeof ( quint8 ) ;
2013-05-19 16:37:30 +02:00
this - > options = QStringList ( ) < < tr ( " 0 " ) < < tr ( " 1 " ) ;
2013-04-05 22:46:56 +02:00
break ;
case STRING :
numBytesPerElement = sizeof ( quint8 ) ;
break ;
default :
numBytesPerElement = 0 ;
}
limitsInitialize ( limits ) ;
}
void UAVObjectField : : limitsInitialize ( const QString & limits )
{
2013-04-20 21:23:00 +02:00
// Limit string format:
2013-05-19 16:37:30 +02:00
// % - start char
// XXXX - optional BOARD_TYPE and BOARD_REVISION (4 hex digits)
// TY - rule type (EQ-equal, NE-not equal, BE-between, BI-bigger, SM-smaller)
// VAL - values for TY separated by colon
// , - rule separator (may have leading or trailing spaces)
// ; - element separator (may have leading or trailing spaces)
2013-04-20 21:23:00 +02:00
//
// Examples:
2013-05-19 16:37:30 +02:00
// Disable few flight modes for Revo (00903):
// "%0903NE:Autotune:VelocityControl:PositionHold:ReturnToBase:Land:PathPlanner"
// Original CC board (rev 1), first element bigger than 3 and second element inside [2.3-5.0]:
// "%0401BI:3; %BE:2.3:5"
// Set applicable range [0-500] for 3 elements of array for all boards:
// "%BE:0:500; %BE:0:500; %BE:0:500"
if ( limits . isEmpty ( ) ) {
2013-04-05 22:46:56 +02:00
return ;
2013-05-19 16:37:30 +02:00
}
2013-04-20 21:23:00 +02:00
QStringList stringPerElement = limits . split ( " ; " ) ;
2013-05-19 16:37:30 +02:00
quint32 index = 0 ;
foreach ( QString str , stringPerElement ) {
2013-04-20 21:23:00 +02:00
QStringList ruleList = str . split ( " , " ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
QList < LimitStruct > limitList ;
2013-05-19 16:37:30 +02:00
foreach ( QString rule , ruleList ) {
QString _str = rule . trimmed ( ) ;
if ( _str . isEmpty ( ) ) {
2013-04-05 22:46:56 +02:00
continue ;
2013-05-19 16:37:30 +02:00
}
QStringList valuesPerElement = _str . split ( " : " ) ;
2013-04-05 22:46:56 +02:00
LimitStruct lstruc ;
2013-05-19 16:37:30 +02:00
bool startFlag = valuesPerElement . at ( 0 ) . startsWith ( " % " ) ;
bool maxIndexFlag = ( int ) ( index ) < ( int ) numElements ;
bool elemNumberSizeFlag = valuesPerElement . at ( 0 ) . size ( ) = = 3 ;
2013-04-05 22:46:56 +02:00
bool aux ;
2013-05-19 16:37:30 +02:00
valuesPerElement . at ( 0 ) . mid ( 1 , 4 ) . toInt ( & aux , 16 ) ;
bool b4 = ( ( valuesPerElement . at ( 0 ) . size ( ) ) = = 7 & & aux ) ;
if ( startFlag & & maxIndexFlag & & ( elemNumberSizeFlag | | b4 ) ) {
if ( b4 ) {
lstruc . board = valuesPerElement . at ( 0 ) . mid ( 1 , 4 ) . toInt ( & aux , 16 ) ;
} else {
lstruc . board = 0 ;
}
if ( valuesPerElement . at ( 0 ) . right ( 2 ) = = " EQ " ) {
lstruc . type = EQUAL ;
} else if ( valuesPerElement . at ( 0 ) . right ( 2 ) = = " NE " ) {
lstruc . type = NOT_EQUAL ;
} else if ( valuesPerElement . at ( 0 ) . right ( 2 ) = = " BE " ) {
lstruc . type = BETWEEN ;
} else if ( valuesPerElement . at ( 0 ) . right ( 2 ) = = " BI " ) {
lstruc . type = BIGGER ;
} else if ( valuesPerElement . at ( 0 ) . right ( 2 ) = = " SM " ) {
lstruc . type = SMALLER ;
} else {
qDebug ( ) < < " limits parsing failed (invalid property) on UAVObjectField " < < name ;
}
2013-04-05 22:46:56 +02:00
valuesPerElement . removeAt ( 0 ) ;
2013-05-19 16:37:30 +02:00
foreach ( QString _value , valuesPerElement ) {
QString value = _value . trimmed ( ) ;
switch ( type ) {
2013-04-05 22:46:56 +02:00
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
lstruc . values . append ( ( quint32 ) value . toULong ( ) ) ;
break ;
case INT8 :
case INT16 :
case INT32 :
lstruc . values . append ( ( qint32 ) value . toLong ( ) ) ;
break ;
case FLOAT32 :
lstruc . values . append ( ( float ) value . toFloat ( ) ) ;
break ;
case ENUM :
lstruc . values . append ( ( QString ) value ) ;
break ;
case STRING :
lstruc . values . append ( ( QString ) value ) ;
break ;
default :
lstruc . values . append ( QVariant ( ) ) ;
}
}
limitList . append ( lstruc ) ;
2013-05-19 16:37:30 +02:00
} else {
if ( ! valuesPerElement . at ( 0 ) . isEmpty ( ) & & ! startFlag ) {
qDebug ( ) < < " limits parsing failed (property doesn't start with %) on UAVObjectField " < < name ;
} else if ( ! maxIndexFlag ) {
qDebug ( ) < < " limits parsing failed (index>numelements) on UAVObjectField " < < name < < " index " < < index < < " numElements " < < numElements ;
} else if ( ! elemNumberSizeFlag | | ! b4 ) {
qDebug ( ) < < " limits parsing failed limit not starting with %XX or %YYYYXX where XX is the limit type and YYYY is the board type on UAVObjectField " < < name ;
}
2013-04-05 22:46:56 +02:00
}
}
2013-05-19 16:37:30 +02:00
elementLimits . insert ( index , limitList ) ;
2013-04-05 22:46:56 +02:00
+ + index ;
}
2013-05-19 16:37:30 +02:00
foreach ( QList < LimitStruct > limitList , elementLimits ) {
foreach ( LimitStruct limit , limitList ) {
qDebug ( ) < < " Limit type " < < limit . type < < " for board " < < limit . board < < " for field " < < getName ( ) ;
foreach ( QVariant var , limit . values ) {
qDebug ( ) < < " value " < < var ;
2013-04-05 22:46:56 +02:00
}
}
}
}
2013-05-19 16:37:30 +02:00
bool UAVObjectField : : isWithinLimits ( QVariant var , quint32 index , int board )
2013-04-05 22:46:56 +02:00
{
2013-05-19 16:37:30 +02:00
if ( ! elementLimits . keys ( ) . contains ( index ) ) {
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
2013-05-19 16:37:30 +02:00
foreach ( LimitStruct struc , elementLimits . value ( index ) ) {
if ( ( struc . board ! = board ) & & board ! = 0 & & struc . board ! = 0 ) {
2013-04-05 22:46:56 +02:00
continue ;
2013-05-19 16:37:30 +02:00
}
switch ( struc . type ) {
2013-04-05 22:46:56 +02:00
case EQUAL :
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toInt ( ) = = vars . toInt ( ) ) {
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toUInt ( ) = = vars . toUInt ( ) ) {
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
case STRING :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toString ( ) = = vars . toString ( ) ) {
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toFloat ( ) = = vars . toFloat ( ) ) {
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return true ;
}
break ;
case NOT_EQUAL :
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toInt ( ) = = vars . toInt ( ) ) {
2013-04-05 22:46:56 +02:00
return false ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toUInt ( ) = = vars . toUInt ( ) ) {
2013-04-05 22:46:56 +02:00
return false ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
case STRING :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toString ( ) = = vars . toString ( ) ) {
2013-04-05 22:46:56 +02:00
return false ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
foreach ( QVariant vars , struc . values ) {
if ( var . toFloat ( ) = = vars . toFloat ( ) ) {
2013-04-05 22:46:56 +02:00
return false ;
2013-05-19 16:37:30 +02:00
}
2013-04-05 22:46:56 +02:00
}
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return true ;
}
break ;
case BETWEEN :
2013-05-19 16:37:30 +02:00
if ( struc . values . length ( ) < 2 ) {
qDebug ( ) < < __FUNCTION__ < < " between limit with less than 1 pair, aborting; field: " < < name ;
2013-04-05 22:46:56 +02:00
return true ;
}
2013-05-19 16:37:30 +02:00
if ( struc . values . length ( ) > 2 ) {
qDebug ( ) < < __FUNCTION__ < < " between limit with more than 1 pair, using first; field " < < name ;
}
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toInt ( ) > = struc . values . at ( 0 ) . toInt ( ) & & var . toInt ( ) < = struc . values . at ( 1 ) . toInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toUInt ( ) > = struc . values . at ( 0 ) . toUInt ( ) & & var . toUInt ( ) < = struc . values . at ( 1 ) . toUInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
2013-05-19 16:37:30 +02:00
if ( ! ( options . indexOf ( var . toString ( ) ) > = options . indexOf ( struc . values . at ( 0 ) . toString ( ) ) & & options . indexOf ( var . toString ( ) ) < = options . indexOf ( struc . values . at ( 1 ) . toString ( ) ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case STRING :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toFloat ( ) > = struc . values . at ( 0 ) . toFloat ( ) & & var . toFloat ( ) < = struc . values . at ( 1 ) . toFloat ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return true ;
}
break ;
case BIGGER :
2013-05-19 16:37:30 +02:00
if ( struc . values . length ( ) < 1 ) {
qDebug ( ) < < __FUNCTION__ < < " BIGGER limit with less than 1 value, aborting; field: " < < name ;
2013-04-05 22:46:56 +02:00
return true ;
}
2013-05-19 16:37:30 +02:00
if ( struc . values . length ( ) > 1 ) {
qDebug ( ) < < __FUNCTION__ < < " BIGGER limit with more than 1 value, using first; field " < < name ;
}
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toInt ( ) > = struc . values . at ( 0 ) . toInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toUInt ( ) > = struc . values . at ( 0 ) . toUInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
2013-05-19 16:37:30 +02:00
if ( ! ( options . indexOf ( var . toString ( ) ) > = options . indexOf ( struc . values . at ( 0 ) . toString ( ) ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case STRING :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toFloat ( ) > = struc . values . at ( 0 ) . toFloat ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return true ;
}
break ;
case SMALLER :
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toInt ( ) < = struc . values . at ( 0 ) . toInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
case UINT16 :
case UINT32 :
case BITFIELD :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toUInt ( ) < = struc . values . at ( 0 ) . toUInt ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
2013-05-19 16:37:30 +02:00
if ( ! ( options . indexOf ( var . toString ( ) ) < = options . indexOf ( struc . values . at ( 0 ) . toString ( ) ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case STRING :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
if ( ! ( var . toFloat ( ) < = struc . values . at ( 0 ) . toFloat ( ) ) ) {
return false ;
}
2013-04-05 22:46:56 +02:00
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return true ;
}
}
}
return true ;
}
2013-05-19 16:37:30 +02:00
QVariant UAVObjectField : : getMaxLimit ( quint32 index , int board )
2013-04-05 22:46:56 +02:00
{
2013-05-19 16:37:30 +02:00
if ( ! elementLimits . keys ( ) . contains ( index ) ) {
2013-04-05 22:46:56 +02:00
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
}
foreach ( LimitStruct struc , elementLimits . value ( index ) ) {
if ( ( struc . board ! = board ) & & board ! = 0 & & struc . board ! = 0 ) {
2013-04-05 22:46:56 +02:00
continue ;
2013-05-19 16:37:30 +02:00
}
switch ( struc . type ) {
2013-04-05 22:46:56 +02:00
case EQUAL :
case NOT_EQUAL :
case BIGGER :
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
break ;
case BETWEEN :
return struc . values . at ( 1 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case SMALLER :
return struc . values . at ( 0 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
}
return QVariant ( ) ;
}
QVariant UAVObjectField : : getMinLimit ( quint32 index , int board )
{
2013-05-19 16:37:30 +02:00
if ( ! elementLimits . keys ( ) . contains ( index ) ) {
2013-04-05 22:46:56 +02:00
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
}
foreach ( LimitStruct struc , elementLimits . value ( index ) ) {
if ( ( struc . board ! = board ) & & board ! = 0 & & struc . board ! = 0 ) {
2013-04-05 22:46:56 +02:00
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
}
switch ( struc . type ) {
2013-04-05 22:46:56 +02:00
case EQUAL :
case NOT_EQUAL :
case SMALLER :
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
break ;
case BETWEEN :
return struc . values . at ( 0 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case BIGGER :
return struc . values . at ( 0 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return QVariant ( ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
}
return QVariant ( ) ;
}
2013-05-19 16:37:30 +02:00
void UAVObjectField : : initialize ( quint8 * data , quint32 dataOffset , UAVObject * obj )
2013-04-05 22:46:56 +02:00
{
2013-05-19 16:37:30 +02:00
this - > data = data ;
2013-04-05 22:46:56 +02:00
this - > offset = dataOffset ;
2013-05-19 16:37:30 +02:00
this - > obj = obj ;
2013-04-05 22:46:56 +02:00
clear ( ) ;
}
UAVObjectField : : FieldType UAVObjectField : : getType ( )
{
return type ;
}
QString UAVObjectField : : getTypeAsString ( )
{
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case UAVObjectField : : INT8 :
return " int8 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : INT16 :
return " int16 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : INT32 :
return " int32 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : UINT8 :
return " uint8 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : UINT16 :
return " uint16 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : UINT32 :
return " uint32 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : FLOAT32 :
return " float32 " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : ENUM :
return " enum " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : BITFIELD :
return " bitfield " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
case UAVObjectField : : STRING :
return " string " ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
default :
return " " ;
}
}
QStringList UAVObjectField : : getElementNames ( )
{
return elementNames ;
}
2013-05-19 16:37:30 +02:00
UAVObject * UAVObjectField : : getObject ( )
2013-04-05 22:46:56 +02:00
{
return obj ;
}
void UAVObjectField : : clear ( )
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case BITFIELD :
2013-05-19 16:37:30 +02:00
memset ( & data [ offset ] , 0 , numBytesPerElement * ( ( quint32 ) ( 1 + ( numElements - 1 ) / 8 ) ) ) ;
2013-04-05 22:46:56 +02:00
break ;
default :
2013-05-19 16:37:30 +02:00
memset ( & data [ offset ] , 0 , numBytesPerElement * numElements ) ;
2013-04-05 22:46:56 +02:00
break ;
}
}
QString UAVObjectField : : getName ( )
{
return name ;
}
QString UAVObjectField : : getUnits ( )
{
return units ;
}
QStringList UAVObjectField : : getOptions ( )
{
return options ;
}
quint32 UAVObjectField : : getNumElements ( )
{
return numElements ;
}
quint32 UAVObjectField : : getDataOffset ( )
{
return offset ;
}
quint32 UAVObjectField : : getNumBytes ( )
{
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case BITFIELD :
2013-05-19 16:37:30 +02:00
return numBytesPerElement * ( ( quint32 ) ( 1 + ( numElements - 1 ) / 8 ) ) ;
2013-04-05 22:46:56 +02:00
break ;
default :
return numBytesPerElement * numElements ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
}
QString UAVObjectField : : toString ( )
{
QString sout ;
2013-05-19 16:37:30 +02:00
sout . append ( QString ( " %1: [ " ) . arg ( name ) ) ;
for ( unsigned int n = 0 ; n < numElements ; + + n ) {
sout . append ( QString ( " %1 " ) . arg ( getDouble ( n ) ) ) ;
2013-04-05 22:46:56 +02:00
}
2013-05-19 16:37:30 +02:00
sout . append ( QString ( " ] %1 \n " ) . arg ( units ) ) ;
2013-04-05 22:46:56 +02:00
return sout ;
}
2013-05-19 16:37:30 +02:00
qint32 UAVObjectField : : pack ( quint8 * dataOut )
2013-04-05 22:46:56 +02:00
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Pack each element in output buffer
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
memcpy ( dataOut , & data [ offset ] , numElements ) ;
break ;
case INT16 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
qint16 value ;
2013-05-19 16:37:30 +02:00
memcpy ( & value , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
qToLittleEndian < qint16 > ( value , & dataOut [ numBytesPerElement * index ] ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case INT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
qint32 value ;
2013-05-19 16:37:30 +02:00
memcpy ( & value , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
qToLittleEndian < qint32 > ( value , & dataOut [ numBytesPerElement * index ] ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT8 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
dataOut [ numBytesPerElement * index ] = data [ offset + numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT16 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint16 value ;
2013-05-19 16:37:30 +02:00
memcpy ( & value , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
qToLittleEndian < quint16 > ( value , & dataOut [ numBytesPerElement * index ] ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint32 value ;
2013-05-19 16:37:30 +02:00
memcpy ( & value , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
qToLittleEndian < quint32 > ( value , & dataOut [ numBytesPerElement * index ] ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint32 value ;
2013-05-19 16:37:30 +02:00
memcpy ( & value , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
qToLittleEndian < quint32 > ( value , & dataOut [ numBytesPerElement * index ] ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case ENUM :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
dataOut [ numBytesPerElement * index ] = data [ offset + numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case BITFIELD :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < ( quint32 ) ( 1 + ( numElements - 1 ) / 8 ) ; + + index ) {
dataOut [ numBytesPerElement * index ] = data [ offset + numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case STRING :
memcpy ( dataOut , & data [ offset ] , numElements ) ;
break ;
}
// Done
return getNumBytes ( ) ;
}
2013-05-19 16:37:30 +02:00
qint32 UAVObjectField : : unpack ( const quint8 * dataIn )
2013-04-05 22:46:56 +02:00
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Unpack each element from input buffer
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
memcpy ( & data [ offset ] , dataIn , numElements ) ;
break ;
case INT16 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
qint16 value ;
2013-05-19 16:37:30 +02:00
value = qFromLittleEndian < qint16 > ( & dataIn [ numBytesPerElement * index ] ) ;
memcpy ( & data [ offset + numBytesPerElement * index ] , & value , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case INT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
qint32 value ;
2013-05-19 16:37:30 +02:00
value = qFromLittleEndian < qint32 > ( & dataIn [ numBytesPerElement * index ] ) ;
memcpy ( & data [ offset + numBytesPerElement * index ] , & value , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT8 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
data [ offset + numBytesPerElement * index ] = dataIn [ numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT16 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint16 value ;
2013-05-19 16:37:30 +02:00
value = qFromLittleEndian < quint16 > ( & dataIn [ numBytesPerElement * index ] ) ;
memcpy ( & data [ offset + numBytesPerElement * index ] , & value , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case UINT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint32 value ;
2013-05-19 16:37:30 +02:00
value = qFromLittleEndian < quint32 > ( & dataIn [ numBytesPerElement * index ] ) ;
memcpy ( & data [ offset + numBytesPerElement * index ] , & value , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case FLOAT32 :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
2013-04-05 22:46:56 +02:00
quint32 value ;
2013-05-19 16:37:30 +02:00
value = qFromLittleEndian < quint32 > ( & dataIn [ numBytesPerElement * index ] ) ;
memcpy ( & data [ offset + numBytesPerElement * index ] , & value , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
}
break ;
case ENUM :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < numElements ; + + index ) {
data [ offset + numBytesPerElement * index ] = dataIn [ numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case BITFIELD :
2013-05-19 16:37:30 +02:00
for ( quint32 index = 0 ; index < ( quint32 ) ( 1 + ( numElements - 1 ) / 8 ) ; + + index ) {
data [ offset + numBytesPerElement * index ] = dataIn [ numBytesPerElement * index ] ;
2013-04-05 22:46:56 +02:00
}
break ;
case STRING :
memcpy ( & data [ offset ] , dataIn , numElements ) ;
break ;
}
// Done
return getNumBytes ( ) ;
}
bool UAVObjectField : : isNumeric ( )
{
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case INT16 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case INT32 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT16 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT32 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case BITFIELD :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case STRING :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return false ;
}
}
bool UAVObjectField : : isText ( )
{
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case INT16 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case INT32 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT8 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT16 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case UINT32 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case FLOAT32 :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case BITFIELD :
return false ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case STRING :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
default :
return false ;
}
}
QVariant UAVObjectField : : getValue ( quint32 index )
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Check that index is not out of bounds
2013-05-19 16:37:30 +02:00
if ( index > = numElements ) {
2013-04-05 22:46:56 +02:00
return QVariant ( ) ;
}
// Get value
2013-05-19 16:37:30 +02:00
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
{
qint8 tmpint8 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpint8 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpint8 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case INT16 :
{
qint16 tmpint16 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpint16 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpint16 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case INT32 :
{
qint32 tmpint32 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpint32 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpint32 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case UINT8 :
{
quint8 tmpuint8 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpuint8 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpuint8 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case UINT16 :
{
quint16 tmpuint16 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpuint16 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpuint16 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case UINT32 :
{
quint32 tmpuint32 ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpuint32 , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpuint32 ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case FLOAT32 :
{
float tmpfloat ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpfloat , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
return QVariant ( tmpfloat ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
}
case ENUM :
{
quint8 tmpenum ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpenum , & data [ offset + numBytesPerElement * index ] , numBytesPerElement ) ;
if ( tmpenum > = options . length ( ) ) {
2013-04-05 22:46:56 +02:00
qDebug ( ) < < " Invalid value for " < < name ;
2013-04-11 15:42:49 +02:00
tmpenum = 0 ;
2013-04-05 22:46:56 +02:00
}
2013-05-19 16:37:30 +02:00
return QVariant ( options [ tmpenum ] ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case BITFIELD :
{
quint8 tmpbitfield ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpbitfield , & data [ offset + numBytesPerElement * ( ( quint32 ) ( index / 8 ) ) ] , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
tmpbitfield = ( tmpbitfield > > ( index % 8 ) ) & 1 ;
2013-05-19 16:37:30 +02:00
return QVariant ( tmpbitfield ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case STRING :
{
data [ offset + numElements - 1 ] = ' \0 ' ;
2013-05-19 16:37:30 +02:00
QString str ( ( char * ) & data [ offset ] ) ;
return QVariant ( str ) ;
2013-04-05 22:46:56 +02:00
break ;
}
}
// If this point is reached then we got an invalid type
return QVariant ( ) ;
}
2013-05-19 16:37:30 +02:00
bool UAVObjectField : : checkValue ( const QVariant & value , quint32 index )
2013-04-05 22:46:56 +02:00
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Check that index is not out of bounds
2013-05-19 16:37:30 +02:00
if ( index > = numElements ) {
2013-04-05 22:46:56 +02:00
return false ;
}
// Get metadata
UAVObject : : Metadata mdata = obj - > getMetadata ( ) ;
// Update value if the access mode permits
2013-05-19 16:37:30 +02:00
if ( UAVObject : : GetFlightAccess ( mdata ) = = UAVObject : : ACCESS_READWRITE ) {
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
case INT16 :
case INT32 :
case UINT8 :
case UINT16 :
case UINT32 :
case FLOAT32 :
case STRING :
case BITFIELD :
return true ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
break ;
case ENUM :
{
2013-05-19 16:37:30 +02:00
qint8 tmpenum = options . indexOf ( value . toString ( ) ) ;
return ( tmpenum < 0 ) ? false : true ;
2013-04-05 22:46:56 +02:00
break ;
}
default :
qDebug ( ) < < " checkValue: other types " < < type ;
Q_ASSERT ( 0 ) ; // To catch any programming errors where we tried to test invalid values
break ;
}
}
return true ;
}
2013-05-19 16:37:30 +02:00
void UAVObjectField : : setValue ( const QVariant & value , quint32 index )
2013-04-05 22:46:56 +02:00
{
QMutexLocker locker ( obj - > getMutex ( ) ) ;
2013-05-19 16:37:30 +02:00
2013-04-05 22:46:56 +02:00
// Check that index is not out of bounds
2013-05-19 16:37:30 +02:00
if ( index > = numElements ) {
2013-04-05 22:46:56 +02:00
return ;
}
// Get metadata
UAVObject : : Metadata mdata = obj - > getMetadata ( ) ;
// Update value if the access mode permits
2013-05-19 16:37:30 +02:00
if ( UAVObject : : GetGcsAccess ( mdata ) = = UAVObject : : ACCESS_READWRITE ) {
switch ( type ) {
2013-04-05 22:46:56 +02:00
case INT8 :
{
qint8 tmpint8 = value . toInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpint8 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case INT16 :
{
qint16 tmpint16 = value . toInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpint16 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case INT32 :
{
qint32 tmpint32 = value . toInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpint32 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case UINT8 :
{
quint8 tmpuint8 = value . toUInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpuint8 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case UINT16 :
{
quint16 tmpuint16 = value . toUInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpuint16 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case UINT32 :
{
quint32 tmpuint32 = value . toUInt ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpuint32 , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case FLOAT32 :
{
float tmpfloat = value . toFloat ( ) ;
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpfloat , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case ENUM :
{
2013-05-19 16:37:30 +02:00
qint8 tmpenum = options . indexOf ( value . toString ( ) ) ;
2013-04-11 15:42:49 +02:00
// Default to 0 on invalid values.
2013-05-19 16:37:30 +02:00
if ( tmpenum < 0 ) {
2013-04-11 15:42:49 +02:00
tmpenum = 0 ;
}
2013-05-19 16:37:30 +02:00
memcpy ( & data [ offset + numBytesPerElement * index ] , & tmpenum , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case BITFIELD :
{
quint8 tmpbitfield ;
2013-05-19 16:37:30 +02:00
memcpy ( & tmpbitfield , & data [ offset + numBytesPerElement * ( ( quint32 ) ( index / 8 ) ) ] , numBytesPerElement ) ;
tmpbitfield = ( tmpbitfield & ~ ( 1 < < ( index % 8 ) ) ) | ( ( value . toUInt ( ) ! = 0 ? 1 : 0 ) < < ( index % 8 ) ) ;
memcpy ( & data [ offset + numBytesPerElement * ( ( quint32 ) ( index / 8 ) ) ] , & tmpbitfield , numBytesPerElement ) ;
2013-04-05 22:46:56 +02:00
break ;
}
case STRING :
{
QString str = value . toString ( ) ;
QByteArray barray = str . toAscii ( ) ;
quint32 index ;
2013-05-19 16:37:30 +02:00
for ( index = 0 ; index < ( quint32 ) barray . length ( ) & & index < ( numElements - 1 ) ; + + index ) {
data [ offset + index ] = barray [ index ] ;
2013-04-05 22:46:56 +02:00
}
barray [ index ] = ' \0 ' ;
break ;
}
}
}
}
double UAVObjectField : : getDouble ( quint32 index )
{
return getValue ( index ) . toDouble ( ) ;
}
void UAVObjectField : : setDouble ( double value , quint32 index )
{
setValue ( QVariant ( value ) , index ) ;
}