altos: Time out reading packet data from cc1120 after 100ms
[fw/altos] / altosui / AltosConfigPyroUI.java
1 /*
2  * Copyright © 2012 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.event.*;
24 import org.altusmetrum.AltosLib.*;
25
26 public class AltosConfigPyroUI
27         extends AltosDialog
28         implements ItemListener, DocumentListener
29 {
30         AltosConfigUI   owner;
31         Container       pane;
32
33         static Insets il = new Insets(4,4,4,4);
34         static Insets ir = new Insets(4,4,4,4);
35
36         static String[] state_names;
37
38         static void make_state_names() {
39                 if (state_names == null) {
40
41                         state_names = new String[AltosLib.ao_flight_landed - AltosLib.ao_flight_boost + 1];
42                         for (int state = AltosLib.ao_flight_boost; state <= AltosLib.ao_flight_landed; state++)
43                                 state_names[state - AltosLib.ao_flight_boost] = AltosLib.state_name_capital(state);
44                 }
45         }
46
47         class PyroItem implements ItemListener, DocumentListener
48         {
49                 public int              flag;
50                 public JRadioButton     enable;
51                 public JTextField       value;
52                 public JComboBox        combo;
53                 AltosConfigPyroUI       ui;
54
55                 public void set_enable(boolean enable) {
56                         if (value != null)
57                                 value.setEnabled(enable);
58                         if (combo != null)
59                                 combo.setEnabled(enable);
60                 }
61
62                 public void itemStateChanged(ItemEvent e) {
63                         set_enable(enable.isSelected());
64                         ui.set_dirty();
65                 }
66
67                 public void changedUpdate(DocumentEvent e) {
68                         ui.set_dirty();
69                 }
70
71                 public void insertUpdate(DocumentEvent e) {
72                         ui.set_dirty();
73                 }
74
75                 public void removeUpdate(DocumentEvent e) {
76                         ui.set_dirty();
77                 }
78
79                 public void set(boolean new_enable, double new_value) {
80                         enable.setSelected(new_enable);
81                         set_enable(new_enable);
82                         if (value != null) {
83                                 double  scale = AltosPyro.pyro_to_scale(flag);
84                                 String  format = "%6.0f";
85                                 if (scale >= 10)
86                                         format = "%6.1f";
87                                 else if (scale >= 100)
88                                         format = "%6.2f";
89                                 value.setText(String.format(format, new_value));
90                         }
91                         if (combo != null)
92                                 if (new_value >= AltosLib.ao_flight_boost && new_value <= AltosLib.ao_flight_landed)
93                                         combo.setSelectedIndex((int) new_value - AltosLib.ao_flight_boost);
94                 }
95
96                 public boolean enabled() {
97                         return enable.isSelected();
98                 }
99
100                 public double value() {
101                         if (value != null)
102                                 return Double.parseDouble(value.getText());
103                         if (combo != null)
104                                 return combo.getSelectedIndex() + AltosLib.ao_flight_boost;
105                         return 0;
106                 }
107
108                 public PyroItem(AltosConfigPyroUI in_ui, int in_flag, int x, int y) {
109
110                         ui = in_ui;
111                         flag = in_flag;
112
113                         GridBagConstraints      c;
114                         c = new GridBagConstraints();
115                         c.gridx = x; c.gridy = y;
116                         c.gridwidth = 1;
117                         c.fill = GridBagConstraints.NONE;
118                         c.anchor = GridBagConstraints.LINE_START;
119                         c.insets = il;
120                         enable = new JRadioButton();
121                         enable.addItemListener(this);
122                         pane.add(enable, c);
123                         
124                         if ((flag & AltosPyro.pyro_no_value) == 0) {
125                                 c = new GridBagConstraints();
126                                 c.gridx = x+1; c.gridy = y;
127                                 c.gridwidth = 1;
128                                 c.fill = GridBagConstraints.NONE;
129                                 c.anchor = GridBagConstraints.LINE_START;
130                                 c.insets = il;
131                                 if ((flag & AltosPyro.pyro_state_value) != 0) {
132                                         make_state_names();
133                                         combo = new JComboBox(state_names);
134                                         combo.addItemListener(this);
135                                         pane.add(combo, c);
136                                 } else {
137                                         value = new JTextField(10);
138                                         value.getDocument().addDocumentListener(this);
139                                         pane.add(value, c);
140                                 }
141                         }
142                 }
143         }
144
145         class PyroColumn {
146                 public PyroItem[]       items;
147                 public JLabel           label;
148                 int                     channel;
149
150                 public void set(AltosPyro pyro) {
151                         int row = 0;
152                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
153                                 if ((AltosPyro.pyro_all & flag) != 0) {
154                                         items[row].set((pyro.flags & flag) != 0,
155                                                        pyro.get_value(flag));
156                                         row++;
157                                 }
158                         }
159                 }
160
161                 public AltosPyro get() {
162                         AltosPyro       p = new AltosPyro(channel);
163
164                         int row = 0;
165                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1) {
166                                 if ((AltosPyro.pyro_all & flag) != 0) {
167                                         if (items[row].enabled()) {
168                                                 System.out.printf ("Flag %x enabled\n", flag);
169                                                 p.flags |= flag;
170                                                 p.set_value(flag, items[row].value());
171                                         }
172                                         row++;
173                                 }
174                         }
175                         System.out.printf ("Pyro %x %s\n", p.flags, p.toString());
176                         return p;
177                 }
178
179                 public PyroColumn(AltosConfigPyroUI ui, int x, int y, int in_channel) {
180
181                         channel = in_channel;
182
183                         int     nrow = 0;
184                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
185                                 if ((flag & AltosPyro.pyro_all) != 0)
186                                         nrow++;
187
188                         items = new PyroItem[nrow];
189                         int row = 0;
190                         
191                         GridBagConstraints      c;
192                         c = new GridBagConstraints();
193                         c.gridx = x; c.gridy = y;
194                         c.gridwidth = 2;
195                         c.fill = GridBagConstraints.NONE;
196                         c.anchor = GridBagConstraints.CENTER;
197                         c.insets = il;
198                         label = new JLabel(String.format("Pyro Channel %d", channel));
199                         pane.add(label, c);
200                         y++;
201
202                         for (int flag = 1; flag < AltosPyro.pyro_all; flag <<= 1)
203                                 if ((flag & AltosPyro.pyro_all) != 0) {
204                                         items[row] = new PyroItem(ui, flag, x, y + row);
205                                         row++;
206                                 }
207                 }
208         }
209
210         PyroColumn[]    columns;
211
212         public void set_pyros(AltosPyro[] pyros) {
213                 for (int i = 0; i < pyros.length; i++) {
214                         if (pyros[i].channel < columns.length)
215                                 columns[pyros[i].channel].set(pyros[i]);
216                 }
217         }
218
219         public AltosPyro[] get_pyros() {
220                 AltosPyro[]     pyros = new AltosPyro[columns.length];
221                 for (int c = 0; c < columns.length; c++)
222                         pyros[c] = columns[c].get();
223                 return pyros;
224         }
225
226         public void set_dirty() {
227                 owner.set_dirty();
228         }
229
230         public void itemStateChanged(ItemEvent e) {
231                 owner.set_dirty();
232         }
233
234         public void changedUpdate(DocumentEvent e) {
235                 owner.set_dirty();
236         }
237
238         public void insertUpdate(DocumentEvent e) {
239                 owner.set_dirty();
240         }
241
242         public void removeUpdate(DocumentEvent e) {
243                 owner.set_dirty();
244         }
245
246         public AltosConfigPyroUI(AltosConfigUI in_owner, AltosPyro[] pyros) {
247
248                 super(in_owner, "Configure Pyro Channels", false);
249
250                 owner = in_owner;
251
252                 GridBagConstraints      c;
253
254                 pane = getContentPane();
255                 pane.setLayout(new GridBagLayout());
256
257                 int     row = 1;
258
259                 for (int flag = 1; flag <= AltosPyro.pyro_all; flag <<= 1) {
260                         String  n;
261
262                         n = AltosPyro.pyro_to_name(flag);
263                         if (n != null) {
264                                 c = new GridBagConstraints();
265                                 c.gridx = 0; c.gridy = row;
266                                 c.gridwidth = 1;
267                                 c.fill = GridBagConstraints.NONE;
268                                 c.anchor = GridBagConstraints.LINE_START;
269                                 c.insets = il;
270                                 JLabel label = new JLabel(n);
271                                 pane.add(label, c);
272                                 row++;
273                         }
274                 }
275
276                 columns = new PyroColumn[pyros.length];
277
278                 for (int i = 0; i < pyros.length; i++) {
279                         columns[i] = new PyroColumn(this, i*2 + 1, 0, i);
280                         columns[i].set(pyros[i]);
281                 }
282         }
283
284         public void make_visible() {
285                 pack();
286                 setVisible(true);
287         }
288 }