Changeset 18:c957b01ca44c in xplra


Ignore:
Timestamp:
01/31/13 18:44:47 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added the setting of array values

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/client/c/hu/varadiistvan/xplra/XPlane.cc

    r17 r18  
    331331//------------------------------------------------------------------------------
    332332
     333void XPlane::setArray(const char* name, uint8_t type, size_t length,
     334                      size_t offset) throw(Exception)
     335{
     336    stream->writeU8(Protocol::COMMAND_SET_SINGLE);
     337    stream->writeString(name, strlen(name));
     338    stream->writeU8(type);
     339    stream->writeS32(static_cast<int32_t>(length));
     340    stream->writeS32(static_cast<int32_t>(offset));
     341}
     342
     343//------------------------------------------------------------------------------
     344
     345void XPlane::setFloatArray(const char* name, const float* values, size_t length,
     346                           size_t offset) throw(Exception)
     347{
     348    setArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
     349    stream->write(values, length*sizeof(*values));
     350    stream->flush();
     351    checkResult();
     352}
     353
     354//------------------------------------------------------------------------------
     355
     356void XPlane::setIntArray(const char* name, const int32_t* values, size_t length,
     357                         size_t offset) throw(Exception)
     358{
     359    setArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
     360    stream->write(values, length*sizeof(*values));
     361    stream->flush();
     362    checkResult();
     363}
     364
     365//------------------------------------------------------------------------------
     366
     367void XPlane::setByteArray(const char* name, const uint8_t* values, size_t length,
     368                          size_t offset) throw(Exception)
     369{
     370    setArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
     371    stream->write(values, length*sizeof(*values));
     372    stream->flush();
     373    checkResult();
     374}
     375
     376//------------------------------------------------------------------------------
     377
     378void XPlane::setString(const char* name, const char* value, size_t length,
     379                       size_t offset) throw(Exception)
     380{
     381    size_t valueLength = strlen(value);
     382    if ((valueLength+1)>=length) {
     383        setByteArray(name, reinterpret_cast<const uint8_t*>(value),
     384                     length, offset);
     385    } else {
     386        auto_ptr<uint8_t> buffer(new uint8_t[length]);
     387        memcpy(buffer.get(), value, valueLength);
     388        memset(buffer.get() + valueLength, 0, length - valueLength);
     389        setByteArray(name, buffer.get(), length, offset);
     390    }
     391}
     392//------------------------------------------------------------------------------
     393
    333394// Local Variables:
    334395// mode: C++
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r17 r18  
    198198    void setDouble(const char* name, double value) throw(Exception);
    199199
     200    /**
     201     * Set the given float array to values in the given buffer.
     202     */
     203    void setFloatArray(const char* name, const float* values, size_t length,
     204                       size_t offset = 0) throw(Exception);
     205
     206    /**
     207     * Set the given integer array to values in the given buffer.
     208     */
     209    void setIntArray(const char* name, const int32_t* values, size_t length,
     210                     size_t offset = 0) throw(Exception);
     211
     212    /**
     213     * Set the given byte array to values in the given buffer.
     214     */
     215    void setByteArray(const char* name, const uint8_t* values, size_t length,
     216                      size_t offset = 0) throw(Exception);
     217
     218    /**
     219     * Set the given string to the given value. Since strings are byte
     220     * arrays, and the bytes after the string should be zeroed, the
     221     * length must be given. The string will be converted to a byte
     222     * array of the given length, padded with 0s.
     223     */
     224    void setString(const char* name, const char* value, size_t length,
     225                   size_t offset = 0) throw(Exception);
     226
    200227private:
    201228    /**
     
    237264     */
    238265    void setScalar(const char* name, uint8_t type) throw(Exception);
     266
     267    /**
     268     * Issue the command to set an array value of the given type.
     269     */
     270    void setArray(const char* name, uint8_t type, size_t length,
     271                  size_t offset) throw(Exception);
    239272};
    240273
  • test/basictest.cc

    r17 r18  
    3232
    3333#include <cstdio>
     34#include <cstring>
    3435
    3536//------------------------------------------------------------------------------
     
    145146        printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
    146147
    147         double acfElevDown = xplane.getDouble("sim/flightmodel/position/local_x");
    148         printf("Setting the aircraft elevator down control from %f to %f...\n",
    149                acfElevDown, acfElevDown + 15.0);
    150         xplane.setDouble("sim/flightmodel/position/local_x", acfElevDown + 15.0);
    151         acfElevDown = xplane.getDouble("sim/flightmodel/position/local_x");
    152         printf("The aircraft elevator down control set to %f\n\n", acfElevDown);
     148        double localX = xplane.getDouble("sim/flightmodel/position/local_x");
     149        printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
     150               localX, localX + 15.0);
     151        xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0);
     152        localX = xplane.getDouble("sim/flightmodel/position/local_x");
     153        printf("The aircraft's local X-coordinate set to %f\n\n", localX);
     154
     155        float numBlades[8];
     156        length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
     157                                      numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
     158
     159        printf("Setting the number of blades\n    from:");
     160        for(size_t i = 0; i<length; ++i) {
     161            if (i>0) printf(",");
     162            printf(" %f", numBlades[i]);
     163        }
     164        printf("\n    to:");
     165        for(size_t i = 0; i<length; ++i) {
     166            numBlades[i] += 2.5;
     167            if (i>0) printf(",");
     168            printf(" %f", numBlades[i]);
     169        }
     170        printf("\n");
     171        xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",
     172                             numBlades, length);
     173        length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
     174                                      numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
     175        printf("The result:");
     176        for(size_t i = 0; i<length; ++i) {
     177            if (i>0) printf(",");
     178            printf(" %f", numBlades[i]);
     179        }
     180        printf("\n\n");
     181
     182        int32_t batteryArrayOn[8];
     183        length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
     184                                    batteryArrayOn,
     185                                    sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
     186
     187        printf("Setting the batteries\n    from:");
     188        for(size_t i = 0; i<length; ++i) {
     189            if (i>0) printf(",");
     190            printf(" %d", batteryArrayOn[i]);
     191        }
     192        printf("\n    to:");
     193        for(size_t i = 0; i<length; ++i) {
     194            batteryArrayOn[i] = !batteryArrayOn[i];
     195            numBlades[i] += 2.5;
     196            if (i>0) printf(",");
     197            printf(" %d", batteryArrayOn[i]);
     198        }
     199        printf("\n");
     200        xplane.setIntArray("sim/cockpit/electrical/battery_array_on",
     201                           batteryArrayOn, length);
     202        length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
     203                                    batteryArrayOn,
     204                                    sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
     205        printf("The result:");
     206        for(size_t i = 0; i<length; ++i) {
     207            if (i>0) printf(",");
     208            printf(" %d", batteryArrayOn[i]);
     209        }
     210        printf("\n\n");
     211
     212        uint8_t tailNum[40];
     213        memset(tailNum, 0, sizeof(tailNum));
     214        strcpy(reinterpret_cast<char*>(tailNum), "HA-VAI");
     215        printf("Setting the tail number to %s as a byte array...\n", tailNum);
     216        xplane.setByteArray("sim/aircraft/view/acf_tailnum",
     217                            tailNum, sizeof(tailNum));
     218        printf("The tail number is: '%s'\n\n",
     219               xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
     220
     221        static const char* tailNum1 = "VAI";
     222        printf("Setting the tail number to %s as a string...\n", tailNum1);
     223        xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40);
     224        printf("The tail number is: '%s'\n\n",
     225               xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
    153226
    154227        try {
Note: See TracChangeset for help on using the changeset viewer.