altosui: Add companion support to the flight UI and CSV conversion
[fw/altos] / altosui / AltosCompanionInfo.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosCompanionInfo extends JTable {
32         private AltosFlightInfoTableModel model;
33
34         private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
35         private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
36
37         static final int info_columns = 2;
38         static final int info_rows = 17;
39
40         int desired_row_height() {
41                 FontMetrics     infoValueMetrics = getFontMetrics(infoValueFont);
42                 return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10;
43         }
44
45         public AltosCompanionInfo() {
46                 super(new AltosFlightInfoTableModel(info_rows, info_columns));
47                 model = (AltosFlightInfoTableModel) getModel();
48                 setFont(infoValueFont);
49                 setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
50                 setShowGrid(true);
51                 setRowHeight(desired_row_height());
52                 doLayout();
53         }
54
55         public Dimension getPreferredScrollableViewportSize() {
56                 return getPreferredSize();
57         }
58
59         void info_reset() {
60                 model.reset();
61         }
62
63         void info_add_row(int col, String name, String value) {
64                 model.addRow(col, name, value);
65         }
66
67         void info_add_row(int col, String name, String format, Object... parameters) {
68                 info_add_row (col, name, String.format(format, parameters));
69         }
70
71         void info_finish() {
72                 model.finish();
73         }
74
75         public void clear() {
76                 model.clear();
77         }
78
79         AltosRecordCompanion    companion;
80
81         public String board_name() {
82                 if (companion == null)
83                         return "None";
84                 switch (companion.board_id) {
85                 case AltosRecordCompanion.board_id_telescience:
86                         return "TeleScience";
87                 default:
88                         return String.format("%02x\n", companion.board_id);
89                 }
90         }
91         
92         public void show(AltosState state, int crc_errors) {
93                 if (state == null)
94                         return;
95                 if (state.data.companion != null)
96                         companion = state.data.companion;
97                 info_reset();
98                 info_add_row(0, "Companion board", "%s", board_name());
99                 if (companion != null) {
100                         info_add_row(0, "Last Data", "%5d", companion.tick);
101                         info_add_row(0, "Update period", "%5.2f s",
102                                      companion.update_period / 100.0);
103                         info_add_row(0, "Channels", "%3d", companion.channels);
104
105                         for (int i = 0; i < companion.channels; i++)
106                                 info_add_row(1, String.format("Channel %2d", i),
107                                              "%6d", companion.companion_data[i]);
108                 }
109                 info_finish();
110         }
111 }