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]);
93 class AltosGPSQuery extends AltosGPS {
94 public AltosGPSQuery (AltosSerial serial, AltosConfigData config_data)
95 throws TimeoutException, InterruptedException {
96 boolean says_done = config_data.compare_version("1.0") >= 0;
99 String line = serial.get_reply_no_dialog(5000);
101 throw new TimeoutException();
102 String[] bits = line.split("\\s+");
103 if (bits.length == 0)
105 if (line.startsWith("Date:")) {
108 String[] d = bits[1].split(":");
111 year = Integer.parseInt(d[0]) + 2000;
112 month = Integer.parseInt(d[1]);
113 day = Integer.parseInt(d[2]);
116 if (line.startsWith("Time:")) {
119 String[] d = bits[1].split("/");
122 hour = Integer.parseInt(d[0]);
123 minute = Integer.parseInt(d[1]);
124 second = Integer.parseInt(d[2]);
127 if (line.startsWith("Lat/Lon:")) {
130 lat = Integer.parseInt(bits[1]) * 1.0e-7;
131 lon = Integer.parseInt(bits[2]) * 1.0e-7;
134 if (line.startsWith("Alt:")) {
137 alt = Integer.parseInt(bits[1]);
140 if (line.startsWith("Flags:")) {
143 int status = Integer.decode(bits[1]);
144 connected = (status & Altos.AO_GPS_RUNNING) != 0;
145 locked = (status & Altos.AO_GPS_VALID) != 0;
150 if (line.startsWith("Sats:")) {
153 nsat = Integer.parseInt(bits[1]);
154 cc_gps_sat = new AltosGPSSat[nsat];
155 for (int i = 0; i < nsat; i++) {
156 int svid = Integer.parseInt(bits[2+i*2]);
157 int cc_n0 = Integer.parseInt(bits[3+i*2]);
158 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
161 if (line.startsWith("done"))
163 if (line.startsWith("Syntax error"))
169 class AltosIdleMonitor extends Thread {
172 AltosIdleMonitorUI ui;
176 AltosState previous_state;
177 AltosConfigData config_data;
181 void update_state() throws InterruptedException, TimeoutException {
182 AltosRecord record = new AltosRecord();
186 serial.set_radio_frequency(frequency);
187 serial.start_remote();
189 serial.flush_input();
190 config_data = new AltosConfigData(serial);
191 adc = new AltosADC(serial);
192 gps = new AltosGPSQuery(serial, config_data);
195 serial.stop_remote();
199 record.callsign = config_data.callsign;
200 record.serial = config_data.serial;
201 record.flight = config_data.log_available() > 0 ? 255 : 0;
204 record.state = Altos.ao_flight_idle;
206 record.tick = adc.tick;
208 // record.accel = adc.accel;
209 // record.pres = adc.pres;
210 // record.batt = adc.batt;
211 // record.temp = adc.temp;
212 // record.drogue = adc.drogue;
213 // record.main = adc.main;
215 // record.ground_accel = record.accel;
216 // record.ground_pres = record.pres;
217 // record.accel_plus_g = config_data.accel_cal_plus;
218 // record.accel_minus_g = config_data.accel_cal_minus;
219 record.acceleration = 0;
223 state = new AltosState (record, state);
226 void set_frequency(double in_frequency) {
227 frequency = in_frequency;
230 public void post_state() {
231 Runnable r = new Runnable() {
236 SwingUtilities.invokeLater(r);
245 } catch (TimeoutException te) {
246 if (AltosSerial.debug)
247 System.out.printf ("monitor idle data timeout\n");
251 } catch (InterruptedException ie) {
256 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
257 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
260 serial = new AltosSerial(device);
266 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
270 AltosInfoTable flightInfo;
271 AltosFlightStatus flightStatus;
272 AltosIdleMonitor thread;
276 void stop_display() {
277 if (thread != null && thread.isAlive()) {
281 } catch (InterruptedException ie) {}
290 public void reset() {
295 public void set_font() {
297 flightInfo.set_font();
300 public void font_size_changed(int font_size) {
304 AltosFlightStatusUpdate status_update;
306 public void show(AltosState state, int crc_errors) {
307 status_update.saved_state = state;
309 pad.show(state, crc_errors);
310 flightStatus.show(state, crc_errors);
311 flightInfo.show(state, crc_errors);
312 } catch (Exception e) {
313 System.out.print("Show exception" + e);
317 public void update(AltosState state) {
322 AltosFreqList frequencies;
324 public AltosIdleMonitorUI(JFrame in_owner)
325 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
327 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
329 if (!device.matchProduct(Altos.product_altimeter))
332 serial = device.getSerial();
333 bag = getContentPane();
334 bag.setLayout(new GridBagLayout());
336 GridBagConstraints c = new GridBagConstraints();
338 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
340 setIconImage(new ImageIcon(imgURL).getImage());
342 setTitle(String.format("AltOS %s", device.toShortString()));
344 /* Stick frequency selector at top of table for telemetry monitoring */
345 if (remote && serial >= 0) {
347 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
348 frequencies.addActionListener(new ActionListener() {
349 public void actionPerformed(ActionEvent e) {
350 double frequency = frequencies.frequency();
351 thread.set_frequency(frequency);
352 AltosUIPreferences.set_frequency(device.getSerial(),
358 c.insets = new Insets(3, 3, 3, 3);
359 c.anchor = GridBagConstraints.WEST;
360 bag.add (frequencies, c);
364 /* Flight status is always visible */
365 flightStatus = new AltosFlightStatus();
368 c.fill = GridBagConstraints.HORIZONTAL;
371 bag.add(flightStatus, c);
374 /* The rest of the window uses a tabbed pane to
375 * show one of the alternate data views
377 pane = new JTabbedPane();
379 pad = new AltosPad();
380 pane.add("Launch Pad", pad);
382 flightInfo = new AltosInfoTable();
383 pane.add("Table", new JScrollPane(flightInfo));
385 /* Make the tabbed pane use the rest of the window space */
388 c.fill = GridBagConstraints.BOTH;
394 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
396 AltosUIPreferences.register_font_listener(this);
398 addWindowListener(new WindowAdapter() {
400 public void windowClosing(WindowEvent e) {
404 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
411 thread = new AltosIdleMonitor(this, device, remote);
413 status_update = new AltosFlightStatusUpdate(flightStatus);
415 new javax.swing.Timer(100, status_update).start();