Generate LED icons on the fly. Include SVG versions.
[fw/altos] / altosuilib / AltosUIAccelCal.java
1 /*
2  * Copyright © 2017 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 3 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  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_13.*;
27
28 public class AltosUIAccelCal
29         extends AltosUIDialog
30         implements AltosAccelCalListener, ActionListener
31 {
32         Frame owner;
33         AltosLink link;
34         AltosAccelCal cal;
35         AltosConfigValues       config_values;
36         Thread thread;
37         Container pane;
38         JTextField message;
39         JButton antenna_up;
40         JButton antenna_down;
41         JButton ok;
42         JButton cancel;
43         boolean success;
44         int accel_plus, accel_minus;
45
46         private void make_visible() {
47                 pack();
48                 cal.start();
49                 setVisible(true);
50         }
51
52         public boolean doit() {
53                 success = false;
54                 make_visible();
55                 return success;
56         }
57
58         public int accel_cal_plus() {
59                 if (success)
60                         return accel_plus;
61                 return AltosLib.MISSING;
62         }
63
64         public int accel_cal_minus() {
65                 if (success)
66                         return accel_minus;
67                 return AltosLib.MISSING;
68         }
69
70         private void setDefaultButton(JButton button) {
71                 this.getRootPane().setDefaultButton(button);
72         }
73
74         /* AltosAccelCalListener interface */
75         public void set_thread(AltosAccelCal cal, Thread thread) {
76                 this.thread = thread;
77         }
78
79         public void set_phase(AltosAccelCal cal, final int phase) {
80                 SwingUtilities.invokeLater(new Runnable() {
81                                 public void run() {
82                                         switch (phase) {
83                                         case AltosAccelCal.phase_antenna_up:
84                                                 message.setText("Orient antenna upwards and click on Antenna Up");
85                                                 antenna_up.setEnabled(true);
86                                                 setDefaultButton(antenna_up);
87                                                 antenna_down.setEnabled(false);
88                                                 ok.setEnabled(false);
89                                                 break;
90                                         case AltosAccelCal.phase_antenna_down:
91                                                 message.setText("Orient antenna downwards and click on Antenna Down");
92                                                 antenna_up.setEnabled(false);
93                                                 antenna_down.setEnabled(true);
94                                                 setDefaultButton(antenna_down);
95                                                 ok.setEnabled(false);
96                                                 break;
97                                         }
98                                 }
99                         });
100         }
101
102         public void cal_done(AltosAccelCal cal, int plus, int minus) {
103                 accel_plus = plus;
104                 accel_minus = minus;
105                 success = true;
106                 SwingUtilities.invokeLater(new Runnable() {
107                                 public void run() {
108                                         message.setText(String.format("Calibration succeeded, plus %d minus %d, press OK to continue", accel_plus, accel_minus));
109                                         antenna_up.setEnabled(false);
110                                         antenna_down.setEnabled(false);
111                                         ok.setEnabled(true);
112                                         setDefaultButton(ok);
113                                 }
114                         });
115         }
116
117         public void message(AltosAccelCal cal, final String msg) {
118                 SwingUtilities.invokeLater(new Runnable() {
119                                 public void run() {
120                                         message.setText(msg);
121                                 }
122                         });
123         }
124
125         public void error(AltosAccelCal cal, String msg) {
126                 message(cal, msg);
127         }
128
129         /* ActionListener interface */
130         public void actionPerformed(ActionEvent e) {
131                 String  cmd = e.getActionCommand();
132
133                 if ("up".equals(cmd)) {
134                         cal.signal(true);
135                         antenna_up.setEnabled(false);
136                 } else if ("down".equals(cmd)) {
137                         cal.signal(true);
138                         antenna_down.setEnabled(false);
139                         this.setDefaultButton(antenna_down);
140                 } else if ("ok".equals(cmd)) {
141                         cal.signal(true);
142                         this.setVisible(false);
143                         if (success) {
144                                 config_values.set_accel_cal(accel_plus, accel_minus);
145                                 config_values.set_dirty();
146                         }
147                         try {
148                                 cal.abort();
149                         } catch (InterruptedException ie) {
150                         }
151                 } else if ("cancel".equals(cmd)) {
152                         cal.signal(false);
153                         this.setVisible(false);
154                         try {
155                                 cal.abort();
156                         } catch (InterruptedException ie) {
157                         }
158                 }
159         }
160         public AltosUIAccelCal(Frame owner, AltosLink link, AltosConfigValues config_values) {
161                 super(owner, "Calibrate Accelerometer", true);
162
163                 this.owner = owner;
164                 this.link = link;
165                 this.config_values = config_values;
166
167                 pane = getContentPane();
168                 pane.setLayout(new GridBagLayout());
169
170                 GridBagConstraints c = new GridBagConstraints();
171                 c.insets = new Insets(4,4,4,4);
172
173                 int x = 0;
174                 int y = 0;
175                 c.fill = GridBagConstraints.HORIZONTAL;
176                 c.anchor = GridBagConstraints.WEST;
177                 c.gridx = x;
178                 c.gridy = y;
179                 c.gridwidth = 4;
180                 c.gridheight = 1;
181                 c.weightx = 0;
182                 c.weighty = 0;
183                 message = new JTextField(64);
184                 pane.add(message, c);
185
186                 y++; x = 0;
187
188                 c.fill = GridBagConstraints.NONE;
189                 c.anchor = GridBagConstraints.WEST;
190                 c.gridx = x;
191                 c.gridy = y;
192                 c.gridwidth = 1;
193                 c.gridheight = 1;
194                 c.weightx = 0;
195                 c.weighty = 0;
196                 antenna_up = new JButton("Antenna Up");
197                 antenna_up.setActionCommand("up");
198                 antenna_up.setEnabled(false);
199                 antenna_up.addActionListener(this);
200                 pane.add(antenna_up, c);
201
202                 x++;
203                 c.fill = GridBagConstraints.NONE;
204                 c.anchor = GridBagConstraints.WEST;
205                 c.gridx = x;
206                 c.gridy = y;
207                 c.gridwidth = 1;
208                 c.gridheight = 1;
209                 c.weightx = 0;
210                 c.weighty = 0;
211                 antenna_down = new JButton("Antenna Down");
212                 antenna_down.setActionCommand("down");
213                 antenna_down.setEnabled(false);
214                 antenna_down.addActionListener(this);
215                 pane.add(antenna_down, c);
216
217                 x++;
218                 c.fill = GridBagConstraints.NONE;
219                 c.anchor = GridBagConstraints.WEST;
220                 c.gridx = x;
221                 c.gridy = y;
222                 c.gridwidth = 1;
223                 c.gridheight = 1;
224                 c.weightx = 0;
225                 c.weighty = 0;
226                 ok = new JButton("OK");
227                 ok.setActionCommand("ok");
228                 ok.setEnabled(false);
229                 ok.addActionListener(this);
230                 pane.add(ok, c);
231
232                 x++;
233                 c.fill = GridBagConstraints.NONE;
234                 c.anchor = GridBagConstraints.WEST;
235                 c.gridx = x;
236                 c.gridy = y;
237                 c.gridwidth = 1;
238                 c.gridheight = 1;
239                 c.weightx = 0;
240                 c.weighty = 0;
241                 cancel = new JButton("Cancel");
242                 cancel.setActionCommand("cancel");
243                 cancel.setEnabled(true);
244                 cancel.addActionListener(this);
245                 pane.add(cancel, c);
246
247                 cal = new AltosAccelCal(this.link, this);
248         }
249 }