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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.event.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_13.*;
26 import org.altusmetrum.altosuilib_13.*;
28 public class AltosConfigTD 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;
62 int_ref radio_channel;
63 int_ref radio_calibration;
64 int_ref radio_setting;
65 int_ref radio_frequency;
66 int_ref telemetry_rate;
67 string_ref config_version;
70 AltosConfigTDUI config_ui;
73 boolean get_int(String line, String label, int_ref x) {
74 if (line.startsWith(label)) {
76 String tail = line.substring(label.length()).trim();
77 String[] tokens = tail.split("\\s+");
78 if (tokens.length > 0) {
79 int i = Integer.parseInt(tokens[0]);
83 } catch (NumberFormatException ne) {
89 boolean get_string(String line, String label, string_ref s) {
90 if (line.startsWith(label)) {
91 String quoted = line.substring(label.length()).trim();
93 if (quoted.startsWith("\""))
94 quoted = quoted.substring(1);
95 if (quoted.endsWith("\""))
96 quoted = quoted.substring(0,quoted.length()-1);
104 synchronized void update_ui() {
105 config_ui.set_serial(serial.get());
106 config_ui.set_product(product.get());
107 config_ui.set_version(version.get());
108 config_ui.set_radio_frequency(frequency());
109 config_ui.set_radio_calibration(radio_calibration.get());
110 config_ui.set_telemetry_rate(telemetry_rate.get());
111 config_ui.set_clean();
114 config_ui.make_visible();
118 void finish_input(String line) {
123 if (line.equals("all finished")) {
124 if (serial_line != null)
130 synchronized void process_line(String line) {
131 if (line == null || line.equals("all finished")) {
132 final String last_line = line;
133 Runnable r = new Runnable() {
135 finish_input(last_line);
138 SwingUtilities.invokeLater(r);
140 get_string(line, "Config version", config_version);
141 get_int(line, "serial-number", serial);
142 get_int(line, "Radio channel:", radio_channel);
143 get_int(line, "Radio cal:", radio_calibration);
144 get_int(line, "Frequency:", radio_frequency);
145 get_int(line, "Radio setting:", radio_setting);
146 get_int(line, "Telemetry rate:", telemetry_rate);
147 get_string(line,"software-version", version);
148 get_string(line,"product", product);
152 synchronized void reset_data() {
154 radio_channel.set(0);
155 radio_setting.set(0);
156 radio_frequency.set(0);
157 radio_calibration.set(1186611);
158 telemetry_rate.set(Altos.ao_telemetry_rate_38400);
159 config_version.set("0.0");
160 version.set("unknown");
161 product.set("unknown");
164 synchronized double frequency() {
165 return AltosConvert.radio_to_frequency(radio_frequency.get(),
167 radio_calibration.get(),
168 radio_channel.get());
171 synchronized void set_frequency(double freq) {
172 int frequency = radio_frequency.get();
173 int setting = radio_setting.get();
176 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
177 } else if (setting > 0) {
178 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
179 radio_calibration.get()));
180 radio_channel.set(0);
182 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
186 synchronized int telemetry_rate() {
187 return telemetry_rate.get();
190 synchronized void set_telemetry_rate(int new_telemetry_rate){
191 int rate = telemetry_rate.get();
194 telemetry_rate.set(new_telemetry_rate);
197 final static int serial_mode_read = 0;
198 final static int serial_mode_save = 1;
199 final static int serial_mode_reboot = 2;
201 SerialData serial_data;
202 Thread serial_thread;
204 class SerialData implements Runnable {
205 AltosConfigTD config;
210 boolean been_there = false;
213 while (config.serial_line != null) {
214 config.serial_line.printf("c s\nf\nv\n");
215 while (config.serial_line != null) {
217 String line = config.serial_line.get_reply(5000);
218 config.process_line(line);
219 if (line != null && line.startsWith("software-version"))
221 } catch (Exception e) {
227 if (!config_version.get().equals("0.0"))
230 if (config != null && config.serial_line != null) {
231 config.serial_line.printf("C\n ");
232 config.serial_line.flush_input();
235 } catch (InterruptedException ie) {
238 * This makes sure the displayed frequency respects the limits that the
239 * available firmware version might place on the actual frequency
241 config.set_frequency(AltosPreferences.frequency(serial.get()));
242 config.set_telemetry_rate(AltosPreferences.telemetry_rate(serial.get()));
243 config.process_line("all finished");
247 double frequency = frequency();
249 AltosPreferences.set_frequency(serial.get(),
251 AltosPreferences.set_telemetry_rate(serial.get(),
256 switch (serial_mode) {
257 case serial_mode_save:
259 /* fall through ... */
260 case serial_mode_read:
262 serial_thread = null;
267 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
269 serial_mode = in_serial_mode;
273 void run_serial_thread(int serial_mode) {
274 serial_data = new SerialData(this, serial_mode);
275 serial_thread = new Thread(serial_data);
276 serial_thread.start();
279 void abort_serial_thread() {
280 if (serial_thread != null) {
281 serial_thread.interrupt();
282 serial_thread = null;
285 void init_ui () throws InterruptedException, TimeoutException {
286 config_ui = new AltosConfigTDUI(owner);
287 config_ui.addActionListener(this);
288 serial_line.set_frame(owner);
293 abort_serial_thread();
294 if (serial_line != null) {
298 JOptionPane.showMessageDialog(owner,
299 String.format("Connection to \"%s\" failed",
300 device.toShortString()),
302 JOptionPane.ERROR_MESSAGE);
303 config_ui.setVisible(false);
306 void set_ui() throws InterruptedException, TimeoutException {
307 if (serial_line != null)
308 run_serial_thread(serial_mode_read);
314 double freq = config_ui.radio_frequency();
316 int telemetry_rate = config_ui.telemetry_rate();
317 set_telemetry_rate(telemetry_rate);
318 run_serial_thread(serial_mode_save);
321 public void actionPerformed(ActionEvent e) {
322 String cmd = e.getActionCommand();
324 if (cmd.equals("Save")) {
326 } else if (cmd.equals("Reset")) {
328 } else if (cmd.equals("Reboot")) {
329 if (serial_line != null)
330 run_serial_thread(serial_mode_reboot);
331 } else if (cmd.equals("Close")) {
332 if (serial_line != null)
335 } catch (InterruptedException ie) {
337 } catch (TimeoutException te) {
342 public AltosConfigTD(JFrame given_owner) {
345 serial = new int_ref(0);
346 radio_channel = new int_ref(0);
347 radio_setting = new int_ref(0);
348 radio_frequency = new int_ref(0);
349 radio_calibration = new int_ref(1186611);
350 telemetry_rate = new int_ref(AltosLib.ao_telemetry_rate_38400);
351 config_version = new string_ref("0.0");
352 version = new string_ref("unknown");
353 product = new string_ref("unknown");
355 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
356 if (device != null) {
358 serial_line = new AltosSerial(device);
361 } catch (InterruptedException ie) {
363 } catch (TimeoutException te) {
366 } catch (FileNotFoundException ee) {
367 JOptionPane.showMessageDialog(owner,
369 "Cannot open target device",
370 JOptionPane.ERROR_MESSAGE);
371 } catch (AltosSerialInUseException si) {
372 JOptionPane.showMessageDialog(owner,
373 String.format("Device \"%s\" already in use",
374 device.toShortString()),
376 JOptionPane.ERROR_MESSAGE);