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