import java.util.*;
import java.text.*;
import java.util.prefs.*;
-import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.*;
import libaltosJNI.*;
}
}
- void get_data() throws InterruptedException {
+ void get_data() throws InterruptedException, TimeoutException {
try {
start_serial();
serial_line.printf("c s\nv\n");
for (;;) {
- String line = serial_line.get_reply();
+ String line = serial_line.get_reply(1000);
+ if (line == null)
+ throw new TimeoutException();
get_int(line, "serial-number", serial);
get_int(line, "Main deploy:", main_deploy);
get_int(line, "Apogee delay:", apogee_delay);
}
}
- void init_ui () {
+ void init_ui () throws InterruptedException, TimeoutException {
config_ui = new AltosConfigUI(owner);
config_ui.addActionListener(this);
set_ui();
}
- void set_ui() {
- try {
- if (serial_line != null)
- get_data();
- config_ui.set_serial(serial.get());
- config_ui.set_product(product.get());
- config_ui.set_version(version.get());
- config_ui.set_main_deploy(main_deploy.get());
- config_ui.set_apogee_delay(apogee_delay.get());
- config_ui.set_radio_channel(radio_channel.get());
- config_ui.set_radio_calibration(radio_calibration.get());
- config_ui.set_callsign(callsign.get());
- config_ui.set_clean();
- } catch (InterruptedException ie) {
- }
+ void abort() {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Connection to \"%s\" failed",
+ device.toString()),
+ "Connection Failed",
+ JOptionPane.ERROR_MESSAGE);
+ serial_line.close();
+ serial_line = null;
+ }
+
+ void set_ui() throws InterruptedException, TimeoutException {
+ if (serial_line != null)
+ get_data();
+ config_ui.set_serial(serial.get());
+ config_ui.set_product(product.get());
+ config_ui.set_version(version.get());
+ config_ui.set_main_deploy(main_deploy.get());
+ config_ui.set_apogee_delay(apogee_delay.get());
+ config_ui.set_radio_channel(radio_channel.get());
+ config_ui.set_radio_calibration(radio_calibration.get());
+ config_ui.set_callsign(callsign.get());
+ config_ui.set_clean();
}
void run_dialog() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
- if (cmd.equals("Save")) {
- save_data();
- set_ui();
- } else if (cmd.equals("Reset")) {
- set_ui();
- } else if (cmd.equals("Reboot")) {
- if (serial_line != null) {
- try {
+ try {
+ if (cmd.equals("Save")) {
+ save_data();
+ set_ui();
+ } else if (cmd.equals("Reset")) {
+ set_ui();
+ } else if (cmd.equals("Reboot")) {
+ if (serial_line != null) {
start_serial();
serial_line.printf("r eboot\n");
- } catch (InterruptedException ie) {
- } finally {
- try {
- stop_serial();
- } catch (InterruptedException ie) {
- }
+ serial_line.flush_output();
+ stop_serial();
+ serial_line.close();
}
- serial_line.close();
+ } else if (cmd.equals("Close")) {
+ if (serial_line != null)
+ serial_line.close();
}
- } else if (cmd.equals("Close")) {
- if (serial_line != null)
- serial_line.close();
+ } catch (InterruptedException ie) {
+ abort();
+ } catch (TimeoutException te) {
+ abort();
}
}
try {
init_ui();
config_ui.make_visible();
-// } catch (InterruptedException ie) {
- } finally {
+ } catch (InterruptedException ie) {
+ abort();
+ } catch (TimeoutException te) {
+ abort();
}
}
} catch (FileNotFoundException ee) {
JOptionPane.showMessageDialog(owner,
String.format("Cannot open device \"%s\"",
- device.getPath()),
+ device.toString()),
"Cannot open target device",
JOptionPane.ERROR_MESSAGE);
} catch (AltosSerialInUseException si) {
JOptionPane.showMessageDialog(owner,
String.format("Device \"%s\" already in use",
- device.getPath()),
+ device.toString()),
"Device in use",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ee) {
JOptionPane.showMessageDialog(owner,
- device.getPath(),
+ device.toString(),
ee.getLocalizedMessage(),
JOptionPane.ERROR_MESSAGE);
}
import java.util.*;
import java.text.*;
import java.util.prefs.*;
-import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.*;
import libaltosJNI.*;
Thread eeprom_thread;
AltosEepromMonitor monitor;
- void CaptureLog() throws IOException, InterruptedException {
+ void CaptureLog() throws IOException, InterruptedException, TimeoutException {
int serial = 0;
int block, state_block = 0;
int addr;
/* Pull the serial number out of the version information */
for (;;) {
- String line = serial_line.get_reply();
+ String line = serial_line.get_reply(1000);
+ if (line == null)
+ throw new TimeoutException();
if (line.startsWith("serial-number")) {
try {
serial = Integer.parseInt(line.substring(13).trim());
any_valid = false;
monitor.set_value(state_names[state], state, block - state_block);
for (addr = 0; addr < 0x100;) {
- String line = serial_line.get_reply();
+ String line = serial_line.get_reply(1000);
+ if (line == null)
+ throw new TimeoutException();
int[] values = ParseHex(line);
if (values == null) {
CaptureLog();
} catch (IOException ee) {
JOptionPane.showMessageDialog(frame,
- device.getPath(),
+ device.toString(),
ee.getLocalizedMessage(),
JOptionPane.ERROR_MESSAGE);
} catch (InterruptedException ie) {
+ } catch (TimeoutException te) {
+ JOptionPane.showMessageDialog(frame,
+ String.format("Connection to \"%s\" failed",
+ device.toString()),
+ "Connection Failed",
+ JOptionPane.ERROR_MESSAGE);
}
if (remote)
serial_line.printf("~");
} catch (FileNotFoundException ee) {
JOptionPane.showMessageDialog(frame,
String.format("Cannot open device \"%s\"",
- device.getPath()),
+ device.toString()),
"Cannot open target device",
JOptionPane.ERROR_MESSAGE);
} catch (AltosSerialInUseException si) {
JOptionPane.showMessageDialog(frame,
String.format("Device \"%s\" already in use",
- device.getPath()),
+ device.toString()),
"Device in use",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ee) {
JOptionPane.showMessageDialog(frame,
- device.getPath(),
+ device.toString(),
ee.getLocalizedMessage(),
JOptionPane.ERROR_MESSAGE);
}
} catch (AltosSerialInUseException si) {
JOptionPane.showMessageDialog(frame,
String.format("Device \"%s\" already in use",
- debug_dongle.getPath()),
+ debug_dongle.toString()),
"Device in use",
JOptionPane.ERROR_MESSAGE);
} catch (IOException e) {
package altosui;
import java.io.*;
+import java.util.concurrent.*;
public class AltosIgnite {
AltosDevice device;
return Unknown;
}
- public int status(int igniter) {
+ public int status(int igniter) throws InterruptedException, TimeoutException {
int status = Unknown;
if (serial == null)
return status;
string_ref status_name = new string_ref();
- try {
- start_serial();
- serial.printf("t\n");
- for (;;) {
- String line = serial.get_reply();
- if (get_string(line, "Igniter: drogue Status: ", status_name))
- if (igniter == Apogee)
- status = status(status_name.get());
- if (get_string(line, "Igniter: main Status: ", status_name)) {
- if (igniter == Main)
- status = status(status_name.get());
- break;
- }
- }
- } catch (InterruptedException ie) {
- } finally {
- try {
- stop_serial();
- } catch (InterruptedException ie) {
+ start_serial();
+ serial.printf("t\n");
+ for (;;) {
+ String line = serial.get_reply(1000);
+ if (line == null)
+ throw new TimeoutException();
+ if (get_string(line, "Igniter: drogue Status: ", status_name))
+ if (igniter == Apogee)
+ status = status(status_name.get());
+ if (get_string(line, "Igniter: main Status: ", status_name)) {
+ if (igniter == Main)
+ status = status(status_name.get());
+ break;
}
}
+ stop_serial();
return status;
}
+ public String status_string(int status) {
+ switch (status) {
+ case Unknown: return "Unknown";
+ case Ready: return "Ready";
+ case Active: return "Active";
+ case Open: return "Open";
+ default: return "Unknown";
+ }
+ }
+
public void fire(int igniter) {
if (serial == null)
return;
}
}
+ public void close() {
+ serial.close();
+ serial = null;
+ }
+
public AltosIgnite(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
device = in_device;
- serial = null;
-// serial = new AltosSerial(device);
+ serial = new AltosSerial(device);
remote = false;
-// if (!device.matchProduct(AltosDevice.product_telemetrum))
-// remote = true;
+ if (!device.matchProduct(AltosDevice.product_telemetrum))
+ remote = true;
}
}
\ No newline at end of file
import java.util.*;
import java.text.*;
import java.util.prefs.*;
+import java.util.concurrent.*;
public class AltosIgniteUI
extends JDialog
implements ActionListener
{
+ AltosDevice device;
+ AltosIgnite ignite;
JFrame owner;
JLabel label;
JRadioButton apogee;
+ JLabel apogee_status_label;
JRadioButton main;
+ JLabel main_status_label;
JToggleButton arm;
JButton fire;
javax.swing.Timer timer;
+ int apogee_status;
+ int main_status;
+
final static int timeout = 1 * 1000;
int time_remaining;
+ boolean timer_running;
void set_arm_text() {
if (arm.isSelected())
void start_timer() {
time_remaining = 10;
set_arm_text();
- timer.restart();
+ timer_running = true;
}
void stop_timer() {
arm.setSelected(false);
arm.setEnabled(false);
fire.setEnabled(false);
- timer.stop();
+ timer_running = false;
set_arm_text();
}
stop_timer();
}
+ void get_ignite_status() throws InterruptedException, TimeoutException {
+ apogee_status = ignite.status(AltosIgnite.Apogee);
+ main_status = ignite.status(AltosIgnite.Main);
+ }
+
+ void set_ignite_status() throws InterruptedException, TimeoutException {
+ get_ignite_status();
+ apogee_status_label.setText(String.format("\"%s\"", ignite.status_string(apogee_status)));
+ main_status_label.setText(String.format("\"%s\"", ignite.status_string(main_status)));
+ }
+
+ void close() {
+ timer.stop();
+ setVisible(false);
+ ignite.close();
+ }
+
+ void abort() {
+ close();
+ JOptionPane.showMessageDialog(owner,
+ String.format("Connection to \"%s\" failed",
+ device.toString()),
+ "Connection Failed",
+ JOptionPane.ERROR_MESSAGE);
+ }
+
void tick_timer() {
- --time_remaining;
- if (time_remaining <= 0)
- cancel();
- else
- set_arm_text();
+ if (timer_running) {
+ --time_remaining;
+ if (time_remaining <= 0)
+ cancel();
+ else
+ set_arm_text();
+ }
+ try {
+ set_ignite_status();
+ } catch (InterruptedException ie) {
+ abort();
+ } catch (TimeoutException te) {
+ abort();
+ }
}
void fire() {
igniter = AltosIgnite.Apogee;
else if (main.isSelected() && !apogee.isSelected())
igniter = AltosIgnite.Main;
- System.out.printf ("fire %d\n", igniter);
+ ignite.fire(igniter);
cancel();
}
}
String cmd = e.getActionCommand();
if (cmd.equals("apogee") || cmd.equals("main")) {
stop_timer();
- arm.setEnabled(true);
}
- if (cmd.equals("apogee") && apogee.isSelected())
+ if (cmd.equals("apogee") && apogee.isSelected()) {
main.setSelected(false);
- if (cmd.equals("main") && main.isSelected())
+ if (apogee_status == AltosIgnite.Ready)
+ arm.setEnabled(true);
+ }
+ if (cmd.equals("main") && main.isSelected()) {
apogee.setSelected(false);
+ if (main_status == AltosIgnite.Ready)
+ arm.setEnabled(true);
+ }
if (cmd.equals("arm")) {
if (arm.isSelected()) {
fire();
if (cmd.equals("tick"))
tick_timer();
+ if (cmd.equals("close")) {
+ close();
+ }
+ }
+
+ /* A window listener to catch closing events and tell the config code */
+ class ConfigListener extends WindowAdapter {
+ AltosIgniteUI ui;
+
+ public ConfigListener(AltosIgniteUI this_ui) {
+ ui = this_ui;
+ }
+
+ public void windowClosing(WindowEvent e) {
+ ui.actionPerformed(new ActionEvent(e.getSource(),
+ ActionEvent.ACTION_PERFORMED,
+ "close"));
+ }
+ }
+
+ private boolean open() {
+ device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
+ if (device != null) {
+ try {
+ ignite = new AltosIgnite(device);
+ return true;
+ } catch (FileNotFoundException ee) {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Cannot open device \"%s\"",
+ device.toString()),
+ "Cannot open target device",
+ JOptionPane.ERROR_MESSAGE);
+ } catch (AltosSerialInUseException si) {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Device \"%s\" already in use",
+ device.toString()),
+ "Device in use",
+ JOptionPane.ERROR_MESSAGE);
+ } catch (IOException ee) {
+ JOptionPane.showMessageDialog(owner,
+ device.toString(),
+ ee.getLocalizedMessage(),
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ return false;
}
public AltosIgniteUI(JFrame in_owner) {
+
+ owner = in_owner;
+ apogee_status = AltosIgnite.Unknown;
+ main_status = AltosIgnite.Unknown;
+
+ if (!open())
+ return;
+
Container pane = getContentPane();
GridBagConstraints c = new GridBagConstraints();
Insets i = new Insets(4,4,4,4);
timer = new javax.swing.Timer(timeout, this);
timer.setActionCommand("tick");
+ timer_running = false;
+ timer.restart();
owner = in_owner;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
+ c.anchor = GridBagConstraints.CENTER;
label = new JLabel ("Fire Igniter");
pane.add(label, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
apogee = new JRadioButton ("Apogee");
pane.add(apogee, c);
apogee.addActionListener(this);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
+ apogee_status_label = new JLabel();
+ pane.add(apogee_status_label, c);
+
+ c.gridx = 0;
+ c.gridy = 2;
+ c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
main = new JRadioButton ("Main");
pane.add(main, c);
main.addActionListener(this);
main.setActionCommand("main");
- c.gridx = 0;
+ c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
+ main_status_label = new JLabel();
+ pane.add(main_status_label, c);
+
+ try {
+ set_ignite_status();
+ } catch (InterruptedException ie) {
+ abort();
+ return;
+ } catch (TimeoutException te) {
+ abort();
+ return;
+ }
+
+ c.gridx = 0;
+ c.gridy = 3;
+ c.gridwidth = 1;
+ c.anchor = GridBagConstraints.CENTER;
arm = new JToggleButton ("Arm");
pane.add(arm, c);
arm.addActionListener(this);
arm.setEnabled(false);
c.gridx = 1;
- c.gridy = 2;
+ c.gridy = 3;
c.gridwidth = 1;
+ c.anchor = GridBagConstraints.CENTER;
fire = new JButton ("Fire");
fire.setEnabled(false);
pane.add(fire, c);
pack();
setLocationRelativeTo(owner);
setVisible(true);
+
+ addWindowListener(new ConfigListener(this));
}
}
\ No newline at end of file
return line.line;
}
+ public String get_reply(int timeout) throws InterruptedException {
+ flush_output();
+ AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
+ if (line == null)
+ return null;
+ return line.line;
+ }
+
public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
set_monitor(true);
monitors.add(q);
throw new AltosSerialInUseException(device);
devices_opened.add(device.getPath());
}
- close();
altos = libaltos.altos_open(device);
if (altos == null)
- throw new FileNotFoundException(device.getPath());
+ throw new FileNotFoundException(device.toString());
input_thread = new Thread(this);
input_thread.start();
print("~\nE 0\n");
device = in_device;
serial = new AltosSerial(device);
log = new AltosLog(serial);
- name = device.getPath();
+ name = device.toString();
telem = new LinkedBlockingQueue<AltosLine>();
serial.add_monitor(telem);
} catch (FileNotFoundException ee) {
JOptionPane.showMessageDialog(AltosUI.this,
String.format("Cannot open device \"%s\"",
- device.getPath()),
+ device.toString()),
"Cannot open target device",
JOptionPane.ERROR_MESSAGE);
} catch (AltosSerialInUseException si) {
JOptionPane.showMessageDialog(AltosUI.this,
String.format("Device \"%s\" already in use",
- device.getPath()),
+ device.toString()),
"Device in use",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ee) {
JOptionPane.showMessageDialog(AltosUI.this,
- device.getPath(),
+ device.toString(),
"Unkonwn I/O error",
JOptionPane.ERROR_MESSAGE);
}