docs: Document altosui "Graph Data" button
[fw/altos] / ao-tools / altosui / AltosIgnite.java
index 5c27e8fafbcf5f3935046b82c257baa0cacf4e69..3cbd8a751e1e53af63fa54c4de70ffcc3b0bab80 100644 (file)
 package altosui;
 
 import java.io.*;
+import java.util.concurrent.*;
 
 public class AltosIgnite {
        AltosDevice     device;
        AltosSerial     serial;
        boolean         remote;
+       boolean         serial_started;
        final static int        None = 0;
        final static int        Apogee = 1;
        final static int        Main = 2;
@@ -33,15 +35,18 @@ public class AltosIgnite {
        final static int        Open = 3;
 
        private void start_serial() throws InterruptedException {
+               serial_started = true;
                if (remote) {
-                       serial.set_channel(AltosPreferences.channel(device.getSerial()));
-                       serial.set_callsign(AltosPreferences.callsign());
-                       serial.printf("~\np\n");
+                       serial.set_radio();
+                       serial.printf("p\nE 0\n");
                        serial.flush_input();
                }
        }
 
        private void stop_serial() throws InterruptedException {
+               if (!serial_started)
+                       return;
+               serial_started = false;
                if (serial == null)
                        return;
                if (remote) {
@@ -91,35 +96,40 @@ public class AltosIgnite {
                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(5000);
+                       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;
@@ -142,14 +152,22 @@ public class AltosIgnite {
                }
        }
 
+       public void close() {
+               try {
+                       stop_serial();
+               } catch (InterruptedException ie) {
+               }
+               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