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) throws TimeoutException, InterruptedException {
97 String line = serial.get_reply_no_dialog(5000);
99 throw new TimeoutException();
100 String[] bits = line.split("\\s+");
101 if (bits.length == 0)
103 if (line.startsWith("Date:")) {
106 String[] d = bits[1].split(":");
109 year = Integer.parseInt(d[0]) + 2000;
110 month = Integer.parseInt(d[1]);
111 day = Integer.parseInt(d[2]);
114 if (line.startsWith("Time:")) {
117 String[] d = bits[1].split("/");
120 hour = Integer.parseInt(d[0]);
121 minute = Integer.parseInt(d[1]);
122 second = Integer.parseInt(d[2]);
125 if (line.startsWith("Lat/Lon:")) {
128 lat = Integer.parseInt(bits[1]) * 1.0e-7;
129 lon = Integer.parseInt(bits[2]) * 1.0e-7;
132 if (line.startsWith("Alt:")) {
135 alt = Integer.parseInt(bits[1]);
138 if (line.startsWith("Flags:")) {
141 int status = Integer.decode(bits[1]);
142 connected = (status & Altos.AO_GPS_RUNNING) != 0;
143 locked = (status & Altos.AO_GPS_VALID) != 0;
146 if (line.startsWith("Sats:")) {
149 nsat = Integer.parseInt(bits[1]);
150 cc_gps_sat = new AltosGPSSat[nsat];
151 for (int i = 0; i < nsat; i++) {
152 int svid = Integer.parseInt(bits[2+i*2]);
153 int cc_n0 = Integer.parseInt(bits[3+i*2]);
154 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
157 if (line.startsWith("done"))
159 if (line.startsWith("Syntax error"))
165 class AltosIdleMonitor extends Thread {
168 AltosIdleMonitorUI ui;
172 AltosState previous_state;
173 AltosConfigData config_data;
177 void update_state() throws InterruptedException, TimeoutException {
178 AltosRecord record = new AltosRecord();
182 serial.set_radio_frequency(frequency);
183 serial.start_remote();
185 serial.flush_input();
186 config_data = new AltosConfigData(serial);
187 adc = new AltosADC(serial);
188 gps = new AltosGPSQuery(serial);
191 serial.stop_remote();
195 record.callsign = config_data.callsign;
196 record.serial = config_data.serial;
197 record.flight = config_data.log_available() > 0 ? 255 : 0;
200 record.state = Altos.ao_flight_idle;
202 record.tick = adc.tick;
203 record.accel = adc.accel;
204 record.pres = adc.pres;
205 record.batt = adc.batt;
206 record.temp = adc.temp;
207 record.drogue = adc.drogue;
208 record.main = adc.main;
210 record.ground_accel = record.accel;
211 record.ground_pres = record.pres;
212 record.accel_plus_g = config_data.accel_cal_plus;
213 record.accel_minus_g = config_data.accel_cal_minus;
214 record.acceleration = 0;
218 state = new AltosState (record, state);
221 void set_frequency(double in_frequency) {
222 frequency = in_frequency;
225 public void post_state() {
226 Runnable r = new Runnable() {
231 SwingUtilities.invokeLater(r);
240 } catch (TimeoutException te) {
244 } catch (InterruptedException ie) {
249 public AltosIdleMonitor(AltosIdleMonitorUI in_ui, AltosDevice in_device, boolean in_remote)
250 throws FileNotFoundException, AltosSerialInUseException, InterruptedException, TimeoutException {
253 serial = new AltosSerial(device);
259 public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
263 AltosInfoTable flightInfo;
264 AltosFlightStatus flightStatus;
265 AltosIdleMonitor thread;
269 void stop_display() {
270 if (thread != null && thread.isAlive()) {
274 } catch (InterruptedException ie) {}
283 public void reset() {
288 public void set_font() {
290 flightInfo.set_font();
293 public void font_size_changed(int font_size) {
297 public void show(AltosState state, int crc_errors) {
299 pad.show(state, crc_errors);
300 flightStatus.show(state, crc_errors);
301 flightInfo.show(state, crc_errors);
302 } catch (Exception e) {
303 System.out.print("Show exception" + e);
307 public void update(AltosState state) {
312 AltosFreqList frequencies;
314 public AltosIdleMonitorUI(JFrame in_owner)
315 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
317 device = AltosDeviceDialog.show(in_owner, Altos.product_any);
319 if (!device.matchProduct(Altos.product_telemetrum))
322 serial = device.getSerial();
323 bag = getContentPane();
324 bag.setLayout(new GridBagLayout());
326 GridBagConstraints c = new GridBagConstraints();
328 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
330 setIconImage(new ImageIcon(imgURL).getImage());
332 setTitle(String.format("AltOS %s", device.toShortString()));
334 /* Stick frequency selector at top of table for telemetry monitoring */
335 if (remote && serial >= 0) {
337 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
338 frequencies.addActionListener(new ActionListener() {
339 public void actionPerformed(ActionEvent e) {
340 double frequency = frequencies.frequency();
341 thread.set_frequency(frequency);
342 AltosUIPreferences.set_frequency(device.getSerial(),
348 c.insets = new Insets(3, 3, 3, 3);
349 c.anchor = GridBagConstraints.WEST;
350 bag.add (frequencies, c);
354 /* Flight status is always visible */
355 flightStatus = new AltosFlightStatus();
358 c.fill = GridBagConstraints.HORIZONTAL;
361 bag.add(flightStatus, c);
364 /* The rest of the window uses a tabbed pane to
365 * show one of the alternate data views
367 pane = new JTabbedPane();
369 pad = new AltosPad();
370 pane.add("Launch Pad", pad);
372 flightInfo = new AltosInfoTable();
373 pane.add("Table", new JScrollPane(flightInfo));
375 /* Make the tabbed pane use the rest of the window space */
378 c.fill = GridBagConstraints.BOTH;
384 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
386 AltosUIPreferences.register_font_listener(this);
388 addWindowListener(new WindowAdapter() {
390 public void windowClosing(WindowEvent e) {
394 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
401 thread = new AltosIdleMonitor(this, device, remote);