altoslib: Add TeleMini v3 support
[fw/altos] / altoslib / AltosIdleFetch.java
1 /*
2  * Copyright © 2013 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_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 class AltosIdler {
27         String  prefix;
28         int[]   idlers;
29
30         static final int        idle_gps = 0;
31         static final int        idle_imu = 1;
32         static final int        idle_mag = 2;
33         static final int        idle_ms5607 = 3;
34         static final int        idle_mma655x = 4;
35
36
37         static final int        idle_sensor_tm = 10;
38         static final int        idle_sensor_metrum = 11;
39         static final int        idle_sensor_mega = 12;
40         static final int        idle_sensor_emini = 13;
41         static final int        idle_sensor_tmini2 = 14;
42         static final int        idle_sensor_tgps = 15;
43         static final int        idle_sensor_tmini3 = 16;
44
45         public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException, AltosUnknownProduct {
46                 for (int idler : idlers) {
47                         AltosIdle idle = null;
48                         switch (idler) {
49                         case idle_gps:
50                                 AltosGPS.update_state(state, link, config_data);
51                                 break;
52                         case idle_imu:
53                                 AltosIMU.update_state(state, link, config_data);
54                                 break;
55                         case idle_mag:
56                                 AltosMag.update_state(state, link, config_data);
57                                 break;
58                         case idle_ms5607:
59                                 AltosMs5607.update_state(state, link, config_data);
60                                 break;
61                         case idle_mma655x:
62                                 AltosMma655x.update_state(state, link, config_data);
63                                 break;
64                         case idle_sensor_tm:
65                                 AltosSensorTM.update_state(state, link, config_data);
66                                 break;
67                         case idle_sensor_metrum:
68                                 AltosSensorMetrum.update_state(state, link, config_data);
69                                 break;
70                         case idle_sensor_mega:
71                                 AltosSensorMega.update_state(state, link, config_data);
72                                 break;
73                         case idle_sensor_emini:
74                                 AltosSensorEMini.update_state(state, link, config_data);
75                                 break;
76                         case idle_sensor_tmini2:
77                                 AltosSensorTMini.update_state(state, link, config_data);
78                                 break;
79                         case idle_sensor_tgps:
80                                 AltosSensorTGPS.update_state(state, link, config_data);
81                                 break;
82                         case idle_sensor_tmini3:
83                                 AltosSensorTMini3.update_state(state, link, config_data);
84                                 break;
85                         }
86                         if (idle != null)
87                                 idle.update_state(state);
88                 }
89         }
90
91         public boolean matches(AltosConfigData config_data) {
92                 return config_data.product.startsWith(prefix);
93         }
94
95         public AltosIdler(String prefix, int ... idlers) {
96                 this.prefix = prefix;
97                 this.idlers = idlers;
98         }
99 }
100
101
102 public class AltosIdleFetch implements AltosStateUpdate {
103
104         static final AltosIdler[] idlers = {
105
106                 new AltosIdler("EasyMini",
107                                AltosIdler.idle_ms5607,
108                                AltosIdler.idle_sensor_emini),
109
110                 new AltosIdler("TeleMini-v1",
111                                AltosIdler.idle_sensor_tm),
112
113                 new AltosIdler("TeleMini-v2",
114                                AltosIdler.idle_ms5607,
115                                AltosIdler.idle_sensor_tmini2),
116
117                 new AltosIdler("TeleMini-v3",
118                                AltosIdler.idle_ms5607,
119                                AltosIdler.idle_sensor_tmini3),
120
121                 new AltosIdler("TeleMetrum-v1",
122                                AltosIdler.idle_gps,
123                                AltosIdler.idle_sensor_tm),
124
125                 new AltosIdler("TeleMetrum-v2",
126                                AltosIdler.idle_gps,
127                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
128                                AltosIdler.idle_sensor_metrum),
129
130                 new AltosIdler("TeleMega",
131                                AltosIdler.idle_gps,
132                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
133                                AltosIdler.idle_imu, AltosIdler.idle_mag,
134                                AltosIdler.idle_sensor_mega),
135                 new AltosIdler("EasyMega",
136                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
137                                AltosIdler.idle_imu, AltosIdler.idle_mag,
138                                AltosIdler.idle_sensor_mega),
139                 new AltosIdler("TeleGPS",
140                                AltosIdler.idle_gps,
141                                AltosIdler.idle_sensor_tgps),
142         };
143
144         AltosLink               link;
145
146         public void update_state(AltosState state) throws InterruptedException, AltosUnknownProduct {
147                 try {
148                         boolean matched = false;
149                         /* Fetch config data from remote */
150                         AltosConfigData config_data = new AltosConfigData(link);
151                         state.set_state(AltosLib.ao_flight_stateless);
152                         state.set_serial(config_data.serial);
153                         state.set_callsign(config_data.callsign);
154                         state.set_ground_accel(config_data.accel_cal_plus);
155                         state.set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
156                         state.set_product(config_data.product);
157                         state.set_firmware_version(config_data.version);
158                         state.set_log_space(config_data.log_space);
159                         for (AltosIdler idler : idlers) {
160                                 if (idler.matches(config_data)) {
161                                         idler.update_state(state, link, config_data);
162                                         matched = true;
163                                         break;
164                                 }
165                         }
166                         if (!matched)
167                                 throw new AltosUnknownProduct(config_data.product);
168                         state.set_received_time(System.currentTimeMillis());
169                 } catch (TimeoutException te) {
170                 }
171
172         }
173
174         public AltosIdleFetch(AltosLink link) {
175                 this.link = link;
176         }
177 }