[14] | 1 | // Copyright (c) 2013 by István Váradi
|
---|
| 2 |
|
---|
| 3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
| 4 |
|
---|
| 5 | // Redistribution and use in source and binary forms, with or without
|
---|
| 6 | // modification, are permitted provided that the following conditions are met:
|
---|
| 7 |
|
---|
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
| 9 | // list of conditions and the following disclaimer.
|
---|
| 10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 11 | // this list of conditions and the following disclaimer in the documentation
|
---|
| 12 | // and/or other materials provided with the distribution.
|
---|
| 13 |
|
---|
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
| 15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
| 16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
| 18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 24 |
|
---|
| 25 | // The views and conclusions contained in the software and documentation are those
|
---|
| 26 | // of the authors and should not be interpreted as representing official policies,
|
---|
| 27 | // either expressed or implied, of the FreeBSD Project.
|
---|
| 28 |
|
---|
| 29 | //------------------------------------------------------------------------------
|
---|
| 30 |
|
---|
| 31 | #include <hu/varadiistvan/xplra/XPlane.h>
|
---|
| 32 |
|
---|
[40] | 33 | #include <hu/varadiistvan/scpl/Thread.h>
|
---|
| 34 |
|
---|
[14] | 35 | #include <cstdio>
|
---|
[18] | 36 | #include <cstring>
|
---|
[14] | 37 |
|
---|
| 38 | //------------------------------------------------------------------------------
|
---|
| 39 |
|
---|
| 40 | using hu::varadiistvan::xplra::XPlane;
|
---|
| 41 | using hu::varadiistvan::xplra::Exception;
|
---|
[15] | 42 | using hu::varadiistvan::xplra::ProtocolException;
|
---|
[14] | 43 |
|
---|
[40] | 44 | using hu::varadiistvan::scpl::Thread;
|
---|
| 45 |
|
---|
[16] | 46 | using std::string;
|
---|
| 47 |
|
---|
[14] | 48 | //------------------------------------------------------------------------------
|
---|
| 49 |
|
---|
| 50 | int main()
|
---|
| 51 | {
|
---|
| 52 | try {
|
---|
| 53 | XPlane xplane;
|
---|
| 54 |
|
---|
| 55 | printf("Connecting to X-Plane...\n");
|
---|
| 56 | xplane.connect();
|
---|
[15] | 57 | printf("Connected to X-Plane.\n\n");
|
---|
[14] | 58 |
|
---|
[36] | 59 | int xplaneVersion = 0;
|
---|
| 60 | int xplmVersion = 0;
|
---|
| 61 | int xplraVersion = 0;
|
---|
| 62 |
|
---|
[40] | 63 | printf("Showing a message...\n");
|
---|
| 64 | xplane.showMessage("[basictest] Starting tests", 5.0);
|
---|
| 65 | printf("\n");
|
---|
| 66 |
|
---|
[36] | 67 | printf("Querying the versions...\n");
|
---|
| 68 | xplane.getVersions(xplaneVersion, xplmVersion, xplraVersion);
|
---|
| 69 | printf("X-Plane version: %d, XPLM: %d, XPLRA: %03d\n\n",
|
---|
| 70 | xplaneVersion, xplmVersion, xplraVersion);
|
---|
| 71 |
|
---|
[14] | 72 | printf("Querying the number of the engines...\n");
|
---|
| 73 | int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
|
---|
[15] | 74 | printf("The number of engines: %d\n\n", numEngines);
|
---|
| 75 |
|
---|
| 76 | try {
|
---|
| 77 | printf("Querying an invalid dataref...\n");
|
---|
| 78 | xplane.getInt("sim/aircraft/engine/num_engines");
|
---|
| 79 | printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
|
---|
| 80 | } catch(const ProtocolException& exception) {
|
---|
| 81 | printf("Exception caugth: %s\n\n", exception.what());
|
---|
| 82 | }
|
---|
[14] | 83 |
|
---|
[15] | 84 | printf("Querying the spool time of a jet engine...\n");
|
---|
| 85 | float spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet");
|
---|
| 86 | printf("The spool time: %f\n\n", spoolTime);
|
---|
| 87 |
|
---|
| 88 | printf("Querying the spool time of a propeller...\n");
|
---|
| 89 | spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop");
|
---|
| 90 | printf("The spool time: %f\n\n", spoolTime);
|
---|
| 91 |
|
---|
| 92 | printf("Querying the coordinates...\n");
|
---|
| 93 | double latitude = xplane.getDouble("sim/flightmodel/position/latitude");
|
---|
| 94 | double longitude = xplane.getDouble("sim/flightmodel/position/longitude");
|
---|
| 95 | printf("The coordinates: %f, %f\n\n", latitude, longitude);
|
---|
[14] | 96 |
|
---|
[16] | 97 | printf("Querying the aircraft's description as an array of a fixed size...\n");
|
---|
| 98 | uint8_t result[260];
|
---|
| 99 | size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip",
|
---|
| 100 | result, sizeof(result)/sizeof(result[0]));
|
---|
[60] | 101 | printf("Got %lu bytes, as a string: '%s'\n\n",
|
---|
| 102 | static_cast<unsigned long>(length), result);
|
---|
[16] | 103 |
|
---|
| 104 | printf("Querying the aircraft's description as a dynamically created array...\n");
|
---|
| 105 | uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
|
---|
[60] | 106 | printf("Got %lu bytes, as a string: '%s'\n\n",
|
---|
| 107 | static_cast<unsigned long>(length), result1);
|
---|
[16] | 108 |
|
---|
| 109 | printf("Querying the aircraft's description as a string, with an offset of 3...\n");
|
---|
| 110 | string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
|
---|
| 111 | printf("Got: '%s'\n\n", result2.c_str());
|
---|
| 112 |
|
---|
| 113 | printf("Querying the aircraft's engine types as an array of a fixed size...\n");
|
---|
| 114 | int32_t result3[8];
|
---|
| 115 | length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
|
---|
| 116 | result3, sizeof(result3)/sizeof(result3[0]));
|
---|
[60] | 117 | printf("Got %lu values:", static_cast<unsigned long>(length));
|
---|
[16] | 118 | for(size_t i = 0; i<length; ++i) {
|
---|
| 119 | if (i>0) printf(",");
|
---|
| 120 | printf(" %d", result3[i]);
|
---|
| 121 | }
|
---|
| 122 | printf("\n\n");
|
---|
| 123 |
|
---|
| 124 | printf("Querying the aircraft's engine types as a dynamically created array...\n");
|
---|
| 125 | length = 0;
|
---|
| 126 | int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
|
---|
[60] | 127 | printf("Got %lu values:", static_cast<unsigned long>(length));
|
---|
[16] | 128 | for(size_t i = 0; i<length; ++i) {
|
---|
| 129 | if (i>0) printf(",");
|
---|
| 130 | printf(" %d", result4[i]);
|
---|
| 131 | }
|
---|
| 132 | printf("\n\n");
|
---|
| 133 |
|
---|
| 134 | printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
|
---|
| 135 | float result5[8];
|
---|
| 136 | length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
|
---|
| 137 | result5, sizeof(result5)/sizeof(result5[0]));
|
---|
[60] | 138 | printf("Got %lu values:", static_cast<unsigned long>(length));
|
---|
[16] | 139 | for(size_t i = 0; i<length; ++i) {
|
---|
| 140 | if (i>0) printf(",");
|
---|
| 141 | printf(" %f", result5[i]);
|
---|
| 142 | }
|
---|
| 143 | printf("\n\n");
|
---|
| 144 |
|
---|
| 145 | printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
|
---|
| 146 | length = 0;
|
---|
| 147 | float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
|
---|
[60] | 148 | printf("Got %lu values:", static_cast<unsigned long>(length));
|
---|
[16] | 149 | for(size_t i = 0; i<length; ++i) {
|
---|
| 150 | if (i>0) printf(",");
|
---|
| 151 | printf(" %f", result6[i]);
|
---|
| 152 | }
|
---|
| 153 | printf("\n\n");
|
---|
| 154 |
|
---|
[17] | 155 | printf("Setting the number of the engines to %d...\n", numEngines + 1);
|
---|
| 156 | xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1);
|
---|
| 157 | numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
|
---|
| 158 | printf("The new number of engines: %d\n\n", numEngines);
|
---|
| 159 |
|
---|
| 160 | float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
|
---|
| 161 | printf("Setting the aircraft elevator up control from %f to %f...\n",
|
---|
| 162 | acfElevUp, acfElevUp + 15.0);
|
---|
[25] | 163 | xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0);
|
---|
[17] | 164 | acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
|
---|
| 165 | printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
|
---|
| 166 |
|
---|
[18] | 167 | double localX = xplane.getDouble("sim/flightmodel/position/local_x");
|
---|
| 168 | printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
|
---|
| 169 | localX, localX + 15.0);
|
---|
| 170 | xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0);
|
---|
| 171 | localX = xplane.getDouble("sim/flightmodel/position/local_x");
|
---|
| 172 | printf("The aircraft's local X-coordinate set to %f\n\n", localX);
|
---|
| 173 |
|
---|
| 174 | float numBlades[8];
|
---|
| 175 | length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
|
---|
| 176 | numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
|
---|
| 177 |
|
---|
| 178 | printf("Setting the number of blades\n from:");
|
---|
| 179 | for(size_t i = 0; i<length; ++i) {
|
---|
| 180 | if (i>0) printf(",");
|
---|
| 181 | printf(" %f", numBlades[i]);
|
---|
| 182 | }
|
---|
| 183 | printf("\n to:");
|
---|
| 184 | for(size_t i = 0; i<length; ++i) {
|
---|
| 185 | numBlades[i] += 2.5;
|
---|
| 186 | if (i>0) printf(",");
|
---|
| 187 | printf(" %f", numBlades[i]);
|
---|
| 188 | }
|
---|
| 189 | printf("\n");
|
---|
| 190 | xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",
|
---|
| 191 | numBlades, length);
|
---|
| 192 | length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
|
---|
| 193 | numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
|
---|
| 194 | printf("The result:");
|
---|
| 195 | for(size_t i = 0; i<length; ++i) {
|
---|
| 196 | if (i>0) printf(",");
|
---|
| 197 | printf(" %f", numBlades[i]);
|
---|
| 198 | }
|
---|
| 199 | printf("\n\n");
|
---|
| 200 |
|
---|
| 201 | int32_t batteryArrayOn[8];
|
---|
| 202 | length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
|
---|
| 203 | batteryArrayOn,
|
---|
| 204 | sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
|
---|
| 205 |
|
---|
| 206 | printf("Setting the batteries\n from:");
|
---|
| 207 | for(size_t i = 0; i<length; ++i) {
|
---|
| 208 | if (i>0) printf(",");
|
---|
| 209 | printf(" %d", batteryArrayOn[i]);
|
---|
| 210 | }
|
---|
| 211 | printf("\n to:");
|
---|
| 212 | for(size_t i = 0; i<length; ++i) {
|
---|
| 213 | batteryArrayOn[i] = !batteryArrayOn[i];
|
---|
| 214 | numBlades[i] += 2.5;
|
---|
| 215 | if (i>0) printf(",");
|
---|
| 216 | printf(" %d", batteryArrayOn[i]);
|
---|
| 217 | }
|
---|
| 218 | printf("\n");
|
---|
| 219 | xplane.setIntArray("sim/cockpit/electrical/battery_array_on",
|
---|
| 220 | batteryArrayOn, length);
|
---|
| 221 | length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
|
---|
| 222 | batteryArrayOn,
|
---|
| 223 | sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
|
---|
| 224 | printf("The result:");
|
---|
| 225 | for(size_t i = 0; i<length; ++i) {
|
---|
| 226 | if (i>0) printf(",");
|
---|
| 227 | printf(" %d", batteryArrayOn[i]);
|
---|
| 228 | }
|
---|
| 229 | printf("\n\n");
|
---|
| 230 |
|
---|
| 231 | uint8_t tailNum[40];
|
---|
| 232 | memset(tailNum, 0, sizeof(tailNum));
|
---|
| 233 | strcpy(reinterpret_cast<char*>(tailNum), "HA-VAI");
|
---|
| 234 | printf("Setting the tail number to %s as a byte array...\n", tailNum);
|
---|
| 235 | xplane.setByteArray("sim/aircraft/view/acf_tailnum",
|
---|
| 236 | tailNum, sizeof(tailNum));
|
---|
| 237 | printf("The tail number is: '%s'\n\n",
|
---|
| 238 | xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
|
---|
| 239 |
|
---|
| 240 | static const char* tailNum1 = "VAI";
|
---|
| 241 | printf("Setting the tail number to %s as a string...\n", tailNum1);
|
---|
| 242 | xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40);
|
---|
| 243 | printf("The tail number is: '%s'\n\n",
|
---|
| 244 | xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
|
---|
[17] | 245 |
|
---|
| 246 | try {
|
---|
| 247 | printf("Querying an invalid dataref...\n");
|
---|
| 248 | xplane.getInt("sim/aircraft/engine/num_engines");
|
---|
| 249 | printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
|
---|
| 250 | } catch(const ProtocolException& exception) {
|
---|
| 251 | printf("Exception caugth: %s\n\n", exception.what());
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[40] | 254 | printf("Preparing for the message tests, sleeping for 5 seconds...\n");
|
---|
| 255 | Thread::sleep(5*1000);
|
---|
| 256 |
|
---|
| 257 | printf("Showing a message for 10 seconds...\n");
|
---|
| 258 | xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0);
|
---|
| 259 |
|
---|
| 260 | printf("Sleeping for 3 seconds...\n");
|
---|
| 261 | Thread::sleep(3*1000);
|
---|
| 262 |
|
---|
| 263 | printf("Showing another message interrupting the previous one for 3 seconds\n");
|
---|
| 264 | xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0);
|
---|
| 265 |
|
---|
| 266 | printf("Sleeping for 5 seconds...\n");
|
---|
| 267 | Thread::sleep(5*1000);
|
---|
| 268 | xplane.showMessage("[basictest] and the tests come to an end!", 5.0);
|
---|
[17] | 269 |
|
---|
[14] | 270 | return 0;
|
---|
| 271 | } catch(const Exception& exception) {
|
---|
[15] | 272 | printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
|
---|
[14] | 273 |
|
---|
| 274 | return 1;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | //------------------------------------------------------------------------------
|
---|
| 279 |
|
---|
| 280 | // Local Variables:
|
---|
| 281 | // mode: C++
|
---|
| 282 | // c-basic-offset: 4
|
---|
| 283 | // indent-tabs-mode: nil
|
---|
| 284 | // End:
|
---|