altos/lisp: Fix uninitialized values in ao_lisp_make_const
[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_tmini = 14;
42         static final int        idle_sensor_tgps = 15;
43
44         public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException, AltosUnknownProduct {
45                 for (int idler : idlers) {
46                         AltosIdle idle = null;
47                         switch (idler) {
48                         case idle_gps:
49                                 AltosGPS.update_state(state, link, config_data);
50                                 break;
51                         case idle_imu:
52                                 AltosIMU.update_state(state, link, config_data);
53                                 break;
54                         case idle_mag:
55                                 AltosMag.update_state(state, link, config_data);
56                                 break;
57                         case idle_ms5607:
58                                 AltosMs5607.update_state(state, link, config_data);
59                                 break;
60                         case idle_mma655x:
61                                 AltosMma655x.update_state(state, link, config_data);
62                                 break;
63                         case idle_sensor_tm:
64                                 AltosSensorTM.update_state(state, link, config_data);
65                                 break;
66                         case idle_sensor_metrum:
67                                 AltosSensorMetrum.update_state(state, link, config_data);
68                                 break;
69                         case idle_sensor_mega:
70                                 AltosSensorMega.update_state(state, link, config_data);
71                                 break;
72                         case idle_sensor_emini:
73                                 AltosSensorEMini.update_state(state, link, config_data);
74                                 break;
75                         case idle_sensor_tmini:
76                                 AltosSensorTMini.update_state(state, link, config_data);
77                                 break;
78                         case idle_sensor_tgps:
79                                 AltosSensorTGPS.update_state(state, link, config_data);
80                                 break;
81                         }
82                         if (idle != null)
83                                 idle.update_state(state);
84                 }
85         }
86
87         public boolean matches(AltosConfigData config_data) {
88                 return config_data.product.startsWith(prefix);
89         }
90
91         public AltosIdler(String prefix, int ... idlers) {
92                 this.prefix = prefix;
93                 this.idlers = idlers;
94         }
95 }
96
97
98 public class AltosIdleFetch implements AltosStateUpdate {
99
100         static final AltosIdler[] idlers = {
101
102                 new AltosIdler("EasyMini",
103                                AltosIdler.idle_ms5607,
104                                AltosIdler.idle_sensor_emini),
105
106                 new AltosIdler("TeleMini-v1",
107                                AltosIdler.idle_sensor_tm),
108
109                 new AltosIdler("TeleMini-v2",
110                                AltosIdler.idle_ms5607,
111                                AltosIdler.idle_sensor_tmini),
112
113                 new AltosIdler("TeleMetrum-v1",
114                                AltosIdler.idle_gps,
115                                AltosIdler.idle_sensor_tm),
116
117                 new AltosIdler("TeleMetrum-v2",
118                                AltosIdler.idle_gps,
119                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
120                                AltosIdler.idle_sensor_metrum),
121
122                 new AltosIdler("TeleMega",
123                                AltosIdler.idle_gps,
124                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
125                                AltosIdler.idle_imu, AltosIdler.idle_mag,
126                                AltosIdler.idle_sensor_mega),
127                 new AltosIdler("EasyMega",
128                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
129                                AltosIdler.idle_imu, AltosIdler.idle_mag,
130                                AltosIdler.idle_sensor_mega),
131                 new AltosIdler("TeleGPS",
132                                AltosIdler.idle_gps,
133                                AltosIdler.idle_sensor_tgps),
134         };
135
136         AltosLink               link;
137
138         public void update_state(AltosState state) throws InterruptedException, AltosUnknownProduct {
139                 try {
140                         boolean matched = false;
141                         /* Fetch config data from remote */
142                         AltosConfigData config_data = new AltosConfigData(link);
143                         state.set_state(AltosLib.ao_flight_stateless);
144                         state.set_serial(config_data.serial);
145                         state.set_callsign(config_data.callsign);
146                         state.set_ground_accel(config_data.accel_cal_plus);
147                         state.set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
148                         state.set_product(config_data.product);
149                         state.set_firmware_version(config_data.version);
150                         state.set_log_space(config_data.log_space);
151                         for (AltosIdler idler : idlers) {
152                                 if (idler.matches(config_data)) {
153                                         idler.update_state(state, link, config_data);
154                                         matched = true;
155                                         break;
156                                 }
157                         }
158                         if (!matched)
159                                 throw new AltosUnknownProduct(config_data.product);
160                         state.set_received_time(System.currentTimeMillis());
161                 } catch (TimeoutException te) {
162                 }
163
164         }
165
166         public AltosIdleFetch(AltosLink link) {
167                 this.link = link;
168         }
169 }