source: xplra/test/basictest.cc@ 93:4afb413401e3

Last change on this file since 93:4afb413401e3 was 89:69ba1a2b5897, checked in by István Váradi <ivaradi@…>, 12 years ago

Added calls to save the current situation

File size: 12.2 KB
Line 
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
33#include <hu/varadiistvan/scpl/Thread.h>
34
35#include <cstdio>
36#include <cstring>
37
38//------------------------------------------------------------------------------
39
40using hu::varadiistvan::xplra::XPlane;
41using hu::varadiistvan::xplra::Exception;
42using hu::varadiistvan::xplra::ProtocolException;
43
44using hu::varadiistvan::scpl::Thread;
45
46using std::string;
47
48//------------------------------------------------------------------------------
49
50int main()
51{
52 try {
53 XPlane xplane;
54
55 printf("Connecting to X-Plane...\n");
56 xplane.connect();
57 printf("Connected to X-Plane.\n\n");
58
59 int xplaneVersion = 0;
60 int xplmVersion = 0;
61 int xplraVersion = 0;
62
63 printf("Showing a message...\n");
64 xplane.showMessage("[basictest] Starting tests", 5.0);
65 printf("\n");
66
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
72 printf("Saving the situation...\n");
73 xplane.saveSituation("output/situations/test.sit");
74 printf("Saved the situation\n\n");
75
76 printf("Querying the number of the engines...\n");
77 int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
78 printf("The number of engines: %d\n\n", numEngines);
79
80 try {
81 printf("Querying an invalid dataref...\n");
82 xplane.getInt("sim/aircraft/engine/num_engines");
83 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
84 } catch(const ProtocolException& exception) {
85 printf("Exception caugth: %s\n\n", exception.what());
86 }
87
88 printf("Querying the spool time of a jet engine...\n");
89 float spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_jet");
90 printf("The spool time: %f\n\n", spoolTime);
91
92 printf("Querying the spool time of a propeller...\n");
93 spoolTime = xplane.getFloat("sim/aircraft/engine/acf_spooltime_prop");
94 printf("The spool time: %f\n\n", spoolTime);
95
96 printf("Querying the coordinates...\n");
97 double latitude = xplane.getDouble("sim/flightmodel/position/latitude");
98 double longitude = xplane.getDouble("sim/flightmodel/position/longitude");
99 printf("The coordinates: %f, %f\n\n", latitude, longitude);
100
101 printf("Querying the aircraft's description as an array of a fixed size...\n");
102 uint8_t result[260];
103 size_t length = xplane.getByteArray("sim/aircraft/view/acf_descrip",
104 result, sizeof(result)/sizeof(result[0]));
105 printf("Got %lu bytes, as a string: '%s'\n\n",
106 static_cast<unsigned long>(length), result);
107
108 printf("Querying the aircraft's description as a dynamically created array...\n");
109 uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
110 printf("Got %lu bytes, as a string: '%s'\n\n",
111 static_cast<unsigned long>(length), result1);
112
113 printf("Querying the aircraft's description as a string, with an offset of 3...\n");
114 string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
115 printf("Got: '%s'\n\n", result2.c_str());
116
117 printf("Querying the aircraft's engine types as an array of a fixed size...\n");
118 int32_t result3[8];
119 length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
120 result3, sizeof(result3)/sizeof(result3[0]));
121 printf("Got %lu values:", static_cast<unsigned long>(length));
122 for(size_t i = 0; i<length; ++i) {
123 if (i>0) printf(",");
124 printf(" %d", result3[i]);
125 }
126 printf("\n\n");
127
128 printf("Querying the aircraft's engine types as a dynamically created array...\n");
129 length = 0;
130 int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
131 printf("Got %lu values:", static_cast<unsigned long>(length));
132 for(size_t i = 0; i<length; ++i) {
133 if (i>0) printf(",");
134 printf(" %d", result4[i]);
135 }
136 printf("\n\n");
137
138 printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
139 float result5[8];
140 length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
141 result5, sizeof(result5)/sizeof(result5[0]));
142 printf("Got %lu values:", static_cast<unsigned long>(length));
143 for(size_t i = 0; i<length; ++i) {
144 if (i>0) printf(",");
145 printf(" %f", result5[i]);
146 }
147 printf("\n\n");
148
149 printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
150 length = 0;
151 float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
152 printf("Got %lu values:", static_cast<unsigned long>(length));
153 for(size_t i = 0; i<length; ++i) {
154 if (i>0) printf(",");
155 printf(" %f", result6[i]);
156 }
157 printf("\n\n");
158
159 printf("Setting the number of the engines to %d...\n", numEngines + 1);
160 xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1);
161 numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
162 printf("The new number of engines: %d\n\n", numEngines);
163
164 float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
165 printf("Setting the aircraft elevator up control from %f to %f...\n",
166 acfElevUp, acfElevUp + 15.0);
167 xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0);
168 acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
169 printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
170
171 double localX = xplane.getDouble("sim/flightmodel/position/local_x");
172 printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
173 localX, localX + 15.0);
174 xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0);
175 localX = xplane.getDouble("sim/flightmodel/position/local_x");
176 printf("The aircraft's local X-coordinate set to %f\n\n", localX);
177
178 float numBlades[8];
179 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
180 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
181
182 printf("Setting the number of blades\n from:");
183 for(size_t i = 0; i<length; ++i) {
184 if (i>0) printf(",");
185 printf(" %f", numBlades[i]);
186 }
187 printf("\n to:");
188 for(size_t i = 0; i<length; ++i) {
189 numBlades[i] += 2.5;
190 if (i>0) printf(",");
191 printf(" %f", numBlades[i]);
192 }
193 printf("\n");
194 xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",
195 numBlades, length);
196 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
197 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
198 printf("The result:");
199 for(size_t i = 0; i<length; ++i) {
200 if (i>0) printf(",");
201 printf(" %f", numBlades[i]);
202 }
203 printf("\n\n");
204
205 int32_t batteryArrayOn[8];
206 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
207 batteryArrayOn,
208 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
209
210 printf("Setting the batteries\n from:");
211 for(size_t i = 0; i<length; ++i) {
212 if (i>0) printf(",");
213 printf(" %d", batteryArrayOn[i]);
214 }
215 printf("\n to:");
216 for(size_t i = 0; i<length; ++i) {
217 batteryArrayOn[i] = !batteryArrayOn[i];
218 numBlades[i] += 2.5;
219 if (i>0) printf(",");
220 printf(" %d", batteryArrayOn[i]);
221 }
222 printf("\n");
223 xplane.setIntArray("sim/cockpit/electrical/battery_array_on",
224 batteryArrayOn, length);
225 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
226 batteryArrayOn,
227 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
228 printf("The result:");
229 for(size_t i = 0; i<length; ++i) {
230 if (i>0) printf(",");
231 printf(" %d", batteryArrayOn[i]);
232 }
233 printf("\n\n");
234
235 uint8_t tailNum[40];
236 memset(tailNum, 0, sizeof(tailNum));
237 strcpy(reinterpret_cast<char*>(tailNum), "HA-VAI");
238 printf("Setting the tail number to %s as a byte array...\n", tailNum);
239 xplane.setByteArray("sim/aircraft/view/acf_tailnum",
240 tailNum, sizeof(tailNum));
241 printf("The tail number is: '%s'\n\n",
242 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
243
244 static const char* tailNum1 = "VAI";
245 printf("Setting the tail number to %s as a string...\n", tailNum1);
246 xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40);
247 printf("The tail number is: '%s'\n\n",
248 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
249
250 try {
251 printf("Querying an invalid dataref...\n");
252 xplane.getInt("sim/aircraft/engine/num_engines");
253 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
254 } catch(const ProtocolException& exception) {
255 printf("Exception caugth: %s\n\n", exception.what());
256 }
257
258 printf("Preparing for the message tests, sleeping for 5 seconds...\n");
259 Thread::sleep(5*1000);
260
261 printf("Showing a message for 10 seconds...\n");
262 xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0);
263
264 printf("Sleeping for 3 seconds...\n");
265 Thread::sleep(3*1000);
266
267 printf("Showing another message interrupting the previous one for 3 seconds\n");
268 xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0);
269
270 printf("Sleeping for 5 seconds...\n");
271 Thread::sleep(5*1000);
272 xplane.showMessage("[basictest] and the tests come to an end!", 5.0);
273
274 return 0;
275 } catch(const Exception& exception) {
276 printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
277
278 return 1;
279 }
280}
281
282//------------------------------------------------------------------------------
283
284// Local Variables:
285// mode: C++
286// c-basic-offset: 4
287// indent-tabs-mode: nil
288// End:
Note: See TracBrowser for help on using the repository browser.