Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / RunLX1600Utils.cpp
blobd402b3c41ca73258092b84e9cc2e05a3b378057d
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "DebugPort.hpp"
25 #include "Device/Port/Port.hpp"
26 #include "Device/Port/ConfiguredPort.hpp"
27 #include "Device/Driver.hpp"
28 #include "Device/Register.hpp"
29 #include "Device/Parser.hpp"
30 #include "Device/Driver/LX/LX1600.hpp"
31 #include "OS/PathName.hpp"
32 #include "OS/Args.hpp"
33 #include "Profile/DeviceConfig.hpp"
34 #include "Util/StringUtil.hpp"
35 #include "Operation/ConsoleOperationEnvironment.hpp"
36 #include "IO/Async/GlobalIOThread.hpp"
37 #include "Units/System.hpp"
38 #include "Atmosphere/Pressure.hpp"
39 #include "IO/DataHandler.hpp"
41 #include <memory>
43 #include <stdio.h>
45 static bool
46 ReadFixed(fixed &value)
48 char buffer[64];
49 if (fgets(buffer, 64, stdin) == NULL || strlen(buffer) == 0)
50 return false;
52 char *end_ptr;
53 value = fixed(strtod(buffer, &end_ptr));
54 return end_ptr != buffer;
57 static bool
58 ReadUnsigned(unsigned &value)
60 char buffer[64];
61 if (fgets(buffer, 64, stdin) == NULL || strlen(buffer) == 0)
62 return false;
64 char *end_ptr;
65 value = strtoul(buffer, &end_ptr, 10);
66 return end_ptr != buffer;
69 static void
70 PrintInputRequest(const char *setting)
72 fprintf(stdout, "Please enter %s:\n", setting);
73 fprintf(stdout, "> ");
76 static bool
77 ReadFixed(const char *setting, fixed &value)
79 PrintInputRequest(setting);
80 if (ReadFixed(value))
81 return true;
83 fprintf(stdout, "Invalid input\n");
84 return false;
87 static bool
88 ReadUnsigned(const char *setting, unsigned &value)
90 PrintInputRequest(setting);
91 if (ReadUnsigned(value))
92 return true;
94 fprintf(stdout, "Invalid input\n");
95 return false;
98 static void
99 SetMC(Port &port, OperationEnvironment &env)
101 fixed mc;
102 if (!ReadFixed("the MC setting (0.0 - 5.0)", mc))
103 return;
105 fprintf(stdout, "Setting MC to \"%.1f\" ...\n", (double)mc);
107 if (LX1600::SetMacCready(port, env, mc))
108 fprintf(stdout, "MC set to \"%.1f\"\n", (double)mc);
109 else
110 fprintf(stdout, "Operation failed!\n");
113 static void
114 SetBallast(Port &port, OperationEnvironment &env)
116 fixed ballast;
117 if (!ReadFixed("the Ballast setting (1.0 - 1.5)", ballast))
118 return;
120 fprintf(stdout, "Setting Ballast to \"%.1f\" ...\n", (double)ballast);
122 if (LX1600::SetBallast(port, env, ballast))
123 fprintf(stdout, "Ballast set to \"%.1f\"\n", (double)ballast);
124 else
125 fprintf(stdout, "Operation failed!\n");
128 static void
129 SetBugs(Port &port, OperationEnvironment &env)
131 unsigned bugs;
132 if (!ReadUnsigned("the Bugs setting (0 - 30%)", bugs))
133 return;
135 fprintf(stdout, "Setting Bugs to \"%u\" ...\n", bugs);
137 if (LX1600::SetBugs(port, env, bugs))
138 fprintf(stdout, "Bugs set to \"%u\"\n", bugs);
139 else
140 fprintf(stdout, "Operation failed!\n");
143 static void
144 SetAltitudeOffset(Port &port, OperationEnvironment &env)
146 fixed altitude_offset;
147 if (!ReadFixed("the altitude offset setting (m)", altitude_offset))
148 return;
150 fprintf(stdout, "Setting altitude offset to \"%.1f m\" ...\n",
151 (double)altitude_offset);
153 if (LX1600::SetAltitudeOffset(port, env, Units::ToUserUnit(altitude_offset, Unit::FEET)))
154 fprintf(stdout, "Altitude offset set to \"%.1f m\"\n",
155 (double)altitude_offset);
156 else
157 fprintf(stdout, "Operation failed!\n");
160 static void
161 SetQNH(Port &port, OperationEnvironment &env)
163 fixed qnh;
164 if (!ReadFixed("the QNH setting (hPa)", qnh))
165 return;
167 fprintf(stdout, "Setting QNH to \"%.1f hPa\" ...\n",
168 (double)qnh);
170 if (LX1600::SetQNH(port, env, AtmosphericPressure::HectoPascal(qnh)))
171 fprintf(stdout, "QNH set to \"%.1f hPa\"\n",
172 (double)qnh);
173 else
174 fprintf(stdout, "Operation failed!\n");
177 static void
178 SetVolume(Port &port, OperationEnvironment &env)
180 unsigned volume;
181 if (!ReadUnsigned("the Volume setting (0 - 100%)", volume))
182 return;
184 fprintf(stdout, "Setting Volume to \"%u %%\" ...\n", volume);
186 if (LX1600::SetVolume(port, env, volume))
187 fprintf(stdout, "Volume set to \"%u %%\"\n", volume);
188 else
189 fprintf(stdout, "Operation failed!\n");
192 static void
193 SetPolar(Port &port, OperationEnvironment &env)
195 fixed a, b, c;
196 if (!ReadFixed("polar coefficient a", a) ||
197 !ReadFixed("polar coefficient b", b) ||
198 !ReadFixed("polar coefficient c", c))
199 return;
201 if (LX1600::SetPolar(port, env, a, b, c))
202 fprintf(stdout, "Polar coefficients set to \"%.2f, %.2f, %.2f\"\n",
203 (double)a, (double)b, (double)c);
204 else
205 fprintf(stdout, "Operation failed!\n");
208 static void
209 SetFilters(Port &port, OperationEnvironment &env)
211 fixed vario_filter, te_filter;
212 unsigned te_level;
213 if (!ReadFixed("the Vario filter (sec, default = 1)", vario_filter) ||
214 !ReadFixed("the TE filter (0.1 - 2.0, default = 1.5)", te_filter) ||
215 !ReadUnsigned("the TE level (50 - 150 %, default = 0 = off)", te_level))
216 return;
218 if (LX1600::SetFilters(port, env, vario_filter, te_filter, te_level))
219 fprintf(stdout, "Filters set to \"%.1f, %.1f, %u\"\n",
220 (double)vario_filter, (double)te_filter, te_level);
221 else
222 fprintf(stdout, "Operation failed!\n");
225 static void
226 SetSCSettings(Port &port, OperationEnvironment &env)
228 unsigned mode, control_mode;
229 fixed deadband, threshold_speed;
231 if (!ReadUnsigned("the SC Mode (EXTERNAL = 0, default = ON_CIRCLING = 1, AUTO_IAS = 2)", mode) ||
232 !ReadFixed("the SC deadband (0 - 10.0 m/s, default=1)", deadband) ||
233 !ReadUnsigned("the SC switch mode (NORMAL = 0, default = INVERTED = 1, TASTER = 2)", control_mode))
234 return;
236 if (mode != 2u)
237 threshold_speed = fixed(0);
238 else if (!ReadFixed("the SC threshold speed (50 - 150 km/h, default=110)", threshold_speed))
239 return;
241 if (LX1600::SetSCSettings(port, env, (LX1600::SCMode)mode, deadband,
242 (LX1600::SCControlMode)control_mode, threshold_speed))
243 fprintf(stdout, "SC settings changed!\n");
244 else
245 fprintf(stdout, "Operation failed!\n");
248 static void
249 WriteMenu()
251 fprintf(stdout, "------------------------------------\n"
252 "LX1600 Utils Menu\n"
253 "------------------------------------\n"
254 "Press any of the following commands:\n\n"
255 "h: Display this menu\n"
256 "1: Set the MC\n"
257 "2: Set Ballast\n"
258 "3: Set Bugs\n"
259 "4: Set Altitude Offset\n"
260 "5: Set QNH\n"
261 "6: Set Volume\n"
262 "p: Set polar coefficients\n"
263 "f: Set filter settings\n"
264 "s: Set speed control settings\n"
265 "q: Quit this application\n"
266 "------------------------------------\n");
269 static void
270 RunUI(Port &port, OperationEnvironment &env)
272 WriteMenu();
274 while (true) {
275 fprintf(stdout, "> ");
277 char in[20];
278 if (fgets(in, 20, stdin) == NULL || strlen(in) == 0) {
279 fprintf(stdout, "Invalid input\n");
280 continue;
283 switch (in[0]) {
284 case '?':
285 case 'h':
286 case 'H':
287 WriteMenu();
288 break;
289 case '1':
290 SetMC(port, env);
291 break;
292 case '2':
293 SetBallast(port, env);
294 break;
295 case '3':
296 SetBugs(port, env);
297 break;
298 case '4':
299 SetAltitudeOffset(port, env);
300 break;
301 case '5':
302 SetQNH(port, env);
303 break;
304 case '6':
305 SetVolume(port, env);
306 break;
307 case 'p':
308 case 'P':
309 SetPolar(port, env);
310 break;
311 case 'f':
312 case 'F':
313 SetFilters(port, env);
314 break;
315 case 's':
316 case 'S':
317 SetSCSettings(port, env);
318 break;
319 case 'q':
320 case 'Q':
321 fprintf(stdout, "Closing LX1600 Utils ...\n");
322 return;
323 default:
324 fprintf(stdout, "Invalid input\n");
325 break;
330 class NullDataHandler : public DataHandler {
331 public:
332 virtual void DataReceived(const void *data, size_t length) {}
336 main(int argc, char **argv)
338 Args args(argc, argv, "PORT BAUD");
339 const DeviceConfig config = ParsePortArgs(args);
340 args.ExpectEnd();
342 InitialiseIOThread();
344 NullDataHandler handler;
345 std::unique_ptr<Port> port(OpenPort(config, handler));
346 if (!port) {
347 fprintf(stderr, "Failed to open COM port\n");
348 return EXIT_FAILURE;
351 ConsoleOperationEnvironment env;
352 RunUI(*port, env);
354 DeinitialiseIOThread();
356 return EXIT_SUCCESS;