source: xplra/test/basictest.cc@ 36:29e3b676c0c2

Last change on this file since 36:29e3b676c0c2 was 36:29e3b676c0c2, checked in by István Váradi <ivaradi@…>, 11 years ago

Added a new command to query the versions

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