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.*;
24 import org.altusmetrum.altoslib_1.*;
25 import org.altusmetrum.altosuilib_1.*;
27 public class AltosConfigTD implements ActionListener {
35 public void set(int i) {
38 public int_ref(int i) {
49 public void set(String i) {
52 public string_ref(String i) {
59 AltosSerial serial_line;
61 int_ref radio_channel;
62 int_ref radio_calibration;
63 int_ref radio_setting;
64 int_ref radio_frequency;
65 string_ref config_version;
68 AltosConfigTDUI config_ui;
71 boolean get_int(String line, String label, int_ref x) {
72 if (line.startsWith(label)) {
74 String tail = line.substring(label.length()).trim();
75 String[] tokens = tail.split("\\s+");
76 if (tokens.length > 0) {
77 int i = Integer.parseInt(tokens[0]);
81 } catch (NumberFormatException ne) {
87 boolean get_string(String line, String label, string_ref s) {
88 if (line.startsWith(label)) {
89 String quoted = line.substring(label.length()).trim();
91 if (quoted.startsWith("\""))
92 quoted = quoted.substring(1);
93 if (quoted.endsWith("\""))
94 quoted = quoted.substring(0,quoted.length()-1);
102 synchronized void update_ui() {
103 config_ui.set_serial(serial.get());
104 config_ui.set_product(product.get());
105 config_ui.set_version(version.get());
106 config_ui.set_radio_frequency(frequency());
107 config_ui.set_radio_calibration(radio_calibration.get());
108 config_ui.set_clean();
111 config_ui.make_visible();
115 void finish_input(String line) {
120 if (line.equals("all finished")) {
121 if (serial_line != null)
127 synchronized void process_line(String line) {
128 if (line == null || line.equals("all finished")) {
129 final String last_line = line;
130 Runnable r = new Runnable() {
132 finish_input(last_line);
135 SwingUtilities.invokeLater(r);
137 get_string(line, "Config version", config_version);
138 get_int(line, "serial-number", serial);
139 get_int(line, "Radio channel:", radio_channel);
140 get_int(line, "Radio cal:", radio_calibration);
141 get_int(line, "Frequency:", radio_frequency);
142 get_int(line, "Radio setting:", radio_setting);
143 get_string(line,"software-version", version);
144 get_string(line,"product", product);
148 synchronized void reset_data() {
150 radio_channel.set(0);
151 radio_setting.set(0);
152 radio_frequency.set(0);
153 radio_calibration.set(1186611);
154 config_version.set("0.0");
155 version.set("unknown");
156 product.set("unknown");
159 synchronized double frequency() {
160 return AltosConvert.radio_to_frequency(radio_frequency.get(),
162 radio_calibration.get(),
163 radio_channel.get());
166 synchronized void set_frequency(double freq) {
167 int frequency = radio_frequency.get();
168 int setting = radio_setting.get();
171 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
172 } else if (setting > 0) {
173 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
174 radio_calibration.get()));
175 radio_channel.set(0);
177 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
181 final static int serial_mode_read = 0;
182 final static int serial_mode_save = 1;
183 final static int serial_mode_reboot = 2;
185 class SerialData implements Runnable {
186 AltosConfigTD config;
191 boolean been_there = false;
195 config.serial_line.printf("c s\nf\nv\n");
198 String line = config.serial_line.get_reply(5000);
199 config.process_line(line);
200 if (line != null && 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) {
217 * This makes sure the displayed frequency respects the limits that the
218 * available firmware version might place on the actual frequency
220 config.set_frequency(AltosPreferences.frequency(serial.get()));
221 config.process_line("all finished");
225 double frequency = frequency();
227 AltosPreferences.set_frequency(serial.get(),
232 switch (serial_mode) {
233 case serial_mode_save:
235 /* fall through ... */
236 case serial_mode_read:
242 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
244 serial_mode = in_serial_mode;
248 void run_serial_thread(int serial_mode) {
249 SerialData sd = new SerialData(this, serial_mode);
250 Thread st = new Thread(sd);
254 void init_ui () throws InterruptedException, TimeoutException {
255 config_ui = new AltosConfigTDUI(owner);
256 config_ui.addActionListener(this);
257 serial_line.set_frame(owner);
264 JOptionPane.showMessageDialog(owner,
265 String.format("Connection to \"%s\" failed",
266 device.toShortString()),
268 JOptionPane.ERROR_MESSAGE);
269 config_ui.setVisible(false);
272 void set_ui() throws InterruptedException, TimeoutException {
273 if (serial_line != null)
274 run_serial_thread(serial_mode_read);
280 double freq = config_ui.radio_frequency();
282 run_serial_thread(serial_mode_save);
285 public void actionPerformed(ActionEvent e) {
286 String cmd = e.getActionCommand();
288 if (cmd.equals("Save")) {
290 } else if (cmd.equals("Reset")) {
292 } else if (cmd.equals("Reboot")) {
293 if (serial_line != null)
294 run_serial_thread(serial_mode_reboot);
295 } else if (cmd.equals("Close")) {
296 if (serial_line != null)
299 } catch (InterruptedException ie) {
301 } catch (TimeoutException te) {
306 public AltosConfigTD(JFrame given_owner) {
309 serial = new int_ref(0);
310 radio_channel = new int_ref(0);
311 radio_setting = new int_ref(0);
312 radio_frequency = new int_ref(0);
313 radio_calibration = new int_ref(1186611);
314 config_version = new string_ref("0.0");
315 version = new string_ref("unknown");
316 product = new string_ref("unknown");
318 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
319 if (device != null) {
321 serial_line = new AltosSerial(device);
324 } catch (InterruptedException ie) {
326 } catch (TimeoutException te) {
329 } catch (FileNotFoundException ee) {
330 JOptionPane.showMessageDialog(owner,
332 "Cannot open target device",
333 JOptionPane.ERROR_MESSAGE);
334 } catch (AltosSerialInUseException si) {
335 JOptionPane.showMessageDialog(owner,
336 String.format("Device \"%s\" already in use",
337 device.toShortString()),
339 JOptionPane.ERROR_MESSAGE);