2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_14.*;
27 import org.altusmetrum.altosuilib_14.*;
29 public class AltosConfigFC implements ActionListener {
37 public void set(int i) {
40 public int_ref(int i) {
51 public void set(String i) {
54 public string_ref(String i) {
61 AltosSerial serial_line;
65 AltosConfigFCUI config_ui;
66 boolean serial_started;
69 void start_serial() throws InterruptedException, TimeoutException {
70 serial_started = true;
72 serial_line.start_remote();
75 void stop_serial() throws InterruptedException {
78 serial_started = false;
80 serial_line.stop_remote();
84 data.set_values(config_ui);
85 config_ui.set_clean();
88 config_ui.make_visible();
94 final static int serial_mode_read = 0;
95 final static int serial_mode_save = 1;
96 final static int serial_mode_reboot = 2;
98 class SerialData implements Runnable {
102 void callback(String in_cmd) {
103 final String cmd = in_cmd;
104 Runnable r = new Runnable() {
106 if (cmd.equals("abort")) {
108 } else if (cmd.equals("all finished")) {
109 if (serial_line != null)
114 SwingUtilities.invokeLater(r);
121 data = new AltosConfigData(config.serial_line);
122 } catch (InterruptedException ie) {
123 } catch (TimeoutException te) {
127 } catch (InterruptedException ie) {
132 } catch (InterruptedException ie) {
135 callback("all finished");
141 data.save(serial_line, remote);
143 AltosUIPreferences.set_frequency(device.getSerial(),
145 } catch (InterruptedException ie) {
146 } catch (TimeoutException te) {
150 } catch (InterruptedException ie) {
158 serial_line.printf("r eboot\n");
159 serial_line.flush_output();
160 } catch (InterruptedException ie) {
161 } catch (TimeoutException te) {
166 } catch (InterruptedException ie) {
172 switch (serial_mode) {
173 case serial_mode_save:
175 /* fall through ... */
176 case serial_mode_read:
179 case serial_mode_reboot:
185 public SerialData(AltosConfigFC in_config, int in_serial_mode) {
187 serial_mode = in_serial_mode;
191 void run_serial_thread(int serial_mode) {
192 SerialData sd = new SerialData(this, serial_mode);
193 Thread st = new Thread(sd);
197 void init_ui () throws InterruptedException, TimeoutException {
198 config_ui = new AltosConfigFCUI(owner, remote);
199 config_ui.addActionListener(this);
200 serial_line.set_frame(owner);
205 if (serial_line != null) {
209 JOptionPane.showMessageDialog(owner,
210 String.format("Connection to \"%s\" failed",
211 device.toShortString()),
213 JOptionPane.ERROR_MESSAGE);
214 config_ui.setVisible(false);
217 void set_ui() throws InterruptedException, TimeoutException {
218 if (serial_line != null)
219 run_serial_thread(serial_mode_read);
225 return AltosConvert.radio_to_frequency(data.radio_frequency,
227 data.radio_calibration,
234 /* bounds check stuff */
235 if (config_ui.flight_log_max() != AltosLib.MISSING &&
236 config_ui.flight_log_max() > data.log_space() / 1024)
238 JOptionPane.showMessageDialog(owner,
239 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
240 config_ui.flight_log_max(),
241 data.log_space() / 1024),
242 "Maximum Flight Log Too Large",
243 JOptionPane.ERROR_MESSAGE);
247 /* Pull data out of the UI and stuff back into our local data record */
249 data.get_values(config_ui);
250 run_serial_thread(serial_mode_save);
251 } catch (AltosConfigDataException ae) {
252 JOptionPane.showMessageDialog(owner,
254 "Configuration Data Error",
255 JOptionPane.ERROR_MESSAGE);
259 public void actionPerformed(ActionEvent e) {
260 String cmd = e.getActionCommand();
262 if (cmd.equals("Save")) {
264 } else if (cmd.equals("Reset")) {
266 } else if (cmd.equals("Reboot")) {
267 if (serial_line != null)
268 run_serial_thread(serial_mode_reboot);
269 } else if (cmd.equals("Close")) {
270 if (serial_line != null)
273 else if (cmd.equals("Accel")) {
274 if (data.pad_orientation != AltosLib.MISSING) {
275 AltosUIAccelCal accel_ui = new AltosUIAccelCal(owner, serial_line, config_ui);
276 if (accel_ui != null)
280 } catch (InterruptedException ie) {
282 } catch (TimeoutException te) {
287 public AltosConfigFC(JFrame given_owner) {
290 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
291 if (device != null) {
293 serial_line = new AltosSerial(device);
295 if (device.matchProduct(Altos.product_basestation))
298 } catch (InterruptedException ie) {
300 } catch (TimeoutException te) {
303 } catch (FileNotFoundException ee) {
304 JOptionPane.showMessageDialog(owner,
306 "Cannot open target device",
307 JOptionPane.ERROR_MESSAGE);
308 } catch (AltosSerialInUseException si) {
309 JOptionPane.showMessageDialog(owner,
310 String.format("Device \"%s\" already in use",
311 device.toShortString()),
313 JOptionPane.ERROR_MESSAGE);