5c27e8fafbcf5f3935046b82c257baa0cacf4e69
[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
22 public class AltosIgnite {
23         AltosDevice     device;
24         AltosSerial     serial;
25         boolean         remote;
26         final static int        None = 0;
27         final static int        Apogee = 1;
28         final static int        Main = 2;
29
30         final static int        Unknown = 0;
31         final static int        Ready = 1;
32         final static int        Active = 2;
33         final static int        Open = 3;
34
35         private void start_serial() throws InterruptedException {
36                 if (remote) {
37                         serial.set_channel(AltosPreferences.channel(device.getSerial()));
38                         serial.set_callsign(AltosPreferences.callsign());
39                         serial.printf("~\np\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) {
95                 int status = Unknown;
96                 if (serial == null)
97                         return status;
98                 string_ref status_name = new string_ref();
99                 try {
100                         start_serial();
101                         serial.printf("t\n");
102                         for (;;) {
103                                 String line = serial.get_reply();
104                                 if (get_string(line, "Igniter: drogue Status: ", status_name))
105                                         if (igniter == Apogee)
106                                                 status = status(status_name.get());
107                                 if (get_string(line, "Igniter:   main Status: ", status_name)) {
108                                         if (igniter == Main)
109                                                 status = status(status_name.get());
110                                         break;
111                                 }
112                         }
113                 } catch (InterruptedException ie) {
114                 } finally {
115                         try {
116                                 stop_serial();
117                         } catch (InterruptedException ie) {
118                         }
119                 }
120                 return status;
121         }
122
123         public void fire(int igniter) {
124                 if (serial == null)
125                         return;
126                 try {
127                         start_serial();
128                         switch (igniter) {
129                         case Main:
130                                 serial.printf("i DoIt main\n");
131                                 break;
132                         case Apogee:
133                                 serial.printf("i DoIt drogue\n");
134                                 break;
135                         }
136                 } catch (InterruptedException ie) {
137                 } finally {
138                         try {
139                                 stop_serial();
140                         } catch (InterruptedException ie) {
141                         }
142                 }
143         }
144
145         public AltosIgnite(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
146
147                 device = in_device;
148                 serial = null;
149 //              serial = new AltosSerial(device);
150                 remote = false;
151
152 //              if (!device.matchProduct(AltosDevice.product_telemetrum))
153 //                      remote = true;
154         }
155 }