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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 import org.altusmetrum.AltosLib.*;
35 public class AltosConfigTD implements ActionListener {
43 public void set(int i) {
46 public int_ref(int i) {
57 public void set(String i) {
60 public string_ref(String i) {
67 AltosSerial serial_line;
69 int_ref radio_channel;
70 int_ref radio_calibration;
71 int_ref radio_setting;
72 int_ref radio_frequency;
73 string_ref config_version;
76 AltosConfigTDUI config_ui;
77 boolean serial_started;
80 boolean get_int(String line, String label, int_ref x) {
81 if (line.startsWith(label)) {
83 String tail = line.substring(label.length()).trim();
84 String[] tokens = tail.split("\\s+");
85 if (tokens.length > 0) {
86 int i = Integer.parseInt(tokens[0]);
90 } catch (NumberFormatException ne) {
96 boolean get_string(String line, String label, string_ref s) {
97 if (line.startsWith(label)) {
98 String quoted = line.substring(label.length()).trim();
100 if (quoted.startsWith("\""))
101 quoted = quoted.substring(1);
102 if (quoted.endsWith("\""))
103 quoted = quoted.substring(0,quoted.length()-1);
111 void start_serial() throws InterruptedException, TimeoutException {
112 serial_started = true;
115 void stop_serial() throws InterruptedException {
118 serial_started = false;
122 config_ui.set_serial(serial.get());
123 config_ui.set_product(product.get());
124 config_ui.set_version(version.get());
125 config_ui.set_radio_frequency(frequency());
126 config_ui.set_radio_calibration(radio_calibration.get());
127 config_ui.set_clean();
130 config_ui.make_visible();
134 void process_line(String line) {
139 if (line.equals("all finished")) {
140 if (serial_line != null)
144 get_string(line, "Config version", config_version);
145 get_int(line, "serial-number", serial);
146 get_int(line, "Radio channel:", radio_channel);
147 get_int(line, "Radio cal:", radio_calibration);
148 get_int(line, "Frequency:", radio_frequency);
149 get_int(line, "Radio setting:", radio_setting);
150 get_string(line,"software-version", version);
151 get_string(line,"product", product);
154 final static int serial_mode_read = 0;
155 final static int serial_mode_save = 1;
156 final static int serial_mode_reboot = 2;
158 class SerialData implements Runnable {
159 AltosConfigTD config;
162 void process_line(String line) {
163 config.process_line(line);
165 void callback(String in_line) {
166 final String line = in_line;
167 Runnable r = new Runnable() {
172 SwingUtilities.invokeLater(r);
177 radio_channel.set(0);
178 radio_setting.set(0);
179 radio_frequency.set(0);
180 radio_calibration.set(1186611);
181 config_version.set("0.0");
182 version.set("unknown");
183 product.set("unknown");
188 boolean been_there = false;
189 config.start_serial();
193 config.serial_line.printf("c s\nf\nl\nv\n");
196 String line = config.serial_line.get_reply(5000);
200 if (line.startsWith("software-version"))
202 } catch (Exception e) {
208 if (!config_version.get().equals("0.0"))
211 config.serial_line.printf("C\n ");
212 config.serial_line.flush_input();
214 } catch (InterruptedException ie) {
215 } catch (TimeoutException te) {
219 } catch (InterruptedException ie) {
222 double pref_frequency = AltosPreferences.frequency(serial.get());
223 if (pref_frequency != 0)
224 radio_frequency.set((int) Math.floor (pref_frequency * 1000 + 0.5));
225 callback("all finished");
229 double frequency = frequency();
231 AltosPreferences.set_frequency(serial.get(),
236 switch (serial_mode) {
237 case serial_mode_save:
239 /* fall through ... */
240 case serial_mode_read:
246 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
248 serial_mode = in_serial_mode;
252 void run_serial_thread(int serial_mode) {
253 SerialData sd = new SerialData(this, serial_mode);
254 Thread st = new Thread(sd);
258 void init_ui () throws InterruptedException, TimeoutException {
259 config_ui = new AltosConfigTDUI(owner);
260 config_ui.addActionListener(this);
261 serial_line.set_frame(owner);
268 JOptionPane.showMessageDialog(owner,
269 String.format("Connection to \"%s\" failed",
270 device.toShortString()),
272 JOptionPane.ERROR_MESSAGE);
273 config_ui.setVisible(false);
276 void set_ui() throws InterruptedException, TimeoutException {
277 if (serial_line != null)
278 run_serial_thread(serial_mode_read);
284 return AltosConvert.radio_to_frequency(radio_frequency.get(),
286 radio_calibration.get(),
287 radio_channel.get());
290 void set_frequency(double freq) {
291 int frequency = radio_frequency.get();
292 int setting = radio_setting.get();
295 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
296 } else if (setting > 0) {
297 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
298 radio_calibration.get()));
299 radio_channel.set(0);
301 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
307 set_frequency(config_ui.radio_frequency());
308 run_serial_thread(serial_mode_save);
311 public void actionPerformed(ActionEvent e) {
312 String cmd = e.getActionCommand();
314 if (cmd.equals("Save")) {
316 } else if (cmd.equals("Reset")) {
318 } else if (cmd.equals("Reboot")) {
319 if (serial_line != null)
320 run_serial_thread(serial_mode_reboot);
321 } else if (cmd.equals("Close")) {
322 if (serial_line != null)
325 } catch (InterruptedException ie) {
327 } catch (TimeoutException te) {
332 public AltosConfigTD(JFrame given_owner) {
335 serial = new int_ref(0);
336 radio_channel = new int_ref(0);
337 radio_setting = new int_ref(0);
338 radio_frequency = new int_ref(0);
339 radio_calibration = new int_ref(1186611);
340 config_version = new string_ref("0.0");
341 version = new string_ref("unknown");
342 product = new string_ref("unknown");
344 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
345 if (device != null) {
347 serial_line = new AltosSerial(device);
350 } catch (InterruptedException ie) {
352 } catch (TimeoutException te) {
355 } catch (FileNotFoundException ee) {
356 JOptionPane.showMessageDialog(owner,
358 "Cannot open target device",
359 JOptionPane.ERROR_MESSAGE);
360 } catch (AltosSerialInUseException si) {
361 JOptionPane.showMessageDialog(owner,
362 String.format("Device \"%s\" already in use",
363 device.toShortString()),
365 JOptionPane.ERROR_MESSAGE);
366 } catch (IOException ee) {
367 JOptionPane.showMessageDialog(owner,
368 device.toShortString(),
369 ee.getLocalizedMessage(),
370 JOptionPane.ERROR_MESSAGE);