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) throws TimeoutException, InterruptedException {
96 String line = serial.get_reply_no_dialog(5000);
98 throw new TimeoutException();
99 String[] bits = line.split("\\s+");
100 if (bits.length == 0)
102 if (line.startsWith("Date:")) {
105 String[] d = bits[1].split(":");
108 year = Integer.parseInt(d[0]) + 2000;
109 month = Integer.parseInt(d[1]);
110 day = Integer.parseInt(d[2]);
113 if (line.startsWith("Time:")) {
116 String[] d = bits[1].split("/");
119 hour = Integer.parseInt(d[0]);
120 minute = Integer.parseInt(d[1]);
121 second = Integer.parseInt(d[2]);
124 if (line.startsWith("Lat/Lon:")) {
127 lat = Integer.parseInt(bits[1]) * 1.0e-7;
128 lon = Integer.parseInt(bits[2]) * 1.0e-7;
131 if (line.startsWith("Alt:")) {
134 alt = Integer.parseInt(bits[1]);
137 if (line.startsWith("Flags:")) {
140 int status = Integer.decode(bits[1]);
141 connected = (status & Altos.AO_GPS_RUNNING) != 0;
142 locked = (status & Altos.AO_GPS_VALID) != 0;
145 if (line.startsWith("Sats:")) {
148 nsat = Integer.parseInt(bits[1]);
149 cc_gps_sat = new AltosGPSSat[nsat];
150 for (int i = 0; i < nsat; i++) {
151 int svid = Integer.parseInt(bits[2+i*2]);
152 int cc_n0 = Integer.parseInt(bits[3+i*2]);
153 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
156 if (line.startsWith("done"))
158 if (line.startsWith("Syntax error"))
164 class AltosIdleMonitor extends Thread {
167 AltosIdleMonitorUI ui;
171 AltosState previous_state;
172 AltosConfigData config_data;
176 void update_state() throws InterruptedException, TimeoutException {
177 AltosRecord record = new AltosRecord();
181 serial.set_radio_frequency(frequency);
182 serial.start_remote();
184 serial.flush_input();
185 config_data = new AltosConfigData(serial);
186 adc = new AltosADC(serial);
187 gps = new AltosGPSQuery(serial);
190 serial.stop_remote();
194 record.callsign = config_data.callsign;
195 record.serial = config_data.serial;
196 record.flight = config_data.log_available() > 0 ? 255 : 0;
199 record.state = Altos.ao_flight_idle;
201 record.tick = adc.tick;
202 record.accel = adc.accel;
203 record.pres = adc.pres;
204 record.batt = adc.batt;
205 record.temp = adc.temp;
206 record.drogue = adc.drogue;
207 record.main = adc.main;
209 record.ground_accel = record.accel;
210 record.ground_pres = record.pres;
211 record.accel_plus_g = config_data.accel_cal_plus;
212 record.accel_minus_g = config_data.accel_cal_minus;
213 record.acceleration = 0;
217 state = new AltosState (record, state);
220 void set_frequency(double in_frequency) {
221 frequency = in_frequency;
224 public void post_state() {
225 Runnable r = new Runnable() {
230 SwingUtilities.invokeLater(r);
239 } catch (TimeoutException te) {
243 } catch (InterruptedException ie) {
248 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
249 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
252 serial = new AltosSerial(device);
258 public class AltosIdleMonitorUI extends JFrame implements AltosFlightDisplay {
262 AltosInfoTable flightInfo;
263 AltosFlightStatus flightStatus;
264 AltosIdleMonitor thread;
268 void stop_display() {
269 if (thread != null && thread.isAlive()) {
273 } catch (InterruptedException ie) {}
282 public void reset() {
287 public void show(AltosState state, int crc_errors) {
289 pad.show(state, crc_errors);
290 flightStatus.show(state, crc_errors);
291 flightInfo.show(state, crc_errors);
292 } catch (Exception e) {
293 System.out.print("Show exception" + e);
297 public void update(AltosState state) {
302 AltosFreqList frequencies;
304 public AltosIdleMonitorUI(JFrame in_owner)
305 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
307 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
309 if (!device.matchProduct(Altos.product_telemetrum))
312 serial = device.getSerial();
313 bag = getContentPane();
314 bag.setLayout(new GridBagLayout());
316 GridBagConstraints c = new GridBagConstraints();
318 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
320 setIconImage(new ImageIcon(imgURL).getImage());
322 setTitle(String.format("AltOS %s", device.toShortString()));
324 /* Stick frequency selector at top of table for telemetry monitoring */
325 if (remote && serial >= 0) {
327 frequencies = new AltosFreqList(AltosPreferences.frequency(serial));
328 frequencies.addActionListener(new ActionListener() {
329 public void actionPerformed(ActionEvent e) {
330 double frequency = frequencies.frequency();
331 thread.set_frequency(frequency);
332 AltosPreferences.set_frequency(device.getSerial(),
338 c.insets = new Insets(3, 3, 3, 3);
339 c.anchor = GridBagConstraints.WEST;
340 bag.add (frequencies, c);
344 /* Flight status is always visible */
345 flightStatus = new AltosFlightStatus();
348 c.fill = GridBagConstraints.HORIZONTAL;
351 bag.add(flightStatus, c);
354 /* The rest of the window uses a tabbed pane to
355 * show one of the alternate data views
357 pane = new JTabbedPane();
359 pad = new AltosPad();
360 pane.add("Launch Pad", pad);
362 flightInfo = new AltosInfoTable();
363 pane.add("Table", new JScrollPane(flightInfo));
365 /* Make the tabbed pane use the rest of the window space */
368 c.fill = GridBagConstraints.BOTH;
374 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
375 addWindowListener(new WindowAdapter() {
377 public void windowClosing(WindowEvent e) {
387 thread = new AltosIdleMonitor(this, device, remote);