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