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;
207 record.accel = adc.accel;
208 record.pres = adc.pres;
209 record.batt = adc.batt;
210 record.temp = adc.temp;
211 record.drogue = adc.drogue;
212 record.main = adc.main;
214 record.ground_accel = record.accel;
215 record.ground_pres = record.pres;
216 record.accel_plus_g = config_data.accel_cal_plus;
217 record.accel_minus_g = config_data.accel_cal_minus;
218 record.acceleration = 0;
222 state = new AltosState (record, state);
225 void set_frequency(double in_frequency) {
226 frequency = in_frequency;
229 public void post_state() {
230 Runnable r = new Runnable() {
235 SwingUtilities.invokeLater(r);
244 } catch (TimeoutException te) {
245 if (AltosSerial.debug)
246 System.out.printf ("monitor idle data timeout\n");
250 } catch (InterruptedException ie) {
255 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
256 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
259 serial = new AltosSerial(device);
265 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
269 AltosInfoTable flightInfo;
270 AltosFlightStatus flightStatus;
271 AltosIdleMonitor thread;
275 void stop_display() {
276 if (thread != null && thread.isAlive()) {
280 } catch (InterruptedException ie) {}
289 public void reset() {
294 public void set_font() {
296 flightInfo.set_font();
299 public void font_size_changed(int font_size) {
303 AltosFlightStatusUpdate status_update;
305 public void show(AltosState state, int crc_errors) {
306 status_update.saved_state = state;
308 pad.show(state, crc_errors);
309 flightStatus.show(state, crc_errors);
310 flightInfo.show(state, crc_errors);
311 } catch (Exception e) {
312 System.out.print("Show exception" + e);
316 public void update(AltosState state) {
321 AltosFreqList frequencies;
323 public AltosIdleMonitorUI(JFrame in_owner)
324 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
326 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
328 if (!device.matchProduct(Altos.product_telemetrum))
331 serial = device.getSerial();
332 bag = getContentPane();
333 bag.setLayout(new GridBagLayout());
335 GridBagConstraints c = new GridBagConstraints();
337 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
339 setIconImage(new ImageIcon(imgURL).getImage());
341 setTitle(String.format("AltOS %s", device.toShortString()));
343 /* Stick frequency selector at top of table for telemetry monitoring */
344 if (remote && serial >= 0) {
346 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
347 frequencies.addActionListener(new ActionListener() {
348 public void actionPerformed(ActionEvent e) {
349 double frequency = frequencies.frequency();
350 thread.set_frequency(frequency);
351 AltosUIPreferences.set_frequency(device.getSerial(),
357 c.insets = new Insets(3, 3, 3, 3);
358 c.anchor = GridBagConstraints.WEST;
359 bag.add (frequencies, c);
363 /* Flight status is always visible */
364 flightStatus = new AltosFlightStatus();
367 c.fill = GridBagConstraints.HORIZONTAL;
370 bag.add(flightStatus, c);
373 /* The rest of the window uses a tabbed pane to
374 * show one of the alternate data views
376 pane = new JTabbedPane();
378 pad = new AltosPad();
379 pane.add("Launch Pad", pad);
381 flightInfo = new AltosInfoTable();
382 pane.add("Table", new JScrollPane(flightInfo));
384 /* Make the tabbed pane use the rest of the window space */
387 c.fill = GridBagConstraints.BOTH;
393 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
395 AltosUIPreferences.register_font_listener(this);
397 addWindowListener(new WindowAdapter() {
399 public void windowClosing(WindowEvent e) {
403 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
410 thread = new AltosIdleMonitor(this, device, remote);
412 status_update = new AltosFlightStatusUpdate(flightStatus);
414 new javax.swing.Timer(100, status_update).start();