summaryrefslogtreecommitdiff
path: root/altosuilib/AltosUIAccelCal.java
blob: dab866a2dff22205d13a37ddf884d6c80d1892c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

package org.altusmetrum.altosuilib_12;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import org.altusmetrum.altoslib_12.*;

public class AltosUIAccelCal
	extends AltosUIDialog
	implements AltosAccelCalListener, ActionListener
{
	Frame owner;
	AltosLink link;
	AltosAccelCal cal;
	Thread thread;
	Container pane;
	JTextField message;
	JButton	antenna_up;
	JButton	antenna_down;
	JButton	ok;
	JButton cancel;
	boolean success;
	int accel_plus, accel_minus;

	private void make_visible() {
		System.out.printf("Make calibration dialog visible\n");
		pack();
		cal.start();
		setVisible(true);
	}

	public boolean doit() {
		success = false;
		make_visible();
		return success;
	}

	public int accel_cal_plus() {
		if (success)
			return accel_plus;
		return AltosLib.MISSING;
	}

	public int accel_cal_minus() {
		if (success)
			return accel_minus;
		return AltosLib.MISSING;
	}

	/* AltosAccelCalListener interface */
	public void set_thread(AltosAccelCal cal, Thread thread) {
		this.thread = thread;
	}

	public void set_phase(AltosAccelCal cal, final int phase) {
		SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					switch (phase) {
					case AltosAccelCal.phase_antenna_up:
						message.setText("Orient antenna upwards and click on Antenna Up");
						antenna_up.setEnabled(true);
						antenna_down.setEnabled(false);
						ok.setEnabled(false);
						break;
					case AltosAccelCal.phase_antenna_down:
						message.setText("Orient antenna upwards and click on Antenna Down");
						antenna_up.setEnabled(false);
						antenna_down.setEnabled(true);
						ok.setEnabled(false);
						break;
					}
				}
			});
	}

	public void cal_done(AltosAccelCal cal, int plus, int minus) {
		accel_plus = plus;
		accel_minus = minus;
		SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					message.setText("Calibration succeeded, press OK to continue");
					antenna_up.setEnabled(false);
					antenna_down.setEnabled(false);
					ok.setEnabled(true);
				}
			});
	}

	public void message(AltosAccelCal cal, final String msg) {
		SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					message.setText(msg);
				}
			});
	}

	public void error(AltosAccelCal cal, String msg) {
		message(cal, msg);
	}

	/* ActionListener interface */
	public void actionPerformed(ActionEvent e) {
		String	cmd = e.getActionCommand();

		if ("up".equals(cmd)) {
			cal.signal(true);
			antenna_up.setEnabled(false);
		} else if ("down".equals(cmd)) {
			cal.signal(true);
			antenna_down.setEnabled(false);
		} else if ("ok".equals(cmd)) {
			cal.signal(true);
			this.setVisible(false);
			try {
				cal.abort();
			} catch (InterruptedException ie) {
			}
		} else if ("cancel".equals(cmd)) {
			cal.signal(false);
			this.setVisible(false);
			try {
				cal.abort();
			} catch (InterruptedException ie) {
			}
		}
	}
	public AltosUIAccelCal(Frame owner, AltosLink link) {
		super(owner, "Calibrate Accelerometer", true);

		this.owner = owner;
		this.link = link;

		pane = getContentPane();
		pane.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(4,4,4,4);

		int x = 0;
		int y = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.WEST;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = 4;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		message = new JTextField(64);
		pane.add(message, c);

		y++; x = 0;

		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.WEST;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		antenna_up = new JButton("Antenna Up");
		antenna_up.setActionCommand("up");
		antenna_up.setEnabled(false);
		antenna_up.addActionListener(this);
		pane.add(antenna_up, c);

		x++;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.WEST;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		antenna_down = new JButton("Antenna Down");
		antenna_down.setActionCommand("down");
		antenna_down.setEnabled(false);
		antenna_down.addActionListener(this);
		pane.add(antenna_down, c);

		x++;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.WEST;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		ok = new JButton("OK");
		ok.setActionCommand("ok");
		ok.setEnabled(false);
		ok.addActionListener(this);
		pane.add(ok, c);

		x++;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.WEST;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		cancel = new JButton("Cancel");
		cancel.setActionCommand("cancel");
		cancel.setEnabled(true);
		cancel.addActionListener(this);
		pane.add(cancel, c);

		cal = new AltosAccelCal(this.link, this);
	}
}