source: xplra/test/basicctest.c@ 23:e3beb05af132

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

Implemented some functions of the C interface and a simple test program exercising those

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