Skip to content

Commit

Permalink
Allow setting byte array (data) datarefs. Not tested yet. Closes #38
Browse files Browse the repository at this point in the history
…if works.
  • Loading branch information
vranki committed Dec 25, 2018
1 parent bbe992b commit 6692bb3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ uia sim/cockpit2/engine/indicators/N1_percent [99,97]

### Data Datarefs ###

Data datarefs output data in base64:
Data datarefs input and output data in base64:
```
ub sim/aircraft/view/acf_descrip RXh0UGxhbmUgU2ltdWxhdGVkIENvbm5lY3Rpb24=
```
Expand All @@ -325,6 +325,12 @@ sub sim/aircraft/view/acf_descrip:string
->
ub sim/aircraft/view/acf_descrip:string "Boeing 737-800"
```
Setting can be done as base64 or string:
```
set sim/aircraft/view/acf_descrip:string "Boeing 737-800"
or
set sim/aircraft/view/acf_descrip RXh0UGxhbmUgU2ltdWxhdGVkIENvbm5lY3Rpb24=
```


### Console Output ###
Expand Down
6 changes: 6 additions & 0 deletions extplane-plugin/xplaneplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ void XPlanePlugin::changeDataRef(DataRef *ref)
XPLMSetDatad(ref->ref(), qobject_cast<DoubleDataRef*>(ref)->value());
break;
}
case extplaneRefTypeData:
{
DataDataRef *bRef = qobject_cast<DataDataRef*>(ref);
XPLMSetDatab(bRef->ref(), bRef->value().data(), 0, bRef->value().length());
break;
}
default:
break;
}
Expand Down
34 changes: 9 additions & 25 deletions extplane-server/datarefs/datadataref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ DataDataRef::DataDataRef(QObject *parent, QString &name, void *ref) : DataRef(pa
_typeString = "b";
_type = extplaneRefTypeData;
_lastUpdate.restart();
_isString = modifiers().contains("string");
}

QByteArray &DataDataRef::value() {
return _value;
}

QByteArray &DataDataRef::newValue()
{
QByteArray &DataDataRef::newValue() {
return _newValue;
}

void DataDataRef::updateValue() {
// Make sure it's time to update again based on the accuracy
if(this->accuracy() == 0 || _lastUpdate.elapsed() > this->accuracy()) {
if(this->accuracy() >= 0 || _lastUpdate.elapsed() > this->accuracy()) {
// TODO: do we really want to make this comparison for large data datarefs? Probably as it's still cheaper than sending over the wire the new data
if (_newValue != _value) {
_value = _newValue;
Expand All @@ -30,42 +30,26 @@ void DataDataRef::updateValue() {
}

void DataDataRef::setValue(QByteArray &newValue) {
Q_UNUSED(newValue);
//TODO: @dankrusi: finish this implementation and test
qFatal("Writing of Data DataRefs is not yet supported");
/*
// Limit number of values to write to ref length or number of given values
int numberOfValuesToWrite = qMin(_length, values.size());
// Convert values to float and copy to _valueArray
for(int i=0;i<numberOfValuesToWrite;i++) {
bool ok = true;
float value = values[i].toFloat(&ok);
if(!ok) {
qDebug() << Q_FUNC_INFO << "Invalid value " << values[i] << "in array";
return;
}
_valueArray[i]=value;
if(newValue != _value) {
_newValue = newValue;
updateValue();
}
XPLMSetDatavf(_ref, _valueArray, 0, numberOfValuesToWrite);
*/
}

QString DataDataRef::valueString() {
if(modifiers().contains("string")) {
if(_isString) {
return QString("\"%1\"").arg(QString(_value));
} else {
return QString(_value).toUtf8().toBase64();
}
}

void DataDataRef::setValue(QString &newValue) {
QByteArray valueBA = newValue.toUtf8();
QByteArray valueBA = _isString ? newValue.toUtf8() : QByteArray::fromBase64(newValue.toUtf8());
setValue(valueBA);
}

void DataDataRef::setLength(int newLength)
{
void DataDataRef::setLength(int newLength) {
Q_ASSERT(newLength > 0);
if(_value.length() != newLength) {
_value = QByteArray(newLength, 0);
Expand Down
1 change: 1 addition & 0 deletions extplane-server/datarefs/datadataref.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DataDataRef : public DataRef {
QByteArray _value; // Value of dataref is stored here once retrieved from XPLM
QByteArray _newValue; // Temp variable used while checking changes. Here to avoid resizing.
QTime _lastUpdate; // Timer used for tracking the last update time
bool _isString; // True, if string modifier is used.
};

#endif // DATADATAREF_H

0 comments on commit 6692bb3

Please sign in to comment.