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; either version 2 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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 import java.awt.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_14.*;
29 import org.altusmetrum.altosuilib_14.*;
31 public class AltosIgniteUI
33 implements ActionListener
40 javax.swing.Timer timer;
48 final static int timeout = 1 * 1000;
51 boolean timer_running;
55 LinkedBlockingQueue<String> command_queue;
63 void set_status (int status) {
65 status_label.setText(String.format("\"%s\"", AltosIgnite.status_string(status)));
68 Igniter(AltosIgniteUI ui, String label, String name, int y) {
69 Container pane = getScrollablePane();
70 GridBagConstraints c = new GridBagConstraints();
71 Insets i = new Insets(4,4,4,4);
74 this.status = AltosIgnite.Unknown;
79 c.anchor = GridBagConstraints.WEST;
80 button = new JRadioButton (label);
82 button.addActionListener(ui);
83 button.setActionCommand(name);
89 c.anchor = GridBagConstraints.WEST;
90 status_label = new JLabel("plenty of text");
91 pane.add(status_label, c);
93 status = AltosIgnite.Unknown;
99 void set_status(String _name, int _status) {
101 final String name = _name;
102 final int status = _status;
103 Runnable r = new Runnable() {
105 for (int p = 0; p < igniters.length; p++)
106 if (name.equals(igniters[p].name))
107 igniters[p].set_status(status);
110 SwingUtilities.invokeLater(r);
113 class IgniteHandler implements Runnable {
118 void send_exception(Exception e) {
119 final Exception f_e = e;
120 Runnable r = new Runnable() {
122 ignite_exception(f_e);
125 SwingUtilities.invokeLater(r);
130 ignite = new AltosIgnite(link,
131 device.matchProduct(Altos.product_basestation));
133 } catch (Exception e) {
142 String command = command_queue.take();
145 if (command.equals("get_status")) {
146 HashMap<String,Integer> status_map = ignite.status();
148 for (int p = 0; p < igniters.length; p++) {
149 Integer i = status_map.get(igniters[p].name);
151 set_status(igniters[p].name, i);
154 } else if (command.equals("get_npyro")) {
155 reply = String.format("npyro %d %d", ignite.npyro(), ignite.has_standard() ? 1 : 0);
156 } else if (command.equals("quit")) {
160 ignite.fire(command);
163 final String f_reply = reply;
166 ignite_reply(f_reply);
169 SwingUtilities.invokeLater(r);
170 } catch (Exception e) {
176 public IgniteHandler(JFrame in_owner, AltosLink in_link) {
182 void ignite_exception(Exception e) {
183 if (e instanceof FileNotFoundException) {
184 JOptionPane.showMessageDialog(owner,
185 ((FileNotFoundException) e).getMessage(),
186 "Cannot open target device",
187 JOptionPane.ERROR_MESSAGE);
188 } else if (e instanceof AltosSerialInUseException) {
189 JOptionPane.showMessageDialog(owner,
190 String.format("Device \"%s\" already in use",
191 device.toShortString()),
193 JOptionPane.ERROR_MESSAGE);
194 } else if (e instanceof IOException) {
195 IOException ee = (IOException) e;
196 JOptionPane.showMessageDialog(owner,
197 device.toShortString(),
198 ee.getLocalizedMessage(),
199 JOptionPane.ERROR_MESSAGE);
201 JOptionPane.showMessageDialog(owner,
202 String.format("Connection to \"%s\" failed",
203 device.toShortString()),
205 JOptionPane.ERROR_MESSAGE);
210 void ignite_reply(String reply) {
211 if (reply.equals("status")) {
213 } else if (reply.equals("fired")) {
215 } else if (reply.startsWith("npyro")) {
216 String items[] = reply.split("\\s+");
217 npyro = Integer.parseInt(items[1]);
218 if (npyro == AltosLib.MISSING)
220 has_standard = Integer.parseInt(items[2]) != 0;
225 void set_arm_text() {
226 if (arm.isSelected())
227 arm.setText(String.format("%d", time_remaining));
235 timer_running = true;
240 fire.setEnabled(false);
241 timer_running = false;
242 arm.setSelected(false);
243 arm.setEnabled(false);
248 group.clearSelection();
249 fire.setEnabled(false);
253 void send_command(String command) {
255 command_queue.put(command);
256 } catch (Exception ex) {
257 ignite_exception(ex);
261 boolean getting_status = false;
263 void set_ignite_status() {
264 getting_status = false;
270 void poll_ignite_status() {
271 if (poll_remaining > 0) {
275 if (!getting_status) {
276 getting_status = true;
277 send_command("get_status");
281 boolean firing = false;
283 void start_fire(String which) {
297 send_command("quit");
308 if (time_remaining <= 0)
313 poll_ignite_status();
317 if (arm.isEnabled() && arm.isSelected() && time_remaining > 0) {
318 String igniter = "none";
320 for (int p = 0; p < igniters.length; p++)
321 if (igniters[p].button.isSelected()) {
322 igniter = igniters[p].name;
325 send_command(igniter);
330 public void actionPerformed(ActionEvent e) {
331 String cmd = e.getActionCommand();
333 for (int p = 0; p < igniters.length; p++)
334 if (cmd.equals(igniters[p].name)) {
336 arm.setEnabled(true);
340 if (cmd.equals("arm")) {
341 if (arm.isSelected()) {
342 fire.setEnabled(true);
347 if (cmd.equals("fire"))
349 if (cmd.equals("tick"))
351 if (cmd.equals("close"))
355 /* A window listener to catch closing events and tell the config code */
356 class ConfigListener extends WindowAdapter {
359 public ConfigListener(AltosIgniteUI this_ui) {
363 public void windowClosing(WindowEvent e) {
364 ui.actionPerformed(new ActionEvent(e.getSource(),
365 ActionEvent.ACTION_PERFORMED,
370 private boolean open() {
371 command_queue = new LinkedBlockingQueue<String>();
374 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
375 if (device != null) {
377 AltosSerial serial = new AltosSerial(device);
378 serial.set_frame(owner);
379 IgniteHandler handler = new IgniteHandler(owner, serial);
380 Thread t = new Thread(handler);
384 } catch (Exception ex) {
385 ignite_exception(ex);
391 private void make_ui() {
392 group = new ButtonGroup();
394 Container pane = getScrollablePane();
396 GridBagConstraints c = new GridBagConstraints();
397 Insets i = new Insets(4,4,4,4);
399 timer = new javax.swing.Timer(timeout, this);
400 timer.setActionCommand("tick");
401 timer_running = false;
404 pane.setLayout(new GridBagLayout());
406 c.fill = GridBagConstraints.NONE;
407 c.anchor = GridBagConstraints.CENTER;
417 c.anchor = GridBagConstraints.CENTER;
418 label = new JLabel ("Fire Igniter");
427 igniters = new Igniter[nstandard + npyro];
430 igniters[0] = new Igniter(this, "Apogee", AltosIgnite.Apogee, y++);
431 igniters[1] = new Igniter(this, "Main", AltosIgnite.Main, y++);
434 for (int p = 0; p < npyro; p++) {
435 String name = String.format("%d", p);
436 String label = String.format("%c", 'A' + p);
437 igniters[nstandard+p] = new Igniter(this, label, name, y++);
443 c.anchor = GridBagConstraints.CENTER;
444 arm = new JToggleButton ("Arm");
446 arm.addActionListener(this);
447 arm.setActionCommand("arm");
448 arm.setEnabled(false);
453 c.anchor = GridBagConstraints.CENTER;
454 fire = new JButton ("Fire");
455 fire.setEnabled(false);
457 fire.addActionListener(this);
458 fire.setActionCommand("fire");
465 c.anchor = GridBagConstraints.CENTER;
466 close = new JButton ("Close");
468 close.addActionListener(this);
469 close.setActionCommand("close");
472 setLocationRelativeTo(owner);
474 addWindowListener(new ConfigListener(this));
477 public AltosIgniteUI(JFrame in_owner) {
484 send_command("get_npyro");