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"
46 ReadFixed(fixed
&value
)
49 if (fgets(buffer
, 64, stdin
) == NULL
|| strlen(buffer
) == 0)
53 value
= fixed(strtod(buffer
, &end_ptr
));
54 return end_ptr
!= buffer
;
58 ReadUnsigned(unsigned &value
)
61 if (fgets(buffer
, 64, stdin
) == NULL
|| strlen(buffer
) == 0)
65 value
= strtoul(buffer
, &end_ptr
, 10);
66 return end_ptr
!= buffer
;
70 PrintInputRequest(const char *setting
)
72 fprintf(stdout
, "Please enter %s:\n", setting
);
73 fprintf(stdout
, "> ");
77 ReadFixed(const char *setting
, fixed
&value
)
79 PrintInputRequest(setting
);
83 fprintf(stdout
, "Invalid input\n");
88 ReadUnsigned(const char *setting
, unsigned &value
)
90 PrintInputRequest(setting
);
91 if (ReadUnsigned(value
))
94 fprintf(stdout
, "Invalid input\n");
99 SetMC(Port
&port
, OperationEnvironment
&env
)
102 if (!ReadFixed("the MC setting (0.0 - 5.0)", mc
))
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
);
110 fprintf(stdout
, "Operation failed!\n");
114 SetBallast(Port
&port
, OperationEnvironment
&env
)
117 if (!ReadFixed("the Ballast setting (1.0 - 1.5)", ballast
))
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
);
125 fprintf(stdout
, "Operation failed!\n");
129 SetBugs(Port
&port
, OperationEnvironment
&env
)
132 if (!ReadUnsigned("the Bugs setting (0 - 30%)", bugs
))
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
);
140 fprintf(stdout
, "Operation failed!\n");
144 SetAltitudeOffset(Port
&port
, OperationEnvironment
&env
)
146 fixed altitude_offset
;
147 if (!ReadFixed("the altitude offset setting (m)", altitude_offset
))
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
);
157 fprintf(stdout
, "Operation failed!\n");
161 SetQNH(Port
&port
, OperationEnvironment
&env
)
164 if (!ReadFixed("the QNH setting (hPa)", qnh
))
167 fprintf(stdout
, "Setting QNH to \"%.1f hPa\" ...\n",
170 if (LX1600::SetQNH(port
, env
, AtmosphericPressure::HectoPascal(qnh
)))
171 fprintf(stdout
, "QNH set to \"%.1f hPa\"\n",
174 fprintf(stdout
, "Operation failed!\n");
178 SetVolume(Port
&port
, OperationEnvironment
&env
)
181 if (!ReadUnsigned("the Volume setting (0 - 100%)", volume
))
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
);
189 fprintf(stdout
, "Operation failed!\n");
193 SetPolar(Port
&port
, OperationEnvironment
&env
)
196 if (!ReadFixed("polar coefficient a", a
) ||
197 !ReadFixed("polar coefficient b", b
) ||
198 !ReadFixed("polar coefficient c", c
))
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
);
205 fprintf(stdout
, "Operation failed!\n");
209 SetFilters(Port
&port
, OperationEnvironment
&env
)
211 fixed vario_filter
, te_filter
;
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
))
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
);
222 fprintf(stdout
, "Operation failed!\n");
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
))
237 threshold_speed
= fixed(0);
238 else if (!ReadFixed("the SC threshold speed (50 - 150 km/h, default=110)", threshold_speed
))
241 if (LX1600::SetSCSettings(port
, env
, (LX1600::SCMode
)mode
, deadband
,
242 (LX1600::SCControlMode
)control_mode
, threshold_speed
))
243 fprintf(stdout
, "SC settings changed!\n");
245 fprintf(stdout
, "Operation failed!\n");
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"
259 "4: Set Altitude Offset\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");
270 RunUI(Port
&port
, OperationEnvironment
&env
)
275 fprintf(stdout
, "> ");
278 if (fgets(in
, 20, stdin
) == NULL
|| strlen(in
) == 0) {
279 fprintf(stdout
, "Invalid input\n");
293 SetBallast(port
, env
);
299 SetAltitudeOffset(port
, env
);
305 SetVolume(port
, env
);
313 SetFilters(port
, env
);
317 SetSCSettings(port
, env
);
321 fprintf(stdout
, "Closing LX1600 Utils ...\n");
324 fprintf(stdout
, "Invalid input\n");
330 class NullDataHandler
: public DataHandler
{
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
);
342 InitialiseIOThread();
344 NullDataHandler handler
;
345 std::unique_ptr
<Port
> port(OpenPort(config
, handler
));
347 fprintf(stderr
, "Failed to open COM port\n");
351 ConsoleOperationEnvironment env
;
354 DeinitialiseIOThread();