source: xplra/test/basictest.cc@ 56:1d491f271eaf

Last change on this file since 56:1d491f271eaf was 40:ec5dde8a6ff6, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented the client support for the new commands and updated the basic test programs with tests showing messages

File size: 11.9 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("Querying the number of the engines...\n");
73 int numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
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 }
83
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);
96
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]));
101 printf("Got %zu bytes, as a string: '%s'\n\n", length, result);
102
103 printf("Querying the aircraft's description as a dynamically created array...\n");
104 uint8_t* result1 = xplane.getByteArray("sim/aircraft/view/acf_descrip", length);
105 printf("Got %zu bytes, as a string: '%s'\n\n", length, result1);
106
107 printf("Querying the aircraft's description as a string, with an offset of 3...\n");
108 string result2 = xplane.getString("sim/aircraft/view/acf_descrip", 3);
109 printf("Got: '%s'\n\n", result2.c_str());
110
111 printf("Querying the aircraft's engine types as an array of a fixed size...\n");
112 int32_t result3[8];
113 length = xplane.getIntArray("sim/aircraft/prop/acf_en_type",
114 result3, sizeof(result3)/sizeof(result3[0]));
115 printf("Got %zu values:", length);
116 for(size_t i = 0; i<length; ++i) {
117 if (i>0) printf(",");
118 printf(" %d", result3[i]);
119 }
120 printf("\n\n");
121
122 printf("Querying the aircraft's engine types as a dynamically created array...\n");
123 length = 0;
124 int32_t* result4 = xplane.getIntArray("sim/aircraft/prop/acf_en_type", length);
125 printf("Got %zu values:", length);
126 for(size_t i = 0; i<length; ++i) {
127 if (i>0) printf(",");
128 printf(" %d", result4[i]);
129 }
130 printf("\n\n");
131
132 printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
133 float result5[8];
134 length = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir",
135 result5, sizeof(result5)/sizeof(result5[0]));
136 printf("Got %zu values:", length);
137 for(size_t i = 0; i<length; ++i) {
138 if (i>0) printf(",");
139 printf(" %f", result5[i]);
140 }
141 printf("\n\n");
142
143 printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
144 length = 0;
145 float* result6 = xplane.getFloatArray("sim/aircraft/prop/acf_prop_dir", length);
146 printf("Got %zu values:", length);
147 for(size_t i = 0; i<length; ++i) {
148 if (i>0) printf(",");
149 printf(" %f", result6[i]);
150 }
151 printf("\n\n");
152
153 printf("Setting the number of the engines to %d...\n", numEngines + 1);
154 xplane.setInt("sim/aircraft/engine/acf_num_engines", numEngines + 1);
155 numEngines = xplane.getInt("sim/aircraft/engine/acf_num_engines");
156 printf("The new number of engines: %d\n\n", numEngines);
157
158 float acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
159 printf("Setting the aircraft elevator up control from %f to %f...\n",
160 acfElevUp, acfElevUp + 15.0);
161 xplane.setFloat("sim/aircraft/controls/acf_elev_up", acfElevUp + 15.0);
162 acfElevUp = xplane.getFloat("sim/aircraft/controls/acf_elev_up");
163 printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
164
165 double localX = xplane.getDouble("sim/flightmodel/position/local_x");
166 printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
167 localX, localX + 15.0);
168 xplane.setDouble("sim/flightmodel/position/local_x", localX + 15.0);
169 localX = xplane.getDouble("sim/flightmodel/position/local_x");
170 printf("The aircraft's local X-coordinate set to %f\n\n", localX);
171
172 float numBlades[8];
173 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
174 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
175
176 printf("Setting the number of blades\n from:");
177 for(size_t i = 0; i<length; ++i) {
178 if (i>0) printf(",");
179 printf(" %f", numBlades[i]);
180 }
181 printf("\n to:");
182 for(size_t i = 0; i<length; ++i) {
183 numBlades[i] += 2.5;
184 if (i>0) printf(",");
185 printf(" %f", numBlades[i]);
186 }
187 printf("\n");
188 xplane.setFloatArray("sim/aircraft/prop/acf_num_blades",
189 numBlades, length);
190 length = xplane.getFloatArray("sim/aircraft/prop/acf_num_blades",
191 numBlades, sizeof(numBlades)/sizeof(numBlades[0]));
192 printf("The result:");
193 for(size_t i = 0; i<length; ++i) {
194 if (i>0) printf(",");
195 printf(" %f", numBlades[i]);
196 }
197 printf("\n\n");
198
199 int32_t batteryArrayOn[8];
200 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
201 batteryArrayOn,
202 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
203
204 printf("Setting the batteries\n from:");
205 for(size_t i = 0; i<length; ++i) {
206 if (i>0) printf(",");
207 printf(" %d", batteryArrayOn[i]);
208 }
209 printf("\n to:");
210 for(size_t i = 0; i<length; ++i) {
211 batteryArrayOn[i] = !batteryArrayOn[i];
212 numBlades[i] += 2.5;
213 if (i>0) printf(",");
214 printf(" %d", batteryArrayOn[i]);
215 }
216 printf("\n");
217 xplane.setIntArray("sim/cockpit/electrical/battery_array_on",
218 batteryArrayOn, length);
219 length = xplane.getIntArray("sim/cockpit/electrical/battery_array_on",
220 batteryArrayOn,
221 sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]));
222 printf("The result:");
223 for(size_t i = 0; i<length; ++i) {
224 if (i>0) printf(",");
225 printf(" %d", batteryArrayOn[i]);
226 }
227 printf("\n\n");
228
229 uint8_t tailNum[40];
230 memset(tailNum, 0, sizeof(tailNum));
231 strcpy(reinterpret_cast<char*>(tailNum), "HA-VAI");
232 printf("Setting the tail number to %s as a byte array...\n", tailNum);
233 xplane.setByteArray("sim/aircraft/view/acf_tailnum",
234 tailNum, sizeof(tailNum));
235 printf("The tail number is: '%s'\n\n",
236 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
237
238 static const char* tailNum1 = "VAI";
239 printf("Setting the tail number to %s as a string...\n", tailNum1);
240 xplane.setString("sim/aircraft/view/acf_tailnum", tailNum1, 40);
241 printf("The tail number is: '%s'\n\n",
242 xplane.getString("sim/aircraft/view/acf_tailnum").c_str());
243
244 try {
245 printf("Querying an invalid dataref...\n");
246 xplane.getInt("sim/aircraft/engine/num_engines");
247 printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
248 } catch(const ProtocolException& exception) {
249 printf("Exception caugth: %s\n\n", exception.what());
250 }
251
252 printf("Preparing for the message tests, sleeping for 5 seconds...\n");
253 Thread::sleep(5*1000);
254
255 printf("Showing a message for 10 seconds...\n");
256 xplane.showMessage("[basictest] this message appears for 10 seconds", 10.0);
257
258 printf("Sleeping for 3 seconds...\n");
259 Thread::sleep(3*1000);
260
261 printf("Showing another message interrupting the previous one for 3 seconds\n");
262 xplane.showMessage("[basictest] but this message interrupts it, and is displayed for 3 seconds", 3.0);
263
264 printf("Sleeping for 5 seconds...\n");
265 Thread::sleep(5*1000);
266 xplane.showMessage("[basictest] and the tests come to an end!", 5.0);
267
268 return 0;
269 } catch(const Exception& exception) {
270 printf("\n>>>>>>>>>>>>>>>>> Exception caugth: %s\n", exception.what());
271
272 return 1;
273 }
274}
275
276//------------------------------------------------------------------------------
277
278// Local Variables:
279// mode: C++
280// c-basic-offset: 4
281// indent-tabs-mode: nil
282// End:
Note: See TracBrowser for help on using the repository browser.