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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 import java.awt.event.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.AltosLib.*;
27 public class AltosConfig implements ActionListener {
35 public void set(int i) {
38 public int_ref(int i) {
49 public void set(String i) {
52 public string_ref(String i) {
59 AltosSerial serial_line;
63 AltosConfigUI config_ui;
64 boolean serial_started;
67 void start_serial() throws InterruptedException, TimeoutException {
68 serial_started = true;
70 serial_line.start_remote();
73 void stop_serial() throws InterruptedException {
76 serial_started = false;
78 serial_line.stop_remote();
82 data.set_values(config_ui);
83 config_ui.set_clean();
86 config_ui.make_visible();
92 final static int serial_mode_read = 0;
93 final static int serial_mode_save = 1;
94 final static int serial_mode_reboot = 2;
96 class SerialData implements Runnable {
100 void callback(String in_cmd) {
101 final String cmd = in_cmd;
102 Runnable r = new Runnable() {
104 if (cmd.equals("abort")) {
106 } else if (cmd.equals("all finished")) {
107 if (serial_line != null)
112 SwingUtilities.invokeLater(r);
119 data = new AltosConfigData(config.serial_line);
120 } catch (InterruptedException ie) {
121 } catch (TimeoutException te) {
125 } catch (InterruptedException ie) {
130 } catch (InterruptedException ie) {
133 callback("all finished");
139 data.save(serial_line, remote);
141 AltosUIPreferences.set_frequency(device.getSerial(),
143 } catch (InterruptedException ie) {
144 } catch (TimeoutException te) {
148 } catch (InterruptedException ie) {
156 serial_line.printf("r eboot\n");
157 serial_line.flush_output();
158 } catch (InterruptedException ie) {
159 } catch (TimeoutException te) {
163 } catch (InterruptedException ie) {
170 switch (serial_mode) {
171 case serial_mode_save:
173 /* fall through ... */
174 case serial_mode_read:
177 case serial_mode_reboot:
183 public SerialData(AltosConfig in_config, int in_serial_mode) {
185 serial_mode = in_serial_mode;
189 void run_serial_thread(int serial_mode) {
190 SerialData sd = new SerialData(this, serial_mode);
191 Thread st = new Thread(sd);
195 void init_ui () throws InterruptedException, TimeoutException {
196 config_ui = new AltosConfigUI(owner, remote);
197 config_ui.addActionListener(this);
198 serial_line.set_frame(owner);
203 if (serial_line != null) {
207 JOptionPane.showMessageDialog(owner,
208 String.format("Connection to \"%s\" failed",
209 device.toShortString()),
211 JOptionPane.ERROR_MESSAGE);
212 config_ui.setVisible(false);
215 void set_ui() throws InterruptedException, TimeoutException {
216 if (serial_line != null)
217 run_serial_thread(serial_mode_read);
223 return AltosConvert.radio_to_frequency(data.radio_frequency,
225 data.radio_calibration,
231 /* bounds check stuff */
232 if (config_ui.flight_log_max() > data.log_limit()) {
233 JOptionPane.showMessageDialog(owner,
234 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
235 config_ui.flight_log_max(),
237 "Maximum Flight Log Too Large",
238 JOptionPane.ERROR_MESSAGE);
242 /* Pull data out of the UI and stuff back into our local data record */
244 data.get_values(config_ui);
246 run_serial_thread(serial_mode_save);
249 public void actionPerformed(ActionEvent e) {
250 String cmd = e.getActionCommand();
252 if (cmd.equals("Save")) {
254 } else if (cmd.equals("Reset")) {
256 } else if (cmd.equals("Reboot")) {
257 if (serial_line != null)
258 run_serial_thread(serial_mode_reboot);
259 } else if (cmd.equals("Close")) {
260 if (serial_line != null)
263 } catch (InterruptedException ie) {
265 } catch (TimeoutException te) {
270 public AltosConfig(JFrame given_owner) {
273 device = AltosDeviceDialog.show(owner, Altos.product_any);
274 if (device != null) {
276 serial_line = new AltosSerial(device);
278 if (device.matchProduct(Altos.product_basestation))
281 } catch (InterruptedException ie) {
283 } catch (TimeoutException te) {
286 } catch (FileNotFoundException ee) {
287 JOptionPane.showMessageDialog(owner,
289 "Cannot open target device",
290 JOptionPane.ERROR_MESSAGE);
291 } catch (AltosSerialInUseException si) {
292 JOptionPane.showMessageDialog(owner,
293 String.format("Device \"%s\" already in use",
294 device.toShortString()),
296 JOptionPane.ERROR_MESSAGE);