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