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/xplra.h>
|
---|
32 |
|
---|
33 | #include <stdio.h>
|
---|
34 | #include <string.h>
|
---|
35 |
|
---|
36 | //------------------------------------------------------------------------------
|
---|
37 |
|
---|
38 | int main()
|
---|
39 | {
|
---|
40 | static const char* tailNum1 = "VAI";
|
---|
41 |
|
---|
42 | int retval = 0;
|
---|
43 | const char* errorString = 0;
|
---|
44 | int numEngines = 0;
|
---|
45 | float spoolTime = 0.0;
|
---|
46 | double latitude = 0.0, longitude = 0.0;
|
---|
47 | ssize_t length, i;
|
---|
48 | size_t length1, j;
|
---|
49 | uint8_t result[260];
|
---|
50 | uint8_t* result1 = 0;
|
---|
51 | int32_t result3[8];
|
---|
52 | int32_t* result4 = 0;
|
---|
53 | float result5[8];
|
---|
54 | float* result6 = 0;
|
---|
55 | float acfElevUp;
|
---|
56 | double localX;
|
---|
57 | float numBlades[8];
|
---|
58 | int32_t batteryArrayOn[8];
|
---|
59 | uint8_t tailNum[40];
|
---|
60 |
|
---|
61 | printf("Connection to X-Plane...\n");
|
---|
62 | int connectionID = xplra_connect();
|
---|
63 |
|
---|
64 | if (connectionID>=0) {
|
---|
65 | printf("Connected.\n\n");
|
---|
66 | } else {
|
---|
67 | printf("Connection failed.\n");
|
---|
68 | goto cleanup;
|
---|
69 | }
|
---|
70 |
|
---|
71 | printf("Querying the number of the engines...\n");
|
---|
72 | if (xplra_get_int(&numEngines, connectionID,
|
---|
73 | "sim/aircraft/engine/acf_num_engines")<0)
|
---|
74 | {
|
---|
75 | goto error;
|
---|
76 | }
|
---|
77 | printf("The number of engines: %d\n\n", numEngines);
|
---|
78 |
|
---|
79 | printf("Querying an invalid dataref...\n");
|
---|
80 | if (xplra_get_int(&numEngines, connectionID,
|
---|
81 | "sim/aircraft/engine/num_engines")==0) {
|
---|
82 | printf("\n>>>>>>>>>>>> Succeeded!!!!!!!!!!!!!!!!!!!!!!\n\n");
|
---|
83 | } else {
|
---|
84 | printf("Error occured: %s\n\n",
|
---|
85 | xplra_get_last_error_string(connectionID));
|
---|
86 | xplra_clear_last_error(connectionID);
|
---|
87 | }
|
---|
88 |
|
---|
89 | printf("Querying the spool time of a jet engine...\n");
|
---|
90 | if (xplra_get_float(&spoolTime, connectionID,
|
---|
91 | "sim/aircraft/engine/acf_spooltime_jet")<0)
|
---|
92 | {
|
---|
93 | goto error;
|
---|
94 | }
|
---|
95 | printf("The spool time: %f\n\n", spoolTime);
|
---|
96 |
|
---|
97 | printf("Querying the spool time of a propeller...\n");
|
---|
98 | if (xplra_get_float(&spoolTime, connectionID,
|
---|
99 | "sim/aircraft/engine/acf_spooltime_prop")<0)
|
---|
100 | {
|
---|
101 | goto error;
|
---|
102 | }
|
---|
103 | printf("The spool time: %f\n\n", spoolTime);
|
---|
104 |
|
---|
105 | printf("Querying the coordinates...\n");
|
---|
106 | if (xplra_get_double(&latitude, connectionID,
|
---|
107 | "sim/flightmodel/position/latitude")<0)
|
---|
108 | {
|
---|
109 | goto error;
|
---|
110 | }
|
---|
111 | if (xplra_get_double(&longitude, connectionID,
|
---|
112 | "sim/flightmodel/position/longitude")<0)
|
---|
113 | {
|
---|
114 | goto error;
|
---|
115 | }
|
---|
116 | printf("The coordinates: %f, %f\n\n", latitude, longitude);
|
---|
117 |
|
---|
118 | printf("Querying the aircraft's description as an array of a fixed size...\n");
|
---|
119 | length = xplra_get_byte_array(result, sizeof(result)/sizeof(result[0]),
|
---|
120 | 0, connectionID,
|
---|
121 | "sim/aircraft/view/acf_descrip");
|
---|
122 | if (length<0) goto error;
|
---|
123 | printf("Got %zu bytes, as a string: '%s'\n\n", length, result);
|
---|
124 |
|
---|
125 | printf("Querying the aircraft's description as a dynamically created array with an offset of 3...\n");
|
---|
126 | length1 = 0;
|
---|
127 | result1 = xplra_get_byte_array_new(&length1, 3, connectionID,
|
---|
128 | "sim/aircraft/view/acf_descrip");
|
---|
129 | if (result1==0) goto error;
|
---|
130 | printf("Got %zu bytes, as a string: '%s'\n\n", length1, result1);
|
---|
131 |
|
---|
132 | printf("Querying the aircraft's engine types as an array of a fixed size...\n");
|
---|
133 | length = xplra_get_int_array(result3, sizeof(result3)/sizeof(result3[0]), 0,
|
---|
134 | connectionID, "sim/aircraft/prop/acf_en_type");
|
---|
135 |
|
---|
136 | if (length<0) goto error;
|
---|
137 | printf("Got %zd values:", length);
|
---|
138 | for(i = 0; i<length; ++i) {
|
---|
139 | if (i>0) printf(",");
|
---|
140 | printf(" %d", result3[i]);
|
---|
141 | }
|
---|
142 | printf("\n\n");
|
---|
143 |
|
---|
144 | printf("Querying the aircraft's engine types as a dynamically created array...\n");
|
---|
145 | length1 = 0;
|
---|
146 | result4 = xplra_get_int_array_new(&length1, 0, connectionID,
|
---|
147 | "sim/aircraft/prop/acf_en_type");
|
---|
148 | printf("Got %zu values:", length1);
|
---|
149 | for(j = 0; j<length1; ++j) {
|
---|
150 | if (j>0) printf(",");
|
---|
151 | printf(" %d", result4[j]);
|
---|
152 | }
|
---|
153 | printf("\n\n");
|
---|
154 |
|
---|
155 | printf("Querying the aircraft's propeller direction as an array of a fixed size...\n");
|
---|
156 | length = xplra_get_float_array(result5, sizeof(result5)/sizeof(result5[0]),
|
---|
157 | 0, connectionID,
|
---|
158 | "sim/aircraft/prop/acf_prop_dir");
|
---|
159 | printf("Got %zd values:", length);
|
---|
160 | for(i = 0; i<length; ++i) {
|
---|
161 | if (i>0) printf(",");
|
---|
162 | printf(" %f", result5[i]);
|
---|
163 | }
|
---|
164 | printf("\n\n");
|
---|
165 |
|
---|
166 | printf("Querying the aircraft's propeller direction as a dynamically created array...\n");
|
---|
167 | length1 = 0;
|
---|
168 | result6 = xplra_get_float_array_new(&length1, 0, connectionID,
|
---|
169 | "sim/aircraft/prop/acf_prop_dir");
|
---|
170 | printf("Got %zu values:", length1);
|
---|
171 | for(j = 0; j<length1; ++j) {
|
---|
172 | if (j>0) printf(",");
|
---|
173 | printf(" %f", result6[j]);
|
---|
174 | }
|
---|
175 | printf("\n\n");
|
---|
176 |
|
---|
177 | printf("Setting the number of the engines to %d...\n", numEngines + 1);
|
---|
178 | if (xplra_set_int(connectionID, "sim/aircraft/engine/acf_num_engines",
|
---|
179 | numEngines + 1)<0)
|
---|
180 | {
|
---|
181 | goto error;
|
---|
182 | }
|
---|
183 | if (xplra_get_int(&numEngines, connectionID,
|
---|
184 | "sim/aircraft/engine/acf_num_engines")<0)
|
---|
185 | {
|
---|
186 | goto error;
|
---|
187 | }
|
---|
188 | printf("The new number of engines: %d\n\n", numEngines);
|
---|
189 |
|
---|
190 |
|
---|
191 | if (xplra_get_float(&acfElevUp, connectionID,
|
---|
192 | "sim/aircraft/controls/acf_elev_up")<0)
|
---|
193 | {
|
---|
194 | goto error;
|
---|
195 | }
|
---|
196 | printf("Setting the aircraft elevator up control from %f to %f...\n",
|
---|
197 | acfElevUp, acfElevUp + 15.0);
|
---|
198 | if (xplra_set_float(connectionID,
|
---|
199 | "sim/aircraft/controls/acf_elev_up",
|
---|
200 | acfElevUp + 15.0) < 0)
|
---|
201 | {
|
---|
202 | goto error;
|
---|
203 | }
|
---|
204 | if (xplra_get_float(&acfElevUp, connectionID,
|
---|
205 | "sim/aircraft/controls/acf_elev_up")<0)
|
---|
206 | {
|
---|
207 | goto error;
|
---|
208 | }
|
---|
209 | printf("The aircraft elevator up control set to %f\n\n", acfElevUp);
|
---|
210 |
|
---|
211 | if (xplra_get_double(&localX, connectionID,
|
---|
212 | "sim/flightmodel/position/local_x")<0)
|
---|
213 | {
|
---|
214 | goto error;
|
---|
215 | }
|
---|
216 | printf("Setting the aircraft's local X-coordinate from %f to %f...\n",
|
---|
217 | localX, localX + 15.0);
|
---|
218 | if (xplra_set_double(connectionID, "sim/flightmodel/position/local_x",
|
---|
219 | localX + 15.0)<0)
|
---|
220 | {
|
---|
221 | goto error;
|
---|
222 | }
|
---|
223 | if (xplra_get_double(&localX, connectionID,
|
---|
224 | "sim/flightmodel/position/local_x")<0)
|
---|
225 | {
|
---|
226 | goto error;
|
---|
227 | }
|
---|
228 | printf("The aircraft's local X-coordinate set to %f\n\n", localX);
|
---|
229 |
|
---|
230 | length = xplra_get_float_array(numBlades,
|
---|
231 | sizeof(numBlades)/sizeof(numBlades[0]), 0,
|
---|
232 | connectionID,
|
---|
233 | "sim/aircraft/prop/acf_num_blades");
|
---|
234 | if (length<0) goto error;
|
---|
235 |
|
---|
236 | printf("Setting the number of blades\n from:");
|
---|
237 | for(i = 0; i<length; ++i) {
|
---|
238 | if (i>0) printf(",");
|
---|
239 | printf(" %f", numBlades[i]);
|
---|
240 | }
|
---|
241 | printf("\n to:");
|
---|
242 | for(i = 0; i<length; ++i) {
|
---|
243 | numBlades[i] += 2.5;
|
---|
244 | if (i>0) printf(",");
|
---|
245 | printf(" %f", numBlades[i]);
|
---|
246 | }
|
---|
247 | printf("\n");
|
---|
248 | if (xplra_set_float_array(connectionID,
|
---|
249 | "sim/aircraft/prop/acf_num_blades",
|
---|
250 | numBlades, length, 0)<0)
|
---|
251 | {
|
---|
252 | goto error;
|
---|
253 | }
|
---|
254 | length = xplra_get_float_array(numBlades,
|
---|
255 | sizeof(numBlades)/sizeof(numBlades[0]), 0,
|
---|
256 | connectionID,
|
---|
257 | "sim/aircraft/prop/acf_num_blades");
|
---|
258 | if (length<0) goto error;
|
---|
259 | printf("The result:");
|
---|
260 | for(i = 0; i<length; ++i) {
|
---|
261 | if (i>0) printf(",");
|
---|
262 | printf(" %f", numBlades[i]);
|
---|
263 | }
|
---|
264 | printf("\n\n");
|
---|
265 |
|
---|
266 | length = xplra_get_int_array(batteryArrayOn,
|
---|
267 | sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
|
---|
268 | 0, connectionID,
|
---|
269 | "sim/cockpit/electrical/battery_array_on");
|
---|
270 | if (length<0) goto error;
|
---|
271 | printf("Setting the batteries\n from:");
|
---|
272 | for(i = 0; i<length; ++i) {
|
---|
273 | if (i>0) printf(",");
|
---|
274 | printf(" %d", batteryArrayOn[i]);
|
---|
275 | }
|
---|
276 | printf("\n to:");
|
---|
277 | for(i = 0; i<length; ++i) {
|
---|
278 | batteryArrayOn[i] = !batteryArrayOn[i];
|
---|
279 | numBlades[i] += 2.5;
|
---|
280 | if (i>0) printf(",");
|
---|
281 | printf(" %d", batteryArrayOn[i]);
|
---|
282 | }
|
---|
283 | printf("\n");
|
---|
284 | if (xplra_set_int_array(connectionID,
|
---|
285 | "sim/cockpit/electrical/battery_array_on",
|
---|
286 | batteryArrayOn, length, 0)<0)
|
---|
287 | {
|
---|
288 | goto error;
|
---|
289 | }
|
---|
290 | length = xplra_get_int_array(batteryArrayOn,
|
---|
291 | sizeof(batteryArrayOn)/sizeof(batteryArrayOn[0]),
|
---|
292 | 0, connectionID,
|
---|
293 | "sim/cockpit/electrical/battery_array_on");
|
---|
294 | if (length<0) goto error;
|
---|
295 | printf("The result:");
|
---|
296 | for(i = 0; i<length; ++i) {
|
---|
297 | if (i>0) printf(",");
|
---|
298 | printf(" %d", batteryArrayOn[i]);
|
---|
299 | }
|
---|
300 | printf("\n\n");
|
---|
301 |
|
---|
302 | memset(tailNum, 0, sizeof(tailNum));
|
---|
303 | strcpy((char*)tailNum, "HA-VAI");
|
---|
304 | printf("Setting the tail number to %s as a byte array...\n", tailNum);
|
---|
305 | if (xplra_set_byte_array(connectionID,
|
---|
306 | "sim/aircraft/view/acf_tailnum",
|
---|
307 | tailNum, sizeof(tailNum), 0)<0)
|
---|
308 | {
|
---|
309 | goto error;
|
---|
310 | }
|
---|
311 |
|
---|
312 | memset(tailNum, 0, sizeof(tailNum));
|
---|
313 | length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
|
---|
314 | connectionID,
|
---|
315 | "sim/aircraft/view/acf_tailnum");
|
---|
316 | if (length<0) goto error;
|
---|
317 | printf("The tail number is: '%s'\n\n", (char*)tailNum);
|
---|
318 |
|
---|
319 | printf("Setting the tail number to %s as a string...\n", tailNum1);
|
---|
320 | if (xplra_set_string(connectionID, "sim/aircraft/view/acf_tailnum",
|
---|
321 | tailNum1, 40, 0)<0)
|
---|
322 | {
|
---|
323 | goto error;
|
---|
324 | }
|
---|
325 | memset(tailNum, 0, sizeof(tailNum));
|
---|
326 | length = xplra_get_byte_array(tailNum, sizeof(tailNum), 0,
|
---|
327 | connectionID,
|
---|
328 | "sim/aircraft/view/acf_tailnum");
|
---|
329 | if (length<0) goto error;
|
---|
330 | printf("The tail number is: '%s'\n\n", (char*)tailNum);
|
---|
331 |
|
---|
332 | goto cleanup;
|
---|
333 | error:
|
---|
334 | errorString = xplra_get_last_error_string(connectionID);
|
---|
335 | if (errorString==0) {
|
---|
336 | printf("\nUnknown error occured!\n");
|
---|
337 | } else {
|
---|
338 | printf("\nError: %s\n", errorString);
|
---|
339 | }
|
---|
340 |
|
---|
341 | retval = 1;
|
---|
342 | cleanup:
|
---|
343 | if (connectionID>=0) xplra_disconnect(connectionID);
|
---|
344 | return retval;
|
---|
345 | }
|
---|
346 |
|
---|
347 | //------------------------------------------------------------------------------
|
---|
348 |
|
---|
349 | // Local Variables:
|
---|
350 | // mode: C++
|
---|
351 | // c-basic-offset: 4
|
---|
352 | // indent-tabs-mode: nil
|
---|
353 | // End:
|
---|