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