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