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