2 * Copyright © 2017 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altosuilib_12;
22 import java.awt.event.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_12.*;
28 public class AltosUIAccelCal
30 implements AltosAccelCalListener, ActionListener
35 AltosConfigValues config_values;
44 int accel_plus, accel_minus;
46 private void make_visible() {
47 System.out.printf("Make calibration dialog visible\n");
53 public boolean doit() {
59 public int accel_cal_plus() {
62 return AltosLib.MISSING;
65 public int accel_cal_minus() {
68 return AltosLib.MISSING;
71 /* AltosAccelCalListener interface */
72 public void set_thread(AltosAccelCal cal, Thread thread) {
76 public void set_phase(AltosAccelCal cal, final int phase) {
77 SwingUtilities.invokeLater(new Runnable() {
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);
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);
97 public void cal_done(AltosAccelCal cal, int plus, int minus) {
101 SwingUtilities.invokeLater(new Runnable() {
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);
111 public void message(AltosAccelCal cal, final String msg) {
112 SwingUtilities.invokeLater(new Runnable() {
114 message.setText(msg);
119 public void error(AltosAccelCal cal, String msg) {
123 /* ActionListener interface */
124 public void actionPerformed(ActionEvent e) {
125 String cmd = e.getActionCommand();
127 if ("up".equals(cmd)) {
129 antenna_up.setEnabled(false);
130 } else if ("down".equals(cmd)) {
132 antenna_down.setEnabled(false);
133 } else if ("ok".equals(cmd)) {
135 this.setVisible(false);
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();
143 } catch (InterruptedException ie) {
145 } else if ("cancel".equals(cmd)) {
147 this.setVisible(false);
150 } catch (InterruptedException ie) {
154 public AltosUIAccelCal(Frame owner, AltosLink link, AltosConfigValues config_values) {
155 super(owner, "Calibrate Accelerometer", true);
159 this.config_values = config_values;
161 pane = getContentPane();
162 pane.setLayout(new GridBagLayout());
164 GridBagConstraints c = new GridBagConstraints();
165 c.insets = new Insets(4,4,4,4);
169 c.fill = GridBagConstraints.HORIZONTAL;
170 c.anchor = GridBagConstraints.WEST;
177 message = new JTextField(64);
178 pane.add(message, c);
182 c.fill = GridBagConstraints.NONE;
183 c.anchor = GridBagConstraints.WEST;
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);
197 c.fill = GridBagConstraints.NONE;
198 c.anchor = GridBagConstraints.WEST;
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);
212 c.fill = GridBagConstraints.NONE;
213 c.anchor = GridBagConstraints.WEST;
220 ok = new JButton("OK");
221 ok.setActionCommand("ok");
222 ok.setEnabled(false);
223 ok.addActionListener(this);
227 c.fill = GridBagConstraints.NONE;
228 c.anchor = GridBagConstraints.WEST;
235 cancel = new JButton("Cancel");
236 cancel.setActionCommand("cancel");
237 cancel.setEnabled(true);
238 cancel.addActionListener(this);
241 cal = new AltosAccelCal(this.link, this);