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.*;
40 public AltosADC(AltosSerial serial) throws InterruptedException, TimeoutException {
43 String line = serial.get_reply_no_dialog(5000);
45 throw new TimeoutException();
47 if (!line.startsWith("tick:"))
49 String[] items = line.split("\\s+");
50 for (int i = 0; i < items.length;) {
51 if (items[i].equals("tick:")) {
52 tick = Integer.parseInt(items[i+1]);
56 if (items[i].equals("accel:")) {
57 accel = Integer.parseInt(items[i+1]);
61 if (items[i].equals("pres:")) {
62 pres = Integer.parseInt(items[i+1]);
66 if (items[i].equals("temp:")) {
67 temp = Integer.parseInt(items[i+1]);
71 if (items[i].equals("batt:")) {
72 batt = Integer.parseInt(items[i+1]);
76 if (items[i].equals("drogue:")) {
77 drogue = Integer.parseInt(items[i+1]);
81 if (items[i].equals("main:")) {
82 main = Integer.parseInt(items[i+1]);
92 class AltosGPSQuery extends AltosGPS {
93 public AltosGPSQuery (AltosSerial serial, AltosConfigData config_data)
94 throws TimeoutException, InterruptedException {
95 boolean says_done = config_data.compare_version("1.0") >= 0;
98 String line = serial.get_reply_no_dialog(5000);
100 throw new TimeoutException();
101 String[] bits = line.split("\\s+");
102 if (bits.length == 0)
104 if (line.startsWith("Date:")) {
107 String[] d = bits[1].split(":");
110 year = Integer.parseInt(d[0]) + 2000;
111 month = Integer.parseInt(d[1]);
112 day = Integer.parseInt(d[2]);
115 if (line.startsWith("Time:")) {
118 String[] d = bits[1].split("/");
121 hour = Integer.parseInt(d[0]);
122 minute = Integer.parseInt(d[1]);
123 second = Integer.parseInt(d[2]);
126 if (line.startsWith("Lat/Lon:")) {
129 lat = Integer.parseInt(bits[1]) * 1.0e-7;
130 lon = Integer.parseInt(bits[2]) * 1.0e-7;
133 if (line.startsWith("Alt:")) {
136 alt = Integer.parseInt(bits[1]);
139 if (line.startsWith("Flags:")) {
142 int status = Integer.decode(bits[1]);
143 connected = (status & Altos.AO_GPS_RUNNING) != 0;
144 locked = (status & Altos.AO_GPS_VALID) != 0;
149 if (line.startsWith("Sats:")) {
152 nsat = Integer.parseInt(bits[1]);
153 cc_gps_sat = new AltosGPSSat[nsat];
154 for (int i = 0; i < nsat; i++) {
155 int svid = Integer.parseInt(bits[2+i*2]);
156 int cc_n0 = Integer.parseInt(bits[3+i*2]);
157 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
160 if (line.startsWith("done"))
162 if (line.startsWith("Syntax error"))
168 class AltosIdleMonitor extends Thread {
171 AltosIdleMonitorUI ui;
175 AltosState previous_state;
176 AltosConfigData config_data;
180 void update_state() throws InterruptedException, TimeoutException {
181 AltosRecord record = new AltosRecord();
185 serial.set_radio_frequency(frequency);
186 serial.start_remote();
188 serial.flush_input();
189 config_data = new AltosConfigData(serial);
190 adc = new AltosADC(serial);
191 gps = new AltosGPSQuery(serial, config_data);
194 serial.stop_remote();
198 record.callsign = config_data.callsign;
199 record.serial = config_data.serial;
200 record.flight = config_data.log_available() > 0 ? 255 : 0;
203 record.state = Altos.ao_flight_idle;
205 record.tick = adc.tick;
206 record.accel = adc.accel;
207 record.pres = adc.pres;
208 record.batt = adc.batt;
209 record.temp = adc.temp;
210 record.drogue = adc.drogue;
211 record.main = adc.main;
213 record.ground_accel = record.accel;
214 record.ground_pres = record.pres;
215 record.accel_plus_g = config_data.accel_cal_plus;
216 record.accel_minus_g = config_data.accel_cal_minus;
217 record.acceleration = 0;
221 state = new AltosState (record, state);
224 void set_frequency(double in_frequency) {
225 frequency = in_frequency;
228 public void post_state() {
229 Runnable r = new Runnable() {
234 SwingUtilities.invokeLater(r);
243 } catch (TimeoutException te) {
244 if (AltosSerial.debug)
245 System.out.printf ("monitor idle data timeout\n");
249 } catch (InterruptedException ie) {
254 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
255 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
258 serial = new AltosSerial(device);
264 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
268 AltosInfoTable flightInfo;
269 AltosFlightStatus flightStatus;
270 AltosIdleMonitor thread;
274 void stop_display() {
275 if (thread != null && thread.isAlive()) {
279 } catch (InterruptedException ie) {}
288 public void reset() {
293 public void set_font() {
295 flightInfo.set_font();
298 public void font_size_changed(int font_size) {
302 AltosFlightStatusUpdate status_update;
304 public void show(AltosState state, int crc_errors) {
305 status_update.saved_state = state;
307 pad.show(state, crc_errors);
308 flightStatus.show(state, crc_errors);
309 flightInfo.show(state, crc_errors);
310 } catch (Exception e) {
311 System.out.print("Show exception" + e);
315 public void update(AltosState state) {
320 AltosFreqList frequencies;
322 public AltosIdleMonitorUI(JFrame in_owner)
323 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
325 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
327 if (!device.matchProduct(Altos.product_telemetrum))
330 serial = device.getSerial();
331 bag = getContentPane();
332 bag.setLayout(new GridBagLayout());
334 GridBagConstraints c = new GridBagConstraints();
336 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
338 setIconImage(new ImageIcon(imgURL).getImage());
340 setTitle(String.format("AltOS %s", device.toShortString()));
342 /* Stick frequency selector at top of table for telemetry monitoring */
343 if (remote && serial >= 0) {
345 frequencies = new AltosFreqList(AltosPreferences.frequency(serial));
346 frequencies.addActionListener(new ActionListener() {
347 public void actionPerformed(ActionEvent e) {
348 double frequency = frequencies.frequency();
349 thread.set_frequency(frequency);
350 AltosPreferences.set_frequency(device.getSerial(),
356 c.insets = new Insets(3, 3, 3, 3);
357 c.anchor = GridBagConstraints.WEST;
358 bag.add (frequencies, c);
362 /* Flight status is always visible */
363 flightStatus = new AltosFlightStatus();
366 c.fill = GridBagConstraints.HORIZONTAL;
369 bag.add(flightStatus, c);
372 /* The rest of the window uses a tabbed pane to
373 * show one of the alternate data views
375 pane = new JTabbedPane();
377 pad = new AltosPad();
378 pane.add("Launch Pad", pad);
380 flightInfo = new AltosInfoTable();
381 pane.add("Table", new JScrollPane(flightInfo));
383 /* Make the tabbed pane use the rest of the window space */
386 c.fill = GridBagConstraints.BOTH;
392 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
394 AltosPreferences.register_font_listener(this);
396 addWindowListener(new WindowAdapter() {
398 public void windowClosing(WindowEvent e) {
402 AltosPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
409 thread = new AltosIdleMonitor(this, device, remote);
411 status_update = new AltosFlightStatusUpdate(flightStatus);
413 new javax.swing.Timer(100, status_update).start();