2 * Copyright © 2010 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 import java.awt.event.*;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_11.*;
28 import org.altusmetrum.altosuilib_11.*;
30 public class AltosIgniteUI
32 implements ActionListener
39 javax.swing.Timer timer;
46 final static int timeout = 1 * 1000;
49 boolean timer_running;
53 LinkedBlockingQueue<String> command_queue;
61 void set_status (int status) {
63 status_label.setText(String.format("\"%s\"", AltosIgnite.status_string(status)));
66 Igniter(AltosIgniteUI ui, String label, String name, int y) {
67 Container pane = getContentPane();
68 GridBagConstraints c = new GridBagConstraints();
69 Insets i = new Insets(4,4,4,4);
72 this.status = AltosIgnite.Unknown;
77 c.anchor = GridBagConstraints.WEST;
78 button = new JRadioButton (label);
80 button.addActionListener(ui);
81 button.setActionCommand(name);
87 c.anchor = GridBagConstraints.WEST;
88 status_label = new JLabel("plenty of text");
89 pane.add(status_label, c);
91 status = AltosIgnite.Unknown;
97 void set_status(String _name, int _status) {
99 final String name = _name;
100 final int status = _status;
101 Runnable r = new Runnable() {
103 for (int p = 0; p < igniters.length; p++)
104 if (name.equals(igniters[p].name))
105 igniters[p].set_status(status);
108 SwingUtilities.invokeLater(r);
111 class IgniteHandler implements Runnable {
116 void send_exception(Exception e) {
117 final Exception f_e = e;
118 Runnable r = new Runnable() {
120 ignite_exception(f_e);
123 SwingUtilities.invokeLater(r);
128 ignite = new AltosIgnite(link,
129 !device.matchProduct(Altos.product_altimeter));
131 } catch (Exception e) {
140 String command = command_queue.take();
143 if (command.equals("get_status")) {
144 HashMap<String,Integer> status_map = ignite.status();
146 for (int p = 0; p < igniters.length; p++) {
147 Integer i = status_map.get(igniters[p].name);
149 set_status(igniters[p].name, i);
152 } else if (command.equals("get_npyro")) {
153 reply = String.format("npyro %d", ignite.npyro());
154 } else if (command.equals("quit")) {
158 ignite.fire(command);
161 final String f_reply = reply;
164 ignite_reply(f_reply);
167 SwingUtilities.invokeLater(r);
168 } catch (Exception e) {
174 public IgniteHandler(JFrame in_owner, AltosLink in_link) {
180 void ignite_exception(Exception e) {
181 if (e instanceof FileNotFoundException) {
182 JOptionPane.showMessageDialog(owner,
183 ((FileNotFoundException) e).getMessage(),
184 "Cannot open target device",
185 JOptionPane.ERROR_MESSAGE);
186 } else if (e instanceof AltosSerialInUseException) {
187 JOptionPane.showMessageDialog(owner,
188 String.format("Device \"%s\" already in use",
189 device.toShortString()),
191 JOptionPane.ERROR_MESSAGE);
192 } else if (e instanceof IOException) {
193 IOException ee = (IOException) e;
194 JOptionPane.showMessageDialog(owner,
195 device.toShortString(),
196 ee.getLocalizedMessage(),
197 JOptionPane.ERROR_MESSAGE);
199 JOptionPane.showMessageDialog(owner,
200 String.format("Connection to \"%s\" failed",
201 device.toShortString()),
203 JOptionPane.ERROR_MESSAGE);
208 void ignite_reply(String reply) {
209 if (reply.equals("status")) {
211 } else if (reply.equals("fired")) {
213 } else if (reply.startsWith("npyro")) {
214 npyro = Integer.parseInt(reply.substring(6));
219 void set_arm_text() {
220 if (arm.isSelected())
221 arm.setText(String.format("%d", time_remaining));
229 timer_running = true;
234 fire.setEnabled(false);
235 timer_running = false;
236 arm.setSelected(false);
237 arm.setEnabled(false);
242 group.clearSelection();
243 fire.setEnabled(false);
247 void send_command(String command) {
249 command_queue.put(command);
250 } catch (Exception ex) {
251 ignite_exception(ex);
255 boolean getting_status = false;
257 boolean visible = false;
259 void set_ignite_status() {
260 getting_status = false;
268 void poll_ignite_status() {
269 if (poll_remaining > 0) {
273 if (!getting_status) {
274 getting_status = true;
275 send_command("get_status");
279 boolean firing = false;
281 void start_fire(String which) {
295 send_command("quit");
306 if (time_remaining <= 0)
311 poll_ignite_status();
315 if (arm.isEnabled() && arm.isSelected() && time_remaining > 0) {
316 String igniter = "none";
318 for (int p = 0; p < igniters.length; p++)
319 if (igniters[p].button.isSelected()) {
320 igniter = igniters[p].name;
323 send_command(igniter);
328 public void actionPerformed(ActionEvent e) {
329 String cmd = e.getActionCommand();
331 for (int p = 0; p < igniters.length; p++)
332 if (cmd.equals(igniters[p].name)) {
334 arm.setEnabled(true);
338 if (cmd.equals("arm")) {
339 if (arm.isSelected()) {
340 fire.setEnabled(true);
345 if (cmd.equals("fire"))
347 if (cmd.equals("tick"))
349 if (cmd.equals("close"))
353 /* A window listener to catch closing events and tell the config code */
354 class ConfigListener extends WindowAdapter {
357 public ConfigListener(AltosIgniteUI this_ui) {
361 public void windowClosing(WindowEvent e) {
362 ui.actionPerformed(new ActionEvent(e.getSource(),
363 ActionEvent.ACTION_PERFORMED,
368 private boolean open() {
369 command_queue = new LinkedBlockingQueue<String>();
372 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
373 if (device != null) {
375 AltosSerial serial = new AltosSerial(device);
376 serial.set_frame(owner);
377 IgniteHandler handler = new IgniteHandler(owner, serial);
378 Thread t = new Thread(handler);
382 } catch (Exception ex) {
383 ignite_exception(ex);
389 private void make_ui() {
390 group = new ButtonGroup();
392 Container pane = getContentPane();
394 GridBagConstraints c = new GridBagConstraints();
395 Insets i = new Insets(4,4,4,4);
397 timer = new javax.swing.Timer(timeout, this);
398 timer.setActionCommand("tick");
399 timer_running = false;
402 pane.setLayout(new GridBagLayout());
404 c.fill = GridBagConstraints.NONE;
405 c.anchor = GridBagConstraints.CENTER;
415 c.anchor = GridBagConstraints.CENTER;
416 label = new JLabel ("Fire Igniter");
421 igniters = new Igniter[2 + npyro];
423 igniters[0] = new Igniter(this, "Apogee", AltosIgnite.Apogee, y++);
424 igniters[1] = new Igniter(this, "Main", AltosIgnite.Main, y++);
426 for (int p = 0; p < npyro; p++) {
427 String name = String.format("%d", p);
428 String label = String.format("%c", 'A' + p);
429 igniters[2+p] = new Igniter(this, label, name, y++);
435 c.anchor = GridBagConstraints.CENTER;
436 arm = new JToggleButton ("Arm");
438 arm.addActionListener(this);
439 arm.setActionCommand("arm");
440 arm.setEnabled(false);
445 c.anchor = GridBagConstraints.CENTER;
446 fire = new JButton ("Fire");
447 fire.setEnabled(false);
449 fire.addActionListener(this);
450 fire.setActionCommand("fire");
457 c.anchor = GridBagConstraints.CENTER;
458 close = new JButton ("Close");
460 close.addActionListener(this);
461 close.setActionCommand("close");
464 setLocationRelativeTo(owner);
466 addWindowListener(new ConfigListener(this));
469 public AltosIgniteUI(JFrame in_owner) {
476 send_command("get_npyro");