altosui: Initial accel calibration UI
[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_12;
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_12.*;
27
28 public class AltosUIAccelCal
29         extends AltosUIDialog
30         implements AltosAccelCalListener, ActionListener
31 {
32         Frame owner;
33         AltosLink link;
34         AltosAccelCal cal;
35         Thread thread;
36         Container pane;
37         JTextField message;
38         JButton antenna_up;
39         JButton antenna_down;
40         JButton ok;
41         JButton cancel;
42         boolean success;
43         int accel_plus, accel_minus;
44
45         private void make_visible() {
46                 System.out.printf("Make calibration dialog visible\n");
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         /* AltosAccelCalListener interface */
71         public void set_thread(AltosAccelCal cal, Thread thread) {
72                 this.thread = thread;
73         }
74
75         public void set_phase(AltosAccelCal cal, final int phase) {
76                 SwingUtilities.invokeLater(new Runnable() {
77                                 public void run() {
78                                         switch (phase) {
79                                         case AltosAccelCal.phase_antenna_up:
80                                                 message.setText("Orient antenna upwards and click on Antenna Up");
81                                                 antenna_up.setEnabled(true);
82                                                 antenna_down.setEnabled(false);
83                                                 ok.setEnabled(false);
84                                                 break;
85                                         case AltosAccelCal.phase_antenna_down:
86                                                 message.setText("Orient antenna upwards and click on Antenna Down");
87                                                 antenna_up.setEnabled(false);
88                                                 antenna_down.setEnabled(true);
89                                                 ok.setEnabled(false);
90                                                 break;
91                                         }
92                                 }
93                         });
94         }
95
96         public void cal_done(AltosAccelCal cal, int plus, int minus) {
97                 accel_plus = plus;
98                 accel_minus = minus;
99                 SwingUtilities.invokeLater(new Runnable() {
100                                 public void run() {
101                                         message.setText("Calibration succeeded, press OK to continue");
102                                         antenna_up.setEnabled(false);
103                                         antenna_down.setEnabled(false);
104                                         ok.setEnabled(true);
105                                 }
106                         });
107         }
108
109         public void message(AltosAccelCal cal, final String msg) {
110                 SwingUtilities.invokeLater(new Runnable() {
111                                 public void run() {
112                                         message.setText(msg);
113                                 }
114                         });
115         }
116
117         public void error(AltosAccelCal cal, String msg) {
118                 message(cal, msg);
119         }
120
121         /* ActionListener interface */
122         public void actionPerformed(ActionEvent e) {
123                 String  cmd = e.getActionCommand();
124
125                 if ("up".equals(cmd)) {
126                         cal.signal(true);
127                         antenna_up.setEnabled(false);
128                 } else if ("down".equals(cmd)) {
129                         cal.signal(true);
130                         antenna_down.setEnabled(false);
131                 } else if ("ok".equals(cmd)) {
132                         cal.signal(true);
133                         this.setVisible(false);
134                         try {
135                                 cal.abort();
136                         } catch (InterruptedException ie) {
137                         }
138                 } else if ("cancel".equals(cmd)) {
139                         cal.signal(false);
140                         this.setVisible(false);
141                         try {
142                                 cal.abort();
143                         } catch (InterruptedException ie) {
144                         }
145                 }
146         }
147         public AltosUIAccelCal(Frame owner, AltosLink link) {
148                 super(owner, "Calibrate Accelerometer", true);
149
150                 this.owner = owner;
151                 this.link = link;
152
153                 pane = getContentPane();
154                 pane.setLayout(new GridBagLayout());
155
156                 GridBagConstraints c = new GridBagConstraints();
157                 c.insets = new Insets(4,4,4,4);
158
159                 int x = 0;
160                 int y = 0;
161                 c.fill = GridBagConstraints.HORIZONTAL;
162                 c.anchor = GridBagConstraints.WEST;
163                 c.gridx = x;
164                 c.gridy = y;
165                 c.gridwidth = 4;
166                 c.gridheight = 1;
167                 c.weightx = 0;
168                 c.weighty = 0;
169                 message = new JTextField(64);
170                 pane.add(message, c);
171
172                 y++; x = 0;
173
174                 c.fill = GridBagConstraints.NONE;
175                 c.anchor = GridBagConstraints.WEST;
176                 c.gridx = x;
177                 c.gridy = y;
178                 c.gridwidth = 1;
179                 c.gridheight = 1;
180                 c.weightx = 0;
181                 c.weighty = 0;
182                 antenna_up = new JButton("Antenna Up");
183                 antenna_up.setActionCommand("up");
184                 antenna_up.setEnabled(false);
185                 antenna_up.addActionListener(this);
186                 pane.add(antenna_up, c);
187
188                 x++;
189                 c.fill = GridBagConstraints.NONE;
190                 c.anchor = GridBagConstraints.WEST;
191                 c.gridx = x;
192                 c.gridy = y;
193                 c.gridwidth = 1;
194                 c.gridheight = 1;
195                 c.weightx = 0;
196                 c.weighty = 0;
197                 antenna_down = new JButton("Antenna Down");
198                 antenna_down.setActionCommand("down");
199                 antenna_down.setEnabled(false);
200                 antenna_down.addActionListener(this);
201                 pane.add(antenna_down, c);
202
203                 x++;
204                 c.fill = GridBagConstraints.NONE;
205                 c.anchor = GridBagConstraints.WEST;
206                 c.gridx = x;
207                 c.gridy = y;
208                 c.gridwidth = 1;
209                 c.gridheight = 1;
210                 c.weightx = 0;
211                 c.weighty = 0;
212                 ok = new JButton("OK");
213                 ok.setActionCommand("ok");
214                 ok.setEnabled(false);
215                 ok.addActionListener(this);
216                 pane.add(ok, c);
217
218                 x++;
219                 c.fill = GridBagConstraints.NONE;
220                 c.anchor = GridBagConstraints.WEST;
221                 c.gridx = x;
222                 c.gridy = y;
223                 c.gridwidth = 1;
224                 c.gridheight = 1;
225                 c.weightx = 0;
226                 c.weighty = 0;
227                 cancel = new JButton("Cancel");
228                 cancel.setActionCommand("cancel");
229                 cancel.setEnabled(true);
230                 cancel.addActionListener(this);
231                 pane.add(cancel, c);
232
233                 cal = new AltosAccelCal(this.link, this);
234         }
235 }