altoslib: Fix 8 to 12 bit conversion for Mega pyro voltage data
[fw/altos] / altoslib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 import java.util.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24
25 public class AltosIgnite {
26         AltosLink       link;
27         boolean         remote;
28         boolean         close_on_exit;
29         boolean         link_started;
30         boolean         have_npyro = false;
31         int             npyro;
32         AltosConfigData config_data;
33
34         public final static String      None = null;
35         public final static String      Apogee = "drogue";
36         public final static String      Main = "main";
37
38         public final static int Unknown = 0;
39         public final static int Ready = 1;
40         public final static int Active = 2;
41         public final static int Open = 3;
42
43         private void start_link() throws InterruptedException, TimeoutException {
44                 link_started = true;
45                 if (remote)
46                         link.start_remote();
47         }
48
49         private void stop_link() throws InterruptedException {
50                 if (!link_started)
51                         return;
52                 link_started = false;
53                 if (link == null)
54                         return;
55                 if (remote)
56                         link.stop_remote();
57         }
58
59         class string_ref {
60                 String  value;
61
62                 public String get() {
63                         return value;
64                 }
65                 public void set(String i) {
66                         value = i;
67                 }
68                 public string_ref() {
69                         value = null;
70                 }
71         }
72
73         /*
74         private boolean get_string(String line, String label, string_ref s) {
75                 if (line.startsWith(label)) {
76                         String  quoted = line.substring(label.length()).trim();
77
78                         if (quoted.startsWith("\""))
79                                 quoted = quoted.substring(1);
80                         if (quoted.endsWith("\""))
81                                 quoted = quoted.substring(0,quoted.length()-1);
82                         s.set(quoted);
83                         return true;
84                 } else {
85                         return false;
86                 }
87         }
88         */
89
90         private int map_status(String status_name) {
91                 if (status_name.equals("unknown"))
92                         return Unknown;
93                 if (status_name.equals("ready"))
94                         return Ready;
95                 if (status_name.equals("active"))
96                         return Active;
97                 if (status_name.equals("open"))
98                         return Open;
99                 return Unknown;
100         }
101
102         private void get_npyro() throws InterruptedException, TimeoutException {
103                 if (config_data == null)
104                         config_data = new AltosConfigData(link);
105                 if (config_data != null && config_data.npyro != AltosLib.MISSING)
106                         npyro = config_data.npyro;
107                 else
108                         npyro = 0;
109                 have_npyro = true;
110         }
111
112         public int npyro() throws InterruptedException, TimeoutException {
113                 if (!have_npyro) {
114                         start_link();
115                         get_npyro();
116                         stop_link();
117                 }
118                 return npyro;
119         }
120
121         public HashMap<String,Integer> status() throws InterruptedException, TimeoutException {
122                 HashMap<String,Integer> status = new HashMap<String,Integer>();
123
124                 if (link == null)
125                         return status;
126                 try {
127                         start_link();
128                         get_npyro();
129
130                         String last_igniter = Main;
131                         if (npyro > 0)
132                                 last_igniter = String.format("%d", npyro - 1);
133
134                         link.printf("t\n");
135                         for (;;) {
136                                 String line = link.get_reply(5000);
137                                 if (line == null)
138                                         throw new TimeoutException();
139                                 String[] items = line.split("\\s+");
140
141                                 if (items.length < 4)
142                                         continue;
143
144                                 if (!items[0].equals("Igniter:"))
145                                         continue;
146
147                                 if (!items[2].equals("Status:"))
148                                         continue;
149
150                                 status.put(items[1], map_status(items[3]));
151
152                                 if (items[1].equals(last_igniter))
153                                         break;
154                         }
155                 } finally {
156                         stop_link();
157                 }
158                 return status;
159         }
160
161         public static String status_string(int status) {
162                 switch (status) {
163                 case Unknown: return "Unknown";
164                 case Ready: return "Ready";
165                 case Active: return "Active";
166                 case Open: return "Open";
167                 default: return "Unknown";
168                 }
169         }
170
171         public void fire(String igniter) throws InterruptedException {
172                 if (link == null)
173                         return;
174                 try {
175                         start_link();
176                         link.printf("i DoIt %s\n", igniter);
177                         link.flush_output();
178                 } catch (TimeoutException te) {
179                 } finally {
180                         stop_link();
181                 }
182         }
183
184         public void close() throws InterruptedException {
185                 stop_link();
186                 if (close_on_exit)
187                         link.close();
188                 link = null;
189         }
190
191         public AltosIgnite(AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
192                 link = in_link;
193                 remote = in_remote;
194                 close_on_exit = in_close_on_exit;
195         }
196
197         public AltosIgnite(AltosLink in_link, boolean in_remote) {
198                 this(in_link, in_remote, true);
199         }
200 }