altosui: Add config and pyro tabs to graph widget
[fw/altos] / altosuilib / AltosFlightConfigTable.java
1 /*
2  * Copyright © 2024 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_14;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.util.*;
24 import org.altusmetrum.altoslib_14.*;
25
26 public class AltosFlightConfigTable extends JComponent
27         implements AltosFontListener, AltosUnitsListener
28 {
29         GridBagLayout   layout;
30
31         FlightConfig[] flight_configs;
32         AltosConfigData config_data;
33
34         class FlightConfig implements AltosFontListener {
35                 JLabel          label;
36                 JLabel[]        value;
37
38                 public void font_size_changed(int font_size) {
39                         label.setFont(AltosUILib.label_font);
40                         for (int i = 0; i < value.length; i++)
41                                 value[i].setFont(AltosUILib.value_font);
42                 }
43
44                 public void set(String l, String[] values) {
45                         label.setText(l);
46                         for (int j = 0; j < values.length; j++)
47                                 value[j].setText(values[j]);
48                 }
49
50                 public FlightConfig(GridBagLayout layout, int y, String label_text, String ... values) {
51                         GridBagConstraints      c = new GridBagConstraints();
52                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
53                         c.weighty = 1;
54
55                         label = new JLabel(label_text);
56                         label.setFont(AltosUILib.label_font);
57                         label.setHorizontalAlignment(SwingConstants.LEFT);
58                         c.gridx = 0; c.gridy = y;
59                         c.anchor = GridBagConstraints.WEST;
60                         c.fill = GridBagConstraints.VERTICAL;
61                         c.weightx = 0;
62                         layout.setConstraints(label, c);
63                         add(label);
64
65                         value = new JLabel[values.length];
66                         for (int j = 0; j < values.length; j++) {
67                                 value[j] = new JLabel(values[j]);
68                                 value[j].setFont(AltosUILib.value_font);
69                                 c.gridx = j+1; c.gridy = y;
70                                 c.anchor = GridBagConstraints.CENTER;
71                                 c.fill = GridBagConstraints.BOTH;
72                                 c.weightx = 1;
73                                 layout.setConstraints(value[j], c);
74                                 add(value[j]);
75                         }
76                         flight_configs[y] = this;
77                 }
78
79         }
80
81         private FlightConfig set_config(int y, String label, String ... values) {
82                 if (flight_configs[y] == null)
83                         flight_configs[y] = new FlightConfig(layout, y, label, values);
84                 else
85                         flight_configs[y].set(label, values);
86                 return flight_configs[y];
87         }
88
89         public void font_size_changed(int font_size) {
90                 for (int y = 0; flight_configs[y] != null; y++)
91                         flight_configs[y].font_size_changed(font_size);
92         }
93
94         private String main_deploy_label() {
95                 return String.format("Main Deploy Altitude(%s)", AltosConvert.height.parse_units());
96         }
97
98         private String main_deploy_value() {
99                 return String.format("%-6.1f", AltosConvert.height.value(config_data.main_deploy, AltosConvert.imperial_units));
100         }
101
102         public void set_config() {
103                 int y = 0;
104                 if (config_data.serial != AltosLib.MISSING) {
105                         if (config_data.product != null && config_data.version != null)
106                                 set_config(y++, "Device",
107                                            config_data.product,
108                                            String.format("version %s", config_data.version),
109                                            String.format("serial %d", config_data.serial));
110                         else
111                                 set_config(y++, "Serial", String.format("%d", config_data.serial));
112                 }
113                 if (config_data.flight != AltosLib.MISSING)
114                         set_config(y++, "Flight", String.format("%d", config_data.flight));
115                 if (config_data.main_deploy != AltosLib.MISSING)
116                         set_config(y++, main_deploy_label(), main_deploy_value());
117                 if (config_data.apogee_delay != AltosLib.MISSING)
118                         set_config(y++, "Apogee Delay(s)", String.format("%d", config_data.apogee_delay));
119                 if (config_data.apogee_lockout != AltosLib.MISSING)
120                         set_config(y++, "Apogee Lockout(s)", String.format("%d", config_data.apogee_lockout));
121                 if (config_data.radio_frequency != AltosLib.MISSING)
122                         set_config(y++, "Radio Frequency (MHz)", String.format("%-7.3f", config_data.radio_frequency / 1000.0));
123                 if (config_data.radio_calibration != AltosLib.MISSING)
124                         set_config(y++, "Radio Calibration", String.format("%d", config_data.radio_calibration));
125                 if (config_data.radio_enable != AltosLib.MISSING)
126                         set_config(y++, "Radio Enable", String.format("%b", config_data.radio_enable != 0));
127                 if (config_data.radio_10mw != AltosLib.MISSING)
128                         set_config(y++, "Limit transmit to 10mW", String.format("%b", config_data.radio_10mw != 0));
129                 if (config_data.report_feet != AltosLib.MISSING)
130                         set_config(y++, "Beep max height in", config_data.report_feet == 0 ? "Meters" : "Feet");
131                 if (config_data.gps_receiver != AltosLib.MISSING)
132                         set_config(y++, "GPS Receiver", AltosLib.gps_receiver_names[config_data.gps_receiver]);
133                 if (config_data.telemetry_rate != AltosLib.MISSING)
134                         set_config(y++, "Telemetry baud rate", String.format("%d", AltosLib.ao_telemetry_rate_values[config_data.telemetry_rate]));
135                 if (config_data.aprs_interval != AltosLib.MISSING)
136                         set_config(y++, "APRS Interval(s)", String.format("%d", config_data.aprs_interval));
137                 if (config_data.aprs_ssid != AltosLib.MISSING)
138                         set_config(y++, "APRS SSID", String.format("%d", config_data.aprs_ssid));
139                 if (config_data.aprs_format != AltosLib.MISSING)
140                         set_config(y++, "APRS Format", AltosLib.ao_aprs_format_name[config_data.aprs_format]);
141                 if (config_data.callsign != null)
142                         set_config(y++, "Callsign", config_data.callsign);
143                 if (config_data.flight_log_max != AltosLib.MISSING)
144                         set_config(y++, "Maximum Flight Log Size(kB)", String.format("%d", config_data.flight_log_max));
145                 if (config_data.ignite_mode != AltosLib.MISSING)
146                         set_config(y++, "Igniter Firing Mode", AltosLib.ignite_mode_values[config_data.ignite_mode]);
147                 if (config_data.pad_orientation != AltosLib.MISSING)
148                         set_config(y++, "Pad Orientation", AltosLib.pad_orientation_values(config_data.has_radio())[config_data.pad_orientation]);
149                 if (config_data.accel_cal_plus != AltosLib.MISSING)
150                         set_config(y++, "Accel Calibration",
151                                    String.format("Plus %d", config_data.accel_cal_plus),
152                                    String.format("Minus %d", config_data.accel_cal_minus));
153                 if (config_data.beep != AltosLib.MISSING)
154                         set_config(y++, "Beeper(Hz)",
155                                    config_data.beep == 0 ? "Disabled" :
156                                    String.format("%-7.1f", AltosConvert.beep_value_to_freq(config_data.beep)));
157                 if (config_data.tracker_motion != AltosLib.MISSING)
158                         set_config(y++,
159                                    String.format("Logging Trigger Motion (%s):", AltosConvert.height.parse_units()),
160                                    String.format("%-6.1f",
161                                                  AltosConvert.height.value(config_data.tracker_motion, AltosConvert.imperial_units)));
162                 if (config_data.tracker_interval != AltosLib.MISSING)
163                         set_config(y++, "Position Reporting Interval(s)",
164                                    String.format("%d", config_data.tracker_interval));
165         }
166
167         public void units_changed(boolean imperial_units) {
168                 set_config();
169         }
170
171         public void tell_closing() {
172                 AltosUIPreferences.unregister_font_listener(this);
173         }
174
175         public AltosFlightConfigTable() {
176                 layout = new GridBagLayout();
177
178                 setLayout(layout);
179
180                 AltosUIPreferences.register_font_listener(this);
181         }
182
183         public AltosFlightConfigTable(AltosConfigData config_data) {
184                 this();
185                 this.config_data = config_data;
186                 flight_configs = new FlightConfig[30];
187                 set_config();
188         }
189 }