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_8.*;
25 import org.altusmetrum.altosuilib_8.*;
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 int_ref telemetry_rate;
66 string_ref config_version;
69 AltosConfigTDUI config_ui;
72 boolean get_int(String line, String label, int_ref x) {
73 if (line.startsWith(label)) {
75 String tail = line.substring(label.length()).trim();
76 String[] tokens = tail.split("\\s+");
77 if (tokens.length > 0) {
78 int i = Integer.parseInt(tokens[0]);
82 } catch (NumberFormatException ne) {
88 boolean get_string(String line, String label, string_ref s) {
89 if (line.startsWith(label)) {
90 String quoted = line.substring(label.length()).trim();
92 if (quoted.startsWith("\""))
93 quoted = quoted.substring(1);
94 if (quoted.endsWith("\""))
95 quoted = quoted.substring(0,quoted.length()-1);
103 synchronized void update_ui() {
104 config_ui.set_serial(serial.get());
105 config_ui.set_product(product.get());
106 config_ui.set_version(version.get());
107 config_ui.set_radio_frequency(frequency());
108 config_ui.set_radio_calibration(radio_calibration.get());
109 config_ui.set_telemetry_rate(telemetry_rate.get());
110 config_ui.set_clean();
113 config_ui.make_visible();
117 void finish_input(String line) {
122 if (line.equals("all finished")) {
123 if (serial_line != null)
129 synchronized void process_line(String line) {
130 if (line == null || line.equals("all finished")) {
131 final String last_line = line;
132 Runnable r = new Runnable() {
134 finish_input(last_line);
137 SwingUtilities.invokeLater(r);
139 get_string(line, "Config version", config_version);
140 get_int(line, "serial-number", serial);
141 get_int(line, "Radio channel:", radio_channel);
142 get_int(line, "Radio cal:", radio_calibration);
143 get_int(line, "Frequency:", radio_frequency);
144 get_int(line, "Radio setting:", radio_setting);
145 get_int(line, "Telemetry rate:", telemetry_rate);
146 get_string(line,"software-version", version);
147 get_string(line,"product", product);
151 synchronized void reset_data() {
153 radio_channel.set(0);
154 radio_setting.set(0);
155 radio_frequency.set(0);
156 radio_calibration.set(1186611);
157 telemetry_rate.set(Altos.ao_telemetry_rate_38400);
158 config_version.set("0.0");
159 version.set("unknown");
160 product.set("unknown");
163 synchronized double frequency() {
164 return AltosConvert.radio_to_frequency(radio_frequency.get(),
166 radio_calibration.get(),
167 radio_channel.get());
170 synchronized void set_frequency(double freq) {
171 int frequency = radio_frequency.get();
172 int setting = radio_setting.get();
175 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
176 } else if (setting > 0) {
177 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
178 radio_calibration.get()));
179 radio_channel.set(0);
181 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
185 synchronized int telemetry_rate() {
186 return telemetry_rate.get();
189 synchronized void set_telemetry_rate(int new_telemetry_rate){
190 int rate = telemetry_rate.get();
193 telemetry_rate.set(new_telemetry_rate);
196 final static int serial_mode_read = 0;
197 final static int serial_mode_save = 1;
198 final static int serial_mode_reboot = 2;
200 class SerialData implements Runnable {
201 AltosConfigTD config;
206 boolean been_there = false;
210 config.serial_line.printf("c s\nf\nv\n");
213 String line = config.serial_line.get_reply(5000);
214 config.process_line(line);
215 if (line != null && line.startsWith("software-version"))
217 } catch (Exception e) {
223 if (!config_version.get().equals("0.0"))
226 if (config != null && config.serial_line != null) {
227 config.serial_line.printf("C\n ");
228 config.serial_line.flush_input();
231 } catch (InterruptedException ie) {
234 * This makes sure the displayed frequency respects the limits that the
235 * available firmware version might place on the actual frequency
237 config.set_frequency(AltosPreferences.frequency(serial.get()));
238 config.set_telemetry_rate(AltosPreferences.telemetry_rate(serial.get()));
239 config.process_line("all finished");
243 double frequency = frequency();
245 AltosPreferences.set_frequency(serial.get(),
247 AltosPreferences.set_telemetry_rate(serial.get(),
252 switch (serial_mode) {
253 case serial_mode_save:
255 /* fall through ... */
256 case serial_mode_read:
262 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
264 serial_mode = in_serial_mode;
268 void run_serial_thread(int serial_mode) {
269 SerialData sd = new SerialData(this, serial_mode);
270 Thread st = new Thread(sd);
274 void init_ui () throws InterruptedException, TimeoutException {
275 config_ui = new AltosConfigTDUI(owner);
276 config_ui.addActionListener(this);
277 serial_line.set_frame(owner);
282 if (serial_line != null) {
286 JOptionPane.showMessageDialog(owner,
287 String.format("Connection to \"%s\" failed",
288 device.toShortString()),
290 JOptionPane.ERROR_MESSAGE);
291 config_ui.setVisible(false);
294 void set_ui() throws InterruptedException, TimeoutException {
295 if (serial_line != null)
296 run_serial_thread(serial_mode_read);
302 double freq = config_ui.radio_frequency();
304 int telemetry_rate = config_ui.telemetry_rate();
305 set_telemetry_rate(telemetry_rate);
306 run_serial_thread(serial_mode_save);
309 public void actionPerformed(ActionEvent e) {
310 String cmd = e.getActionCommand();
312 if (cmd.equals("Save")) {
314 } else if (cmd.equals("Reset")) {
316 } else if (cmd.equals("Reboot")) {
317 if (serial_line != null)
318 run_serial_thread(serial_mode_reboot);
319 } else if (cmd.equals("Close")) {
320 if (serial_line != null)
323 } catch (InterruptedException ie) {
325 } catch (TimeoutException te) {
330 public AltosConfigTD(JFrame given_owner) {
333 serial = new int_ref(0);
334 radio_channel = new int_ref(0);
335 radio_setting = new int_ref(0);
336 radio_frequency = new int_ref(0);
337 radio_calibration = new int_ref(1186611);
338 telemetry_rate = new int_ref(AltosLib.ao_telemetry_rate_38400);
339 config_version = new string_ref("0.0");
340 version = new string_ref("unknown");
341 product = new string_ref("unknown");
343 device = AltosDeviceUIDialog.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);