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