// Copyright (c) 2013 by István Váradi // This file is part of XPLRA, a remote-access plugin for X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include #include #include #include //------------------------------------------------------------------------------ using hu::varadiistvan::xplra::XPlane; using hu::varadiistvan::xplra::Exception; using hu::varadiistvan::xplra::ProtocolException; using hu::varadiistvan::scpl::Thread; using std::string; //------------------------------------------------------------------------------ int main() { try { XPlane xplane; printf("Connecting to X-Plane...\n"); xplane.connect(); printf("Connected to X-Plane.\n\n"); int xplaneVersion = 0; int xplmVersion = 0; int xplraVersion = 0; printf("Showing a message...\n"); xplane.showMessage("[basictest] Starting tests", 5.0); printf("\n"); printf("Querying the versions...\n"); xplane.getVersions(xplaneVersion, xplmVersion, xplraVersion); printf("X-Plane version: %d, XPLM: %d, XPLRA: %03d\n\n", xplaneVersion, xplmVersion, xplraVersion); printf("Querying the number of the engines...\n"); int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines"); printf("The number of engines: %d\n\n", numEngines); try { printf("Querying an invalid dataref...\n"); xplane.getInt("sim/aircraft/engine/num_engines"); printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n"); } catch(const ProtocolException& exception) { printf("Exception caugth: %s\n\n", exception.what()); } printf("Querying the spool time of a jet engine...\n"); float spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet"); printf("The spool time: %f\n\n", spoolTime); printf("Querying the spool time of a propeller...\n"); spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop"); printf("The spool time: %f\n\n", spoolTime); printf("Querying the coordinates...\n"); double latitude = xplane.getDouble("sim/flightmodel/position/latitude"); double longitude = xplane.getDouble("sim/flightmodel/position/longitude"); printf("The coordinates: %f, %f\n\n", latitude, longitude); printf("Querying the aircraft's description as an array of a fixed size...\n"); uint8_t result[260]; size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip", result, sizeof(result)/sizeof(result[0])); printf("Got %zu bytes, as a string: '%s'\n\n", length, result); printf("Querying the aircraft's description as a dynamically created array...\n"); uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length); printf("Got %zu bytes, as a string: '%s'\n\n", length, result1); printf("Querying the aircraft's description as a string, with an offset of 3...\n"); string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3); printf("Got: '%s'\n\n", result2.c_str()); printf("Querying the aircraft's engine types as an array of a fixed size...\n"); int32_t result3[8]; length = xplane.getIntArray("sim/aircraft/prop/acf_en_type", result3, sizeof(result3)/sizeof(result3[0])); printf("Got %zu values:", length); for(size_t i = 0; i0) printf(","); printf(" %d", result3[i]); } printf("\n\n"); printf("Querying the aircraft's engine types as a dynamically created array...\n"); length = 0; int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length); printf("Got %zu values:", length); for(size_t i = 0; i0) printf(","); printf(" %d", result4[i]); } printf("\n\n"); printf("Querying the aircraft's propeller direction as an array of a fixed size...\n"); float result5[8]; length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", result5, sizeof(result5)/sizeof(result5[0])); printf("Got %zu values:", length); for(size_t i = 0; i0) printf(","); printf(" %f", result5[i]); } printf("\n\n"); printf("Querying the aircraft's propeller direction as a dynamically created array...\n"); length = 0; float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length); printf("Got %zu values:", length); for(size_t i = 0; i0) printf(","); printf(" %f", result6[i]); } printf("\n\n"); printf("Setting the number of the engines to %d...\n", numEngines + 1); xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1); numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines"); printf("The new number of engines: %d\n\n", numEngines); float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up"); printf("Setting the aircraft elevator up control from %f to %f...\n", acfElevUp, acfElevUp + 15.0); xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0); acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up"); printf("The aircraft elevator up control set to %f\n\n", acfElevUp); double localX = xplane.getDouble("sim/flightmodel/position/local_x"); printf("Setting the aircraft's local X-coordinate from %f to %f...\n", localX, localX + 15.0); xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0); localX = xplane.getDouble("sim/flightmodel/position/local_x"); printf("The aircraft's local X-coordinate set to %f\n\n", localX); float numBlades[8]; length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades", numBlades, sizeof(numBlades)/sizeof(numBlades[0])); printf("Setting the number of blades\n from:"); for(size_t i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n to:"); for(size_t i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n"); xplane.setFloatArray("sim/aircraft/prop/acf_num_blades", numBlades, length); length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades", numBlades, sizeof(numBlades)/sizeof(numBlades[0])); printf("The result:"); for(size_t i = 0; i0) printf(","); printf(" %f", numBlades[i]); } printf("\n\n"); int32_t batteryArrayOn[8]; length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn, sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0])); printf("Setting the batteries\n from:"); for(size_t i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n to:"); for(size_t i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n"); xplane.setIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn, length); length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on", batteryArrayOn, sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0])); printf("The result:"); for(size_t i = 0; i0) printf(","); printf(" %d", batteryArrayOn[i]); } printf("\n\n"); uint8_t tailNum[40]; memset(tailNum, 0, sizeof(tailNum)); strcpy(reinterpret_cast(tailNum), "HA-VAI"); printf("Setting the tail number to %s as a byte array...\n", tailNum); xplane.setByteArray("sim/aircraft/view/acf_tailnum", tailNum, sizeof(tailNum)); printf("The tail number is: '%s'\n\n", xplane.getString("sim/aircraft/view/acf_tailnum").c_str()); static const char* tailNum1 = "VAI"; printf("Setting the tail number to %s as a string...\n", tailNum1); xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40); printf("The tail number is: '%s'\n\n", xplane.getString("sim/aircraft/view/acf_tailnum").c_str()); try { printf("Querying an invalid dataref...\n"); xplane.getInt("sim/aircraft/engine/num_engines"); printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n"); } catch(const ProtocolException& exception) { printf("Exception caugth: %s\n\n", exception.what()); } printf("Preparing for the message tests, sleeping for 5 seconds...\n"); Thread::sleep(5*1000); printf("Showing a message for 10 seconds...\n"); xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0); printf("Sleeping for 3 seconds...\n"); Thread::sleep(3*1000); printf("Showing another message interrupting the previous one for 3 seconds\n"); xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0); printf("Sleeping for 5 seconds...\n"); Thread::sleep(5*1000); xplane.showMessage("[basictest] and the tests come to an end!", 5.0); return 0; } catch(const Exception& exception) { printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what()); return 1; } } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: