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.*;
25 import org.altusmetrum.altoslib_4.*;
26 import org.altusmetrum.altosuilib_2.*;
28 public class AltosConfig 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;
64 AltosConfigUI config_ui;
65 boolean serial_started;
68 void start_serial() throws InterruptedException, TimeoutException {
69 serial_started = true;
71 serial_line.start_remote();
74 void stop_serial() throws InterruptedException {
77 serial_started = false;
79 serial_line.stop_remote();
83 data.set_values(config_ui);
84 config_ui.set_clean();
87 config_ui.make_visible();
93 final static int serial_mode_read = 0;
94 final static int serial_mode_save = 1;
95 final static int serial_mode_reboot = 2;
97 class SerialData implements Runnable {
101 void callback(String in_cmd) {
102 final String cmd = in_cmd;
103 Runnable r = new Runnable() {
105 if (cmd.equals("abort")) {
107 } else if (cmd.equals("all finished")) {
108 if (serial_line != null)
113 SwingUtilities.invokeLater(r);
120 data = new AltosConfigData(config.serial_line);
121 } catch (InterruptedException ie) {
122 } catch (TimeoutException te) {
126 } catch (InterruptedException ie) {
131 } catch (InterruptedException ie) {
134 callback("all finished");
140 data.save(serial_line, remote);
142 AltosUIPreferences.set_frequency(device.getSerial(),
144 } catch (InterruptedException ie) {
145 } catch (TimeoutException te) {
149 } catch (InterruptedException ie) {
157 serial_line.printf("r eboot\n");
158 serial_line.flush_output();
159 } catch (InterruptedException ie) {
160 } catch (TimeoutException te) {
165 } catch (InterruptedException ie) {
171 switch (serial_mode) {
172 case serial_mode_save:
174 /* fall through ... */
175 case serial_mode_read:
178 case serial_mode_reboot:
184 public SerialData(AltosConfig in_config, int in_serial_mode) {
186 serial_mode = in_serial_mode;
190 void run_serial_thread(int serial_mode) {
191 SerialData sd = new SerialData(this, serial_mode);
192 Thread st = new Thread(sd);
196 void init_ui () throws InterruptedException, TimeoutException {
197 config_ui = new AltosConfigUI(owner, remote);
198 config_ui.addActionListener(this);
199 serial_line.set_frame(owner);
204 if (serial_line != null) {
208 JOptionPane.showMessageDialog(owner,
209 String.format("Connection to \"%s\" failed",
210 device.toShortString()),
212 JOptionPane.ERROR_MESSAGE);
213 config_ui.setVisible(false);
216 void set_ui() throws InterruptedException, TimeoutException {
217 if (serial_line != null)
218 run_serial_thread(serial_mode_read);
224 return AltosConvert.radio_to_frequency(data.radio_frequency,
226 data.radio_calibration,
232 /* bounds check stuff */
233 if (config_ui.flight_log_max() > data.log_limit()) {
234 JOptionPane.showMessageDialog(owner,
235 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
236 config_ui.flight_log_max(),
238 "Maximum Flight Log Too Large",
239 JOptionPane.ERROR_MESSAGE);
243 /* Pull data out of the UI and stuff back into our local data record */
246 data.get_values(config_ui);
247 run_serial_thread(serial_mode_save);
248 } catch (AltosConfigDataException ae) {
249 JOptionPane.showMessageDialog(owner,
251 "Configuration Data Error",
252 JOptionPane.ERROR_MESSAGE);
256 public void actionPerformed(ActionEvent e) {
257 String cmd = e.getActionCommand();
259 if (cmd.equals("Save")) {
261 } else if (cmd.equals("Reset")) {
263 } else if (cmd.equals("Reboot")) {
264 if (serial_line != null)
265 run_serial_thread(serial_mode_reboot);
266 } else if (cmd.equals("Close")) {
267 if (serial_line != null)
270 } catch (InterruptedException ie) {
272 } catch (TimeoutException te) {
277 public AltosConfig(JFrame given_owner) {
280 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
281 if (device != null) {
283 serial_line = new AltosSerial(device);
285 if (device.matchProduct(Altos.product_basestation))
288 } catch (InterruptedException ie) {
290 } catch (TimeoutException te) {
293 } catch (FileNotFoundException ee) {
294 JOptionPane.showMessageDialog(owner,
296 "Cannot open target device",
297 JOptionPane.ERROR_MESSAGE);
298 } catch (AltosSerialInUseException si) {
299 JOptionPane.showMessageDialog(owner,
300 String.format("Device \"%s\" already in use",
301 device.toShortString()),
303 JOptionPane.ERROR_MESSAGE);