diff options
author | Keith Packard <keithp@keithp.com> | 2017-08-11 23:42:53 -0400 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2017-08-12 18:28:17 -0400 |
commit | 5de9496b103b495b7ff77c84e1047d207bc4259c (patch) | |
tree | f74f2bf436efc9513666cfa10eb997ad1fdbdad6 /altosuilib/AltosUIAccelCal.java | |
parent | 43e2275250d9c91560a770942f3c06a8f74ed501 (diff) |
altosui: Initial accel calibration UIaccel-cal-ui
Almost working, needs further tweaking.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'altosuilib/AltosUIAccelCal.java')
-rw-r--r-- | altosuilib/AltosUIAccelCal.java | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/altosuilib/AltosUIAccelCal.java b/altosuilib/AltosUIAccelCal.java new file mode 100644 index 00000000..dab866a2 --- /dev/null +++ b/altosuilib/AltosUIAccelCal.java @@ -0,0 +1,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); + } +} |