X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosui%2FAltosIgniteUI.java;h=5a09252ecc14babaa5d101a7758ae3e33ff4d970;hp=9d063702a3c1112b458e63801cbe45059774a69b;hb=HEAD;hpb=081455dcba860f3e4df8cd66f3fe686b204034ad diff --git a/altosui/AltosIgniteUI.java b/altosui/AltosIgniteUI.java index 9d063702..5a09252e 100644 --- a/altosui/AltosIgniteUI.java +++ b/altosui/AltosIgniteUI.java @@ -3,7 +3,8 @@ * * 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; version 2 of the License. + * the Free Software Foundation; either version 2 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 @@ -24,8 +25,8 @@ import java.io.*; import java.text.*; import java.util.*; import java.util.concurrent.*; -import org.altusmetrum.altoslib_3.*; -import org.altusmetrum.altosuilib_1.*; +import org.altusmetrum.altoslib_14.*; +import org.altusmetrum.altosuilib_14.*; public class AltosIgniteUI extends AltosUIDialog @@ -39,7 +40,9 @@ public class AltosIgniteUI javax.swing.Timer timer; JButton close; ButtonGroup group; + Boolean opened; + boolean has_standard; int npyro; final static int timeout = 1 * 1000; @@ -47,9 +50,9 @@ public class AltosIgniteUI int time_remaining; boolean timer_running; - LinkedBlockingQueue command_queue; + int poll_remaining; - LinkedBlockingQueue reply_queue; + LinkedBlockingQueue command_queue; class Igniter { JRadioButton button; @@ -63,7 +66,7 @@ public class AltosIgniteUI } Igniter(AltosIgniteUI ui, String label, String name, int y) { - Container pane = getContentPane(); + Container pane = getScrollablePane(); GridBagConstraints c = new GridBagConstraints(); Insets i = new Insets(4,4,4,4); @@ -110,6 +113,7 @@ public class AltosIgniteUI class IgniteHandler implements Runnable { AltosIgnite ignite; JFrame owner; + AltosLink link; void send_exception(Exception e) { final Exception f_e = e; @@ -123,10 +127,8 @@ public class AltosIgniteUI public void run () { try { - AltosSerial serial = new AltosSerial(device); - serial.set_frame(owner); - ignite = new AltosIgnite(serial, - !device.matchProduct(Altos.product_altimeter)); + ignite = new AltosIgnite(link, + device.matchProduct(Altos.product_basestation)); } catch (Exception e) { send_exception(e); @@ -150,8 +152,7 @@ public class AltosIgniteUI } reply = "status"; } else if (command.equals("get_npyro")) { - put_reply(String.format("%d", ignite.npyro())); - continue; + reply = String.format("npyro %d %d", ignite.npyro(), ignite.has_standard() ? 1 : 0); } else if (command.equals("quit")) { ignite.close(); break; @@ -172,8 +173,9 @@ public class AltosIgniteUI } } - public IgniteHandler(JFrame in_owner) { + public IgniteHandler(JFrame in_owner, AltosLink in_link) { owner = in_owner; + link = in_link; } } @@ -210,6 +212,13 @@ public class AltosIgniteUI set_ignite_status(); } else if (reply.equals("fired")) { fired(); + } else if (reply.startsWith("npyro")) { + String items[] = reply.split("\\s+"); + npyro = Integer.parseInt(items[1]); + if (npyro == AltosLib.MISSING) + npyro = 0; + has_standard = Integer.parseInt(items[2]) != 0; + make_ui(); } } @@ -249,49 +258,26 @@ public class AltosIgniteUI } } - void put_reply(String reply) { - try { - reply_queue.put(reply); - } catch (Exception ex) { - ignite_exception(ex); - } - } - - String get_reply() { - String reply = ""; - try { - reply = reply_queue.take(); - } catch (Exception ex) { - ignite_exception(ex); - } - return reply; - } - boolean getting_status = false; - boolean visible = false; - void set_ignite_status() { getting_status = false; - if (!visible) { - visible = true; + poll_remaining = 2; + if (!isVisible()) setVisible(true); - } } void poll_ignite_status() { + if (poll_remaining > 0) { + --poll_remaining; + return; + } if (!getting_status) { getting_status = true; send_command("get_status"); } } - int get_npyro() { - send_command("get_npyro"); - String reply = get_reply(); - return Integer.parseInt(reply); - } - boolean firing = false; void start_fire(String which) { @@ -307,8 +293,11 @@ public class AltosIgniteUI } void close() { - send_command("quit"); - timer.stop(); + if (opened) { + send_command("quit"); + } + if (timer != null) + timer.stop(); setVisible(false); dispose(); } @@ -380,28 +369,29 @@ public class AltosIgniteUI private boolean open() { command_queue = new LinkedBlockingQueue(); - reply_queue = new LinkedBlockingQueue(); + opened = false; device = AltosDeviceUIDialog.show(owner, Altos.product_any); if (device != null) { - IgniteHandler handler = new IgniteHandler(owner); + try { + AltosSerial serial = new AltosSerial(device); + serial.set_frame(owner); + IgniteHandler handler = new IgniteHandler(owner, serial); Thread t = new Thread(handler); t.start(); + opened = true; return true; + } catch (Exception ex) { + ignite_exception(ex); + } } return false; } - public AltosIgniteUI(JFrame in_owner) { - - owner = in_owner; - - if (!open()) - return; - + private void make_ui() { group = new ButtonGroup(); - Container pane = getContentPane(); + Container pane = getScrollablePane(); GridBagConstraints c = new GridBagConstraints(); Insets i = new Insets(4,4,4,4); @@ -411,8 +401,6 @@ public class AltosIgniteUI timer_running = false; timer.restart(); - owner = in_owner; - pane.setLayout(new GridBagLayout()); c.fill = GridBagConstraints.NONE; @@ -432,17 +420,21 @@ public class AltosIgniteUI y++; - int npyro = get_npyro(); + int nstandard = 0; + if (has_standard) + nstandard = 2; - igniters = new Igniter[2 + npyro]; + igniters = new Igniter[nstandard + npyro]; - igniters[0] = new Igniter(this, "Apogee", AltosIgnite.Apogee, y++); - igniters[1] = new Igniter(this, "Main", AltosIgnite.Main, y++); + if (has_standard) { + igniters[0] = new Igniter(this, "Apogee", AltosIgnite.Apogee, y++); + igniters[1] = new Igniter(this, "Main", AltosIgnite.Main, y++); + } for (int p = 0; p < npyro; p++) { String name = String.format("%d", p); String label = String.format("%c", 'A' + p); - igniters[2+p] = new Igniter(this, label, name, y++); + igniters[nstandard+p] = new Igniter(this, label, name, y++); } c.gridx = 0; @@ -475,10 +467,20 @@ public class AltosIgniteUI pane.add(close, c); close.addActionListener(this); close.setActionCommand("close"); - + pack(); setLocationRelativeTo(owner); addWindowListener(new ConfigListener(this)); } -} \ No newline at end of file + + public AltosIgniteUI(JFrame in_owner) { + + owner = in_owner; + + if (!open()) + return; + + send_command("get_npyro"); + } +}