altos: Support staging by going back to boost as needed
[fw/altos] / altosui / AltosLaunch.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 AltosLaunch {
30         AltosDevice     device;
31         AltosSerial     serial;
32         boolean         serial_started;
33         int             launcher_serial;
34         int             launcher_channel;
35         int             rssi;
36
37         final static int        Unknown = -1;
38         final static int        Good = 0;
39         final static int        Bad = 1;
40
41         int             armed;
42         int             igniter;
43
44         private void start_serial() throws InterruptedException {
45                 serial_started = true;
46         }
47
48         private void stop_serial() throws InterruptedException {
49                 if (!serial_started)
50                         return;
51                 serial_started = false;
52                 if (serial == null)
53                         return;
54         }
55
56         class string_ref {
57                 String  value;
58
59                 public String get() {
60                         return value;
61                 }
62                 public void set(String i) {
63                         value = i;
64                 }
65                 public string_ref() {
66                         value = null;
67                 }
68         }
69
70         private boolean get_string(String line, String label, string_ref s) {
71                 if (line.startsWith(label)) {
72                         String  quoted = line.substring(label.length()).trim();
73
74                         if (quoted.startsWith("\""))
75                                 quoted = quoted.substring(1);
76                         if (quoted.endsWith("\""))
77                                 quoted = quoted.substring(0,quoted.length()-1);
78                         s.set(quoted);
79                         return true;
80                 } else {
81                         return false;
82                 }
83         }
84
85         public boolean status() throws InterruptedException, TimeoutException {
86                 boolean ok = false;
87                 if (serial == null)
88                         return false;
89                 string_ref status_name = new string_ref();
90                 start_serial();
91                 serial.printf("l %d %d\n", launcher_serial, launcher_channel);
92                 for (;;) {
93                         String line = serial.get_reply(20000);
94                         if (line == null)
95                                 throw new TimeoutException();
96                         if (get_string(line, "Rssi: ", status_name)) {
97                                 try {
98                                         rssi = Altos.fromdec(status_name.get());
99                                 } catch (NumberFormatException ne) {
100                                 }
101                                 break;
102                         } else if (get_string(line, "Armed: ", status_name)) {
103                                 armed = Good;
104                                 String status = status_name.get();
105                                 if (status.startsWith("igniter good"))
106                                         igniter = Good;
107                                 else if (status.startsWith("igniter bad"))
108                                         igniter = Bad;
109                                 else
110                                         igniter = Unknown;
111                                 ok = true;
112                         } else if (get_string(line, "Disarmed: ", status_name)) {
113                                 armed = Bad;
114                                 if (status_name.get().startsWith("igniter good"))
115                                         igniter = Good;
116                                 else if (status_name.get().startsWith("igniter bad"))
117                                         igniter = Bad;
118                                 else
119                                         igniter = Unknown;
120                                 ok = true;
121                         } else if (get_string(line, "Error ", status_name)) {
122                                 armed = Unknown;
123                                 igniter = Unknown;
124                                 ok = false;
125                                 break;
126                         }
127                 }
128                 stop_serial();
129                 if (!ok) {
130                         armed = Unknown;
131                         igniter = Unknown;
132                 }
133                 return ok;
134         }
135
136         public static String status_string(int status) {
137                 switch (status) {
138                 case Good:
139                         return "good";
140                 case Bad:
141                         return "open";
142                 }
143                 return "unknown";
144         }
145
146         public void arm() {
147                 if (serial == null)
148                         return;
149                 try {
150                         start_serial();
151                         serial.printf("a %d %d\n", launcher_serial, launcher_channel);
152                         serial.flush_output();
153                 } catch (InterruptedException ie) {
154                 } finally {
155                         try {
156                                 stop_serial();
157                         } catch (InterruptedException ie) {
158                         }
159                 }
160         }
161
162         public void fire() {
163                 if (serial == null)
164                         return;
165                 try {
166                         start_serial();
167                         serial.printf("i %d %d\n", launcher_serial, launcher_channel);
168                         serial.flush_output();
169                 } catch (InterruptedException ie) {
170                 } finally {
171                         try {
172                                 stop_serial();
173                         } catch (InterruptedException ie) {
174                         }
175                 }
176         }
177
178         public void close() {
179                 try {
180                         stop_serial();
181                 } catch (InterruptedException ie) {
182                 }
183                 serial.close();
184                 serial = null;
185         }
186
187         public void set_frame(Frame frame) {
188                 serial.set_frame(frame);
189         }
190
191         public void set_remote(int in_serial, int in_channel) {
192                 launcher_serial = in_serial;
193                 launcher_channel = in_channel;
194         }
195
196         public AltosLaunch(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
197
198                 device = in_device;
199                 serial = new AltosSerial(device);
200         }
201 }