Changeset 17:2b7d9b08ce3f in xplra


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

Setting of scalar values works

Files:
3 edited

Legend:

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

    r16 r17  
    9090
    9191//------------------------------------------------------------------------------
     92
     93inline void XPlane::checkResult() throw(ProtocolException)
     94{
     95    uint8_t result = stream->readU8();
     96    checkStream();
     97    checkResult(result);
     98}
     99
     100//------------------------------------------------------------------------------
    92101//------------------------------------------------------------------------------
    93102
     
    142151    if (!stream->flush()) checkStream();
    143152
    144     uint8_t result = stream->readU8();
    145     checkStream();
    146     checkResult(result);
     153    checkResult();
    147154}
    148155
     
    285292//------------------------------------------------------------------------------
    286293
     294void XPlane::setScalar(const char* name, uint8_t type) throw(Exception)
     295{
     296    stream->writeU8(Protocol::COMMAND_SET_SINGLE);
     297    stream->writeString(name, strlen(name));
     298    stream->writeU8(type);
     299}
     300
     301//------------------------------------------------------------------------------
     302
     303void XPlane::setInt(const char* name, int value) throw(Exception)
     304{
     305    setScalar(name, Protocol::TYPE_INT);
     306    stream->writeS32(value);
     307    stream->flush();
     308    checkResult();
     309}
     310
     311//------------------------------------------------------------------------------
     312
     313void XPlane::setFloat(const char* name, float value) throw(Exception)
     314{
     315    setScalar(name, Protocol::TYPE_FLOAT);
     316    stream->writeFloat(value);
     317    stream->flush();
     318    checkResult();
     319}
     320
     321//------------------------------------------------------------------------------
     322
     323void XPlane::setDouble(const char* name, double value) throw(Exception)
     324{
     325    setScalar(name, Protocol::TYPE_DOUBLE);
     326    stream->writeDouble(value);
     327    stream->flush();
     328    checkResult();
     329}
     330
     331//------------------------------------------------------------------------------
     332
    287333// Local Variables:
    288334// mode: C++
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r16 r17  
    183183    std::string getString(const char* name, size_t offset = 0) throw(Exception);
    184184
     185    /**
     186     * Set the given dataref to the given integer value.
     187     */
     188    void setInt(const char* name, int value) throw(Exception);
     189
     190    /**
     191     * Set the given dataref to the given float value.
     192     */
     193    void setFloat(const char* name, float value) throw(Exception);
     194
     195    /**
     196     * Set the given dataref to the given double value.
     197     */
     198    void setDouble(const char* name, double value) throw(Exception);
     199
    185200private:
    186201    /**
     
    194209     */
    195210    void checkResult(uint8_t result) throw(ProtocolException);
     211
     212    /**
     213     * Read and check the result.  If it signifies an error,
     214     * throw a ProtocolException with the correct error code.
     215     */
     216    void checkResult() throw(ProtocolException);
    196217
    197218    /**
     
    211232                    ssize_t length, size_t offset) throw(Exception);
    212233
     234
     235    /**
     236     * Issue the command to set a scalar value of the given type.
     237     */
     238    void setScalar(const char* name, uint8_t type) throw(Exception);
    213239};
    214240
  • test/basictest.cc

    r16 r17  
    133133        printf("\n\n");
    134134
     135        printf("Setting the number of the engines to %d...\n", numEngines + 1);
     136        xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1);
     137        numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
     138        printf("The new number of engines: %d\n\n", numEngines);
     139
     140        float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
     141        printf("Setting the aircraft elevator up control from %f to %f...\n",
     142               acfElevUp, acfElevUp + 15.0);
     143        xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp);
     144        acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
     145        printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
     146
     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);
     153
     154        try {
     155            printf("Querying an invalid dataref...\n");
     156            xplane.getInt("sim/aircraft/engine/num_engines");
     157            printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
     158        } catch(const ProtocolException& exception) {
     159            printf("Exception caugth: %s\n\n", exception.what());
     160        }
     161
     162
    135163        return 0;
    136164    } catch(const Exception& exception) {
Note: See TracChangeset for help on using the changeset viewer.