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.
18 package org.altusmetrum.telegps;
20 import java.awt.event.*;
23 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_5.*;
26 import org.altusmetrum.altosuilib_3.*;
28 public class TeleGPSConfig implements ActionListener {
36 public void set(int i) {
39 public int_ref(int i) {
50 public void set(String i) {
53 public string_ref(String i) {
60 AltosSerial serial_line;
63 TeleGPSConfigUI config_ui;
64 boolean serial_started;
67 void start_serial() throws InterruptedException, TimeoutException {
68 serial_started = true;
71 void stop_serial() throws InterruptedException {
74 serial_started = false;
78 data.set_values(config_ui);
79 config_ui.set_clean();
82 config_ui.make_visible();
88 final static int serial_mode_read = 0;
89 final static int serial_mode_save = 1;
90 final static int serial_mode_reboot = 2;
92 class SerialData implements Runnable {
96 void callback(String in_cmd) {
97 final String cmd = in_cmd;
98 Runnable r = new Runnable() {
100 if (cmd.equals("abort")) {
102 } else if (cmd.equals("all finished")) {
103 if (serial_line != null)
108 SwingUtilities.invokeLater(r);
115 data = new AltosConfigData(config.serial_line);
116 } catch (InterruptedException ie) {
117 } catch (TimeoutException te) {
121 } catch (InterruptedException ie) {
126 } catch (InterruptedException ie) {
129 callback("all finished");
135 data.save(serial_line, false);
136 } catch (InterruptedException ie) {
137 } catch (TimeoutException te) {
141 } catch (InterruptedException ie) {
149 serial_line.printf("r eboot\n");
150 serial_line.flush_output();
151 } catch (InterruptedException ie) {
152 } catch (TimeoutException te) {
157 } catch (InterruptedException ie) {
163 switch (serial_mode) {
164 case serial_mode_save:
166 /* fall through ... */
167 case serial_mode_read:
170 case serial_mode_reboot:
176 public SerialData(TeleGPSConfig in_config, int in_serial_mode) {
178 serial_mode = in_serial_mode;
182 void run_serial_thread(int serial_mode) {
183 SerialData sd = new SerialData(this, serial_mode);
184 Thread st = new Thread(sd);
188 void init_ui () throws InterruptedException, TimeoutException {
189 config_ui = new TeleGPSConfigUI(owner);
190 config_ui.addActionListener(this);
191 serial_line.set_frame(owner);
196 if (serial_line != null) {
200 JOptionPane.showMessageDialog(owner,
201 String.format("Connection to \"%s\" failed",
202 device.toShortString()),
204 JOptionPane.ERROR_MESSAGE);
205 config_ui.setVisible(false);
208 void set_ui() throws InterruptedException, TimeoutException {
209 if (serial_line != null)
210 run_serial_thread(serial_mode_read);
216 return AltosConvert.radio_to_frequency(data.radio_frequency,
218 data.radio_calibration,
225 /* bounds check stuff */
226 if (config_ui.flight_log_max() > data.log_space()/1024) {
227 JOptionPane.showMessageDialog(owner,
228 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
229 config_ui.flight_log_max(),
230 data.log_space()/1024),
231 "Maximum Flight Log Too Large",
232 JOptionPane.ERROR_MESSAGE);
236 /* Pull data out of the UI and stuff back into our local data record */
238 data.get_values(config_ui);
239 run_serial_thread(serial_mode_save);
240 } catch (AltosConfigDataException ae) {
241 JOptionPane.showMessageDialog(owner,
243 "Configuration Data Error",
244 JOptionPane.ERROR_MESSAGE);
248 public void actionPerformed(ActionEvent e) {
249 String cmd = e.getActionCommand();
251 if (cmd.equals("Save")) {
253 } else if (cmd.equals("Reset")) {
255 } else if (cmd.equals("Reboot")) {
256 if (serial_line != null)
257 run_serial_thread(serial_mode_reboot);
258 } else if (cmd.equals("Close")) {
259 if (serial_line != null)
262 } catch (InterruptedException ie) {
264 } catch (TimeoutException te) {
269 public TeleGPSConfig(JFrame given_owner) {
272 device = AltosDeviceUIDialog.show(owner, AltosLib.product_telegps);
273 if (device != null) {
275 serial_line = new AltosSerial(device);
278 } catch (InterruptedException ie) {
280 } catch (TimeoutException te) {
283 } catch (FileNotFoundException ee) {
284 JOptionPane.showMessageDialog(owner,
286 "Cannot open target device",
287 JOptionPane.ERROR_MESSAGE);
288 } catch (AltosSerialInUseException si) {
289 JOptionPane.showMessageDialog(owner,
290 String.format("Device \"%s\" already in use",
291 device.toShortString()),
293 JOptionPane.ERROR_MESSAGE);