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 public class AltosConfigTD implements ActionListener {
41 public void set(int i) {
44 public int_ref(int i) {
55 public void set(String i) {
58 public string_ref(String i) {
65 AltosSerial serial_line;
67 int_ref radio_channel;
68 int_ref radio_calibration;
69 int_ref radio_setting;
70 int_ref radio_frequency;
71 string_ref config_version;
74 AltosConfigTDUI config_ui;
75 boolean serial_started;
78 boolean get_int(String line, String label, int_ref x) {
79 if (line.startsWith(label)) {
81 String tail = line.substring(label.length()).trim();
82 String[] tokens = tail.split("\\s+");
83 if (tokens.length > 0) {
84 int i = Integer.parseInt(tokens[0]);
88 } catch (NumberFormatException ne) {
94 boolean get_string(String line, String label, string_ref s) {
95 if (line.startsWith(label)) {
96 String quoted = line.substring(label.length()).trim();
98 if (quoted.startsWith("\""))
99 quoted = quoted.substring(1);
100 if (quoted.endsWith("\""))
101 quoted = quoted.substring(0,quoted.length()-1);
109 void start_serial() throws InterruptedException, TimeoutException {
110 serial_started = true;
113 void stop_serial() throws InterruptedException {
116 serial_started = false;
120 config_ui.set_serial(serial.get());
121 config_ui.set_product(product.get());
122 config_ui.set_version(version.get());
123 config_ui.set_radio_frequency(frequency());
124 config_ui.set_radio_calibration(radio_calibration.get());
125 config_ui.set_clean();
128 config_ui.make_visible();
132 void process_line(String line) {
137 if (line.equals("all finished")) {
138 if (serial_line != null)
142 get_string(line, "Config version", config_version);
143 get_int(line, "serial-number", serial);
144 get_int(line, "Radio channel:", radio_channel);
145 if (get_int(line, "Radio cal:", radio_calibration))
146 System.out.printf("got radio cal %d\n", radio_calibration.get());
147 get_int(line, "Radio setting:", radio_setting);
148 get_string(line,"software-version", version);
149 get_string(line,"product", product);
152 final static int serial_mode_read = 0;
153 final static int serial_mode_save = 1;
154 final static int serial_mode_reboot = 2;
156 class SerialData implements Runnable {
157 AltosConfigTD config;
160 void process_line(String line) {
161 config.process_line(line);
163 void callback(String in_line) {
164 final String line = in_line;
165 Runnable r = new Runnable() {
170 SwingUtilities.invokeLater(r);
175 radio_channel.set(0);
176 radio_setting.set(0);
177 radio_frequency.set(0);
178 radio_calibration.set(1186611);
179 config_version.set("0.0");
180 version.set("unknown");
181 product.set("unknown");
186 boolean been_there = false;
187 config.start_serial();
191 config.serial_line.printf("c s\nf\nl\nv\n");
194 String line = config.serial_line.get_reply(5000);
198 if (line.startsWith("software-version"))
200 } catch (Exception e) {
204 System.out.printf("config_version %s\n", config_version.get());
207 if (!config_version.get().equals("0.0"))
210 config.serial_line.printf("C\n ");
211 config.serial_line.flush_input();
213 } catch (InterruptedException ie) {
214 } catch (TimeoutException te) {
218 } catch (InterruptedException ie) {
221 double pref_frequency = AltosPreferences.frequency(serial.get());
222 if (pref_frequency != 0)
223 radio_frequency.set((int) Math.floor (pref_frequency * 1000 + 0.5));
224 callback("all finished");
228 double frequency = frequency();
230 AltosPreferences.set_frequency(serial.get(),
235 switch (serial_mode) {
236 case serial_mode_save:
238 /* fall through ... */
239 case serial_mode_read:
245 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
247 serial_mode = in_serial_mode;
251 void run_serial_thread(int serial_mode) {
252 SerialData sd = new SerialData(this, serial_mode);
253 Thread st = new Thread(sd);
257 void init_ui () throws InterruptedException, TimeoutException {
258 config_ui = new AltosConfigTDUI(owner);
259 config_ui.addActionListener(this);
260 serial_line.set_frame(owner);
267 JOptionPane.showMessageDialog(owner,
268 String.format("Connection to \"%s\" failed",
269 device.toShortString()),
271 JOptionPane.ERROR_MESSAGE);
272 config_ui.setVisible(false);
275 void set_ui() throws InterruptedException, TimeoutException {
276 if (serial_line != null)
277 run_serial_thread(serial_mode_read);
283 return AltosConvert.radio_to_frequency(radio_frequency.get(),
285 radio_calibration.get(),
286 radio_channel.get());
289 void set_frequency(double freq) {
290 int frequency = radio_frequency.get();
291 int setting = radio_setting.get();
294 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
295 } else if (setting > 0) {
296 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
297 radio_calibration.get()));
298 radio_channel.set(0);
300 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
306 set_frequency(config_ui.radio_frequency());
307 run_serial_thread(serial_mode_save);
310 public void actionPerformed(ActionEvent e) {
311 String cmd = e.getActionCommand();
313 if (cmd.equals("Save")) {
315 } else if (cmd.equals("Reset")) {
317 } else if (cmd.equals("Reboot")) {
318 if (serial_line != null)
319 run_serial_thread(serial_mode_reboot);
320 } else if (cmd.equals("Close")) {
321 if (serial_line != null)
324 } catch (InterruptedException ie) {
326 } catch (TimeoutException te) {
331 public AltosConfigTD(JFrame given_owner) {
334 serial = new int_ref(0);
335 radio_channel = new int_ref(0);
336 radio_setting = new int_ref(0);
337 radio_frequency = new int_ref(0);
338 radio_calibration = new int_ref(1186611);
339 config_version = new string_ref("0.0");
340 version = new string_ref("unknown");
341 product = new string_ref("unknown");
343 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
344 if (device != null) {
346 serial_line = new AltosSerial(device);
349 } catch (InterruptedException ie) {
351 } catch (TimeoutException te) {
354 } catch (FileNotFoundException ee) {
355 JOptionPane.showMessageDialog(owner,
357 "Cannot open target device",
358 JOptionPane.ERROR_MESSAGE);
359 } catch (AltosSerialInUseException si) {
360 JOptionPane.showMessageDialog(owner,
361 String.format("Device \"%s\" already in use",
362 device.toShortString()),
364 JOptionPane.ERROR_MESSAGE);
365 } catch (IOException ee) {
366 JOptionPane.showMessageDialog(owner,
367 device.toShortString(),
368 ee.getLocalizedMessage(),
369 JOptionPane.ERROR_MESSAGE);