Changeset 15:1c0347ed41c6 in xplra


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

Implemented the scalar queries

Files:
3 edited

Legend:

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

    r14 r15  
    132132//------------------------------------------------------------------------------
    133133
    134 int XPlane::getInt(const char* name) throw(Exception)
     134void XPlane::getScalar(const char* name, uint8_t type) throw(Exception)
    135135{
    136136    checkStream();
     
    138138    stream->writeU8(Protocol::COMMAND_GET_SINGLE);
    139139    stream->writeString(name, strlen(name));
    140     stream->writeU8(Protocol::TYPE_INT);
     140    stream->writeU8(type);
    141141    if (!stream->flush()) checkStream();
    142142
     
    144144    checkStream();
    145145    checkResult(result);
     146}
     147
     148//------------------------------------------------------------------------------
     149
     150int XPlane::getInt(const char* name) throw(Exception)
     151{
     152    getScalar(name, Protocol::TYPE_INT);
    146153
    147154    int value = stream->readS32();
     155    checkStream();
     156    return value;
     157}
     158
     159//------------------------------------------------------------------------------
     160
     161float XPlane::getFloat(const char* name) throw(Exception)
     162{
     163    getScalar(name, Protocol::TYPE_FLOAT);
     164
     165    float value = stream->readFloat();
     166    checkStream();
     167    return value;
     168}
     169
     170//------------------------------------------------------------------------------
     171
     172double XPlane::getDouble(const char* name) throw(Exception)
     173{
     174    getScalar(name, Protocol::TYPE_DOUBLE);
     175
     176    double value = stream->readDouble();
    148177    checkStream();
    149178    return value;
  • src/client/c/hu/varadiistvan/xplra/XPlane.h

    r14 r15  
    108108    int getInt(const char* name) throw(Exception);
    109109
     110    /**
     111     * Get a float value.
     112     */
     113    float getFloat(const char* name) throw(Exception);
     114
     115    /**
     116     * Get a double value.
     117     */
     118    double getDouble(const char* name) throw(Exception);
     119
    110120private:
    111121    /**
     
    119129     */
    120130    void checkResult(uint8_t result) throw(ProtocolException);
     131
     132    /**
     133     * Issue the command to get a single, scalar value. The result is
     134     * also checked, but the value should be read by the caller.
     135     */
     136    void getScalar(const char* name, uint8_t type) throw(Exception);
    121137};
    122138
  • test/basictest.cc

    r14 r15  
    3737using hu::varadiistvan::xplra::XPlane;
    3838using hu::varadiistvan::xplra::Exception;
     39using hu::varadiistvan::xplra::ProtocolException;
    3940
    4041//------------------------------------------------------------------------------
     
    4748        printf("Connecting to X-Plane...\n");
    4849        xplane.connect();
    49         printf("Connected to X-Plane.\n");
     50        printf("Connected to X-Plane.\n\n");
    5051
    5152        printf("Querying the number of the engines...\n");
    5253        int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
    53         printf("The number of engines: %d\n", numEngines);
     54        printf("The number of engines: %d\n\n", numEngines);
    5455
    55         printf("Querying an invalid dataref...\n");
    56         xplane.getInt("sim/aircraft/engine/num_engines");
     56        try {
     57            printf("Querying an invalid dataref...\n");
     58            xplane.getInt("sim/aircraft/engine/num_engines");
     59            printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
     60        } catch(const ProtocolException& exception) {
     61            printf("Exception caugth: %s\n\n", exception.what());
     62        }
     63
     64        printf("Querying the spool time of a jet engine...\n");
     65        float spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet");
     66        printf("The spool time: %f\n\n", spoolTime);
     67
     68        printf("Querying the spool time of a propeller...\n");
     69        spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop");
     70        printf("The spool time: %f\n\n", spoolTime);
     71
     72        printf("Querying the coordinates...\n");
     73        double latitude = xplane.getDouble("sim/flightmodel/position/latitude");
     74        double longitude = xplane.getDouble("sim/flightmodel/position/longitude");
     75        printf("The coordinates: %f, %f\n\n", latitude, longitude);
    5776
    5877        return 0;
    5978    } catch(const Exception& exception) {
    60         printf("Exception caugth: %s\n", exception.what());
     79        printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
    6180
    6281        return 1;
Note: See TracChangeset for help on using the changeset viewer.