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.*;
30 import org.altusmetrum.AltosLib.*;
41 public AltosADC(AltosSerial serial) throws InterruptedException, TimeoutException {
44 String line = serial.get_reply_no_dialog(5000);
46 throw new TimeoutException();
48 if (!line.startsWith("tick:"))
50 String[] items = line.split("\\s+");
51 for (int i = 0; i < items.length;) {
52 if (items[i].equals("tick:")) {
53 tick = Integer.parseInt(items[i+1]);
57 if (items[i].equals("accel:")) {
58 accel = Integer.parseInt(items[i+1]);
62 if (items[i].equals("pres:")) {
63 pres = Integer.parseInt(items[i+1]);
67 if (items[i].equals("temp:")) {
68 temp = Integer.parseInt(items[i+1]);
72 if (items[i].equals("batt:")) {
73 batt = Integer.parseInt(items[i+1]);
77 if (items[i].equals("drogue:")) {
78 drogue = Integer.parseInt(items[i+1]);
82 if (items[i].equals("main:")) {
83 main = Integer.parseInt(items[i+1]);
94 class AltosGPSQuery extends AltosGPS {
95 public AltosGPSQuery (AltosSerial serial, AltosConfigData config_data)
96 throws TimeoutException, InterruptedException {
97 boolean says_done = config_data.compare_version("1.0") >= 0;
100 String line = serial.get_reply_no_dialog(5000);
102 throw new TimeoutException();
103 String[] bits = line.split("\\s+");
104 if (bits.length == 0)
106 if (line.startsWith("Date:")) {
109 String[] d = bits[1].split(":");
112 year = Integer.parseInt(d[0]) + 2000;
113 month = Integer.parseInt(d[1]);
114 day = Integer.parseInt(d[2]);
117 if (line.startsWith("Time:")) {
120 String[] d = bits[1].split("/");
123 hour = Integer.parseInt(d[0]);
124 minute = Integer.parseInt(d[1]);
125 second = Integer.parseInt(d[2]);
128 if (line.startsWith("Lat/Lon:")) {
131 lat = Integer.parseInt(bits[1]) * 1.0e-7;
132 lon = Integer.parseInt(bits[2]) * 1.0e-7;
135 if (line.startsWith("Alt:")) {
138 alt = Integer.parseInt(bits[1]);
141 if (line.startsWith("Flags:")) {
144 int status = Integer.decode(bits[1]);
145 connected = (status & Altos.AO_GPS_RUNNING) != 0;
146 locked = (status & Altos.AO_GPS_VALID) != 0;
151 if (line.startsWith("Sats:")) {
154 nsat = Integer.parseInt(bits[1]);
155 cc_gps_sat = new AltosGPSSat[nsat];
156 for (int i = 0; i < nsat; i++) {
157 int svid = Integer.parseInt(bits[2+i*2]);
158 int cc_n0 = Integer.parseInt(bits[3+i*2]);
159 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
162 if (line.startsWith("done"))
164 if (line.startsWith("Syntax error"))
170 class AltosIdleMonitor extends Thread {
173 AltosIdleMonitorUI ui;
177 AltosState previous_state;
178 AltosConfigData config_data;
182 int AltosRSSI() throws TimeoutException, InterruptedException {
183 serial.printf("s\n");
184 String line = serial.get_reply_no_dialog(5000);
186 throw new TimeoutException();
187 String[] items = line.split("\\s+");
188 if (items.length < 2)
190 if (!items[0].equals("RSSI:"))
192 int rssi = Integer.parseInt(items[1]);
196 void update_state() throws InterruptedException, TimeoutException {
197 AltosRecordTM record = new AltosRecordTM();
202 serial.set_radio_frequency(frequency);
203 serial.start_remote();
205 serial.flush_input();
206 config_data = new AltosConfigData(serial);
207 adc = new AltosADC(serial);
208 gps = new AltosGPSQuery(serial, config_data);
211 serial.stop_remote();
218 record.callsign = config_data.callsign;
219 record.serial = config_data.serial;
220 record.flight = config_data.log_available() > 0 ? 255 : 0;
223 record.state = Altos.ao_flight_idle;
225 record.tick = adc.tick;
227 record.accel = adc.accel;
228 record.pres = adc.pres;
229 record.batt = adc.batt;
230 record.temp = adc.temp;
231 record.drogue = adc.drogue;
232 record.main = adc.main;
234 record.ground_accel = record.accel;
235 record.ground_pres = record.pres;
236 record.accel_plus_g = config_data.accel_cal_plus;
237 record.accel_minus_g = config_data.accel_cal_minus;
238 record.acceleration = 0;
242 state = new AltosState (record, state);
245 void set_frequency(double in_frequency) {
246 frequency = in_frequency;
249 public void post_state() {
250 Runnable r = new Runnable() {
255 SwingUtilities.invokeLater(r);
264 } catch (TimeoutException te) {
265 if (AltosSerial.debug)
266 System.out.printf ("monitor idle data timeout\n");
270 } catch (InterruptedException ie) {
275 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
276 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
279 serial = new AltosSerial(device);
285 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
289 AltosInfoTable flightInfo;
290 AltosFlightStatus flightStatus;
291 AltosIdleMonitor thread;
295 void stop_display() {
296 if (thread != null && thread.isAlive()) {
300 } catch (InterruptedException ie) {}
309 public void reset() {
314 public void set_font() {
316 flightInfo.set_font();
319 public void font_size_changed(int font_size) {
323 AltosFlightStatusUpdate status_update;
325 public void show(AltosState state, int crc_errors) {
326 status_update.saved_state = state;
328 pad.show(state, crc_errors);
329 flightStatus.show(state, crc_errors);
330 flightInfo.show(state, crc_errors);
331 } catch (Exception e) {
332 System.out.print("Show exception" + e);
336 public void update(AltosState state) {
341 AltosFreqList frequencies;
343 public AltosIdleMonitorUI(JFrame in_owner)
344 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
346 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
348 if (!device.matchProduct(Altos.product_altimeter))
351 serial = device.getSerial();
352 bag = getContentPane();
353 bag.setLayout(new GridBagLayout());
355 GridBagConstraints c = new GridBagConstraints();
357 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
359 setIconImage(new ImageIcon(imgURL).getImage());
361 setTitle(String.format("AltOS %s", device.toShortString()));
363 /* Stick frequency selector at top of table for telemetry monitoring */
364 if (remote && serial >= 0) {
366 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
367 frequencies.addActionListener(new ActionListener() {
368 public void actionPerformed(ActionEvent e) {
369 double frequency = frequencies.frequency();
370 thread.set_frequency(frequency);
371 AltosUIPreferences.set_frequency(device.getSerial(),
377 c.insets = new Insets(3, 3, 3, 3);
378 c.anchor = GridBagConstraints.WEST;
379 bag.add (frequencies, c);
383 /* Flight status is always visible */
384 flightStatus = new AltosFlightStatus();
387 c.fill = GridBagConstraints.HORIZONTAL;
390 bag.add(flightStatus, c);
393 /* The rest of the window uses a tabbed pane to
394 * show one of the alternate data views
396 pane = new JTabbedPane();
398 pad = new AltosPad();
399 pane.add("Launch Pad", pad);
401 flightInfo = new AltosInfoTable();
402 pane.add("Table", new JScrollPane(flightInfo));
404 /* Make the tabbed pane use the rest of the window space */
407 c.fill = GridBagConstraints.BOTH;
413 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
415 AltosUIPreferences.register_font_listener(this);
417 addWindowListener(new WindowAdapter() {
419 public void windowClosing(WindowEvent e) {
423 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
430 thread = new AltosIdleMonitor(this, device, remote);
432 status_update = new AltosFlightStatusUpdate(flightStatus);
434 new javax.swing.Timer(100, status_update).start();