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.
19 package org.altusmetrum.telegps;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_12.*;
27 import org.altusmetrum.altosuilib_12.*;
29 public class TeleGPSConfig 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;
64 TeleGPSConfigUI config_ui;
65 boolean serial_started;
68 void start_serial() throws InterruptedException, TimeoutException {
69 serial_started = true;
72 void stop_serial() throws InterruptedException {
75 serial_started = false;
79 data.set_values(config_ui);
80 config_ui.set_clean();
83 config_ui.make_visible();
89 final static int serial_mode_read = 0;
90 final static int serial_mode_save = 1;
91 final static int serial_mode_reboot = 2;
93 class SerialData implements Runnable {
97 void callback(String in_cmd) {
98 final String cmd = in_cmd;
99 Runnable r = new Runnable() {
101 if (cmd.equals("abort")) {
103 } else if (cmd.equals("all finished")) {
104 if (serial_line != null)
109 SwingUtilities.invokeLater(r);
116 data = new AltosConfigData(config.serial_line);
117 } catch (InterruptedException ie) {
118 } catch (TimeoutException te) {
122 } catch (InterruptedException ie) {
127 } catch (InterruptedException ie) {
130 callback("all finished");
136 data.save(serial_line, false);
137 } catch (InterruptedException ie) {
138 } catch (TimeoutException te) {
142 } catch (InterruptedException ie) {
150 serial_line.printf("r eboot\n");
151 serial_line.flush_output();
152 } catch (InterruptedException ie) {
153 } catch (TimeoutException te) {
158 } catch (InterruptedException ie) {
164 switch (serial_mode) {
165 case serial_mode_save:
167 /* fall through ... */
168 case serial_mode_read:
171 case serial_mode_reboot:
177 public SerialData(TeleGPSConfig in_config, int in_serial_mode) {
179 serial_mode = in_serial_mode;
183 void run_serial_thread(int serial_mode) {
184 SerialData sd = new SerialData(this, serial_mode);
185 Thread st = new Thread(sd);
189 void init_ui () throws InterruptedException, TimeoutException {
190 config_ui = new TeleGPSConfigUI(owner);
191 config_ui.addActionListener(this);
192 serial_line.set_frame(owner);
197 if (serial_line != null) {
201 JOptionPane.showMessageDialog(owner,
202 String.format("Connection to \"%s\" failed",
203 device.toShortString()),
205 JOptionPane.ERROR_MESSAGE);
206 config_ui.setVisible(false);
209 void set_ui() throws InterruptedException, TimeoutException {
210 if (serial_line != null)
211 run_serial_thread(serial_mode_read);
217 return AltosConvert.radio_to_frequency(data.radio_frequency,
219 data.radio_calibration,
226 /* bounds check stuff */
227 if (config_ui.flight_log_max() > data.log_space()/1024) {
228 JOptionPane.showMessageDialog(owner,
229 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
230 config_ui.flight_log_max(),
231 data.log_space()/1024),
232 "Maximum Flight Log Too Large",
233 JOptionPane.ERROR_MESSAGE);
237 /* Pull data out of the UI and stuff back into our local data record */
239 data.get_values(config_ui);
240 run_serial_thread(serial_mode_save);
241 } catch (AltosConfigDataException ae) {
242 JOptionPane.showMessageDialog(owner,
244 "Configuration Data Error",
245 JOptionPane.ERROR_MESSAGE);
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 TeleGPSConfig(JFrame given_owner) {
273 device = AltosDeviceUIDialog.show(owner, AltosLib.product_telegps);
274 if (device != null) {
276 serial_line = new AltosSerial(device);
279 } catch (InterruptedException ie) {
281 } catch (TimeoutException te) {
284 } catch (FileNotFoundException ee) {
285 JOptionPane.showMessageDialog(owner,
287 "Cannot open target device",
288 JOptionPane.ERROR_MESSAGE);
289 } catch (AltosSerialInUseException si) {
290 JOptionPane.showMessageDialog(owner,
291 String.format("Device \"%s\" already in use",
292 device.toShortString()),
294 JOptionPane.ERROR_MESSAGE);