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