altos: Support staging by going back to boost as needed
[fw/altos] / altosui / AltosIgnite.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.io.*;
21 import java.util.concurrent.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.filechooser.FileNameExtensionFilter;
26 import javax.swing.table.*;
27 import javax.swing.event.*;
28
29 public class AltosIgnite {
30         AltosDevice     device;
31         AltosSerial     serial;
32         boolean         remote;
33         boolean         serial_started;
34         final static int        None = 0;
35         final static int        Apogee = 1;
36         final static int        Main = 2;
37
38         final static int        Unknown = 0;
39         final static int        Ready = 1;
40         final static int        Active = 2;
41         final static int        Open = 3;
42
43         private void start_serial() throws InterruptedException, TimeoutException {
44                 serial_started = true;
45                 if (remote)
46                         serial.start_remote();
47         }
48
49         private void stop_serial() throws InterruptedException {
50                 if (!serial_started)
51                         return;
52                 serial_started = false;
53                 if (serial == null)
54                         return;
55                 if (remote)
56                         serial.stop_remote();
57         }
58
59         class string_ref {
60                 String  value;
61
62                 public String get() {
63                         return value;
64                 }
65                 public void set(String i) {
66                         value = i;
67                 }
68                 public string_ref() {
69                         value = null;
70                 }
71         }
72
73         private boolean get_string(String line, String label, string_ref s) {
74                 if (line.startsWith(label)) {
75                         String  quoted = line.substring(label.length()).trim();
76
77                         if (quoted.startsWith("\""))
78                                 quoted = quoted.substring(1);
79                         if (quoted.endsWith("\""))
80                                 quoted = quoted.substring(0,quoted.length()-1);
81                         s.set(quoted);
82                         return true;
83                 } else {
84                         return false;
85                 }
86         }
87
88         private int status(String status_name) {
89                 if (status_name.equals("unknown"))
90                         return Unknown;
91                 if (status_name.equals("ready"))
92                         return Ready;
93                 if (status_name.equals("active"))
94                         return Active;
95                 if (status_name.equals("open"))
96                         return Open;
97                 return Unknown;
98         }
99
100         public int status(int igniter) throws InterruptedException, TimeoutException {
101                 int status = Unknown;
102                 if (serial == null)
103                         return status;
104                 string_ref status_name = new string_ref();
105                 try {
106                         start_serial();
107                         serial.printf("t\n");
108                         for (;;) {
109                                 String line = serial.get_reply(5000);
110                                 if (line == null)
111                                         throw new TimeoutException();
112                                 if (get_string(line, "Igniter: drogue Status: ", status_name))
113                                         if (igniter == Apogee)
114                                                 status = status(status_name.get());
115                                 if (get_string(line, "Igniter:   main Status: ", status_name)) {
116                                         if (igniter == Main)
117                                                 status = status(status_name.get());
118                                         break;
119                                 }
120                         }
121                 } finally {
122                         stop_serial();
123                 }
124                 return status;
125         }
126
127         public static String status_string(int status) {
128                 switch (status) {
129                 case Unknown: return "Unknown";
130                 case Ready: return "Ready";
131                 case Active: return "Active";
132                 case Open: return "Open";
133                 default: return "Unknown";
134                 }
135         }
136
137         public void fire(int igniter) {
138                 if (serial == null)
139                         return;
140                 try {
141                         start_serial();
142                         switch (igniter) {
143                         case Main:
144                                 serial.printf("i DoIt main\n");
145                                 break;
146                         case Apogee:
147                                 serial.printf("i DoIt drogue\n");
148                                 break;
149                         }
150                 } catch (InterruptedException ie) {
151                 } catch (TimeoutException te) {
152                 } finally {
153                         try {
154                                 stop_serial();
155                         } catch (InterruptedException ie) {
156                         }
157                 }
158         }
159
160         public void close() {
161                 try {
162                         stop_serial();
163                 } catch (InterruptedException ie) {
164                 }
165                 serial.close();
166                 serial = null;
167         }
168
169         public void set_frame(Frame frame) {
170                 serial.set_frame(frame);
171         }
172
173         public AltosIgnite(AltosDevice in_device)
174                 throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException {
175
176                 device = in_device;
177                 serial = new AltosSerial(device);
178                 remote = false;
179
180                 if (!device.matchProduct(Altos.product_telemetrum))
181                         remote = true;
182         }
183 }