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