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_11.*;
27 import org.altusmetrum.altosuilib_11.*;
29 public class AltosConfig 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 AltosConfigUI 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(AltosConfig 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 AltosConfigUI(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() > data.log_space() / 1024) {
236 JOptionPane.showMessageDialog(owner,
237 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
238 config_ui.flight_log_max(),
239 data.log_space() / 1024),
240 "Maximum Flight Log Too Large",
241 JOptionPane.ERROR_MESSAGE);
245 /* Pull data out of the UI and stuff back into our local data record */
247 data.get_values(config_ui);
248 run_serial_thread(serial_mode_save);
249 } catch (AltosConfigDataException ae) {
250 JOptionPane.showMessageDialog(owner,
252 "Configuration Data Error",
253 JOptionPane.ERROR_MESSAGE);
257 public void actionPerformed(ActionEvent e) {
258 String cmd = e.getActionCommand();
260 if (cmd.equals("Save")) {
262 } else if (cmd.equals("Reset")) {
264 } else if (cmd.equals("Reboot")) {
265 if (serial_line != null)
266 run_serial_thread(serial_mode_reboot);
267 } else if (cmd.equals("Close")) {
268 if (serial_line != null)
271 } catch (InterruptedException ie) {
273 } catch (TimeoutException te) {
278 public AltosConfig(JFrame given_owner) {
281 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
282 if (device != null) {
284 serial_line = new AltosSerial(device);
286 if (device.matchProduct(Altos.product_basestation))
289 } catch (InterruptedException ie) {
291 } catch (TimeoutException te) {
294 } catch (FileNotFoundException ee) {
295 JOptionPane.showMessageDialog(owner,
297 "Cannot open target device",
298 JOptionPane.ERROR_MESSAGE);
299 } catch (AltosSerialInUseException si) {
300 JOptionPane.showMessageDialog(owner,
301 String.format("Device \"%s\" already in use",
302 device.toShortString()),
304 JOptionPane.ERROR_MESSAGE);