42943c07c478e7cfb4f1ae426ef8f254d7e0c5a2
[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; 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 org.altusmetrum.altoslib_2;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 class AltosIdler {
26         String  prefix;
27         int[]   idlers;
28
29         static final int        idle_gps = 0;
30         static final int        idle_imu = 1;
31         static final int        idle_mag = 2;
32         static final int        idle_ms5607 = 3;
33         static final int        idle_mma655x = 4;
34
35
36         static final int        idle_sensor_tm = 10;
37         static final int        idle_sensor_metrum = 11;
38         static final int        idle_sensor_mega = 12;
39         static final int        idle_sensor_emini = 13;
40
41         public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
42                 for (int idler : idlers) {
43                         AltosIdle idle = null;
44                         switch (idler) {
45                         case idle_gps:
46                                 AltosGPS.update_state(state, link, config_data);
47                                 break;
48                         case idle_imu:
49                                 AltosIMU.update_state(state, link, config_data);
50                                 break;
51                         case idle_mag:
52                                 AltosMag.update_state(state, link, config_data);
53                                 break;
54                         case idle_ms5607:
55                                 AltosMs5607.update_state(state, link, config_data);
56                                 break;
57                         case idle_mma655x:
58                                 AltosMma655x.update_state(state, link, config_data);
59                                 break;
60                         case idle_sensor_tm:
61                                 AltosSensorTM.update_state(state, link, config_data);
62                                 break;
63                         case idle_sensor_metrum:
64                                 AltosSensorMetrum.update_state(state, link, config_data);
65                                 break;
66                         case idle_sensor_mega:
67                                 AltosSensorMega.update_state(state, link, config_data);
68                                 break;
69                         case idle_sensor_emini:
70                                 AltosSensorEMini.update_state(state, link, config_data);
71                                 break;
72                         }
73                         if (idle != null)
74                                 idle.update_state(state);
75                 }
76         }
77
78         public boolean matches(AltosConfigData config_data) {
79                 return config_data.product.startsWith(prefix);
80         }
81
82         public AltosIdler(String prefix, int ... idlers) {
83                 this.prefix = prefix;
84                 this.idlers = idlers;
85         }
86 }
87
88
89 public class AltosIdleFetch implements AltosStateUpdate {
90
91         static final AltosIdler[] idlers = {
92
93                 new AltosIdler("EasyMini",
94                                AltosIdler.idle_ms5607,
95                                AltosIdler.idle_sensor_emini),
96
97                 new AltosIdler("TeleMini-v1",
98                                AltosIdler.idle_sensor_tm),
99
100                 new AltosIdler("TeleMini-v2",
101                                AltosIdler.idle_ms5607,
102                                AltosIdler.idle_sensor_tm),
103
104                 new AltosIdler("TeleMetrum-v1",
105                                AltosIdler.idle_gps,
106                                AltosIdler.idle_sensor_tm),
107
108                 new AltosIdler("TeleMetrum-v2",
109                                AltosIdler.idle_gps,
110                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
111                                AltosIdler.idle_sensor_metrum),
112
113                 new AltosIdler("TeleMega",
114                                AltosIdler.idle_gps,
115                                AltosIdler.idle_ms5607, AltosIdler.idle_mma655x,
116                                AltosIdler.idle_imu, AltosIdler.idle_mag,
117                                AltosIdler.idle_sensor_mega),
118         };
119
120         AltosLink               link;
121
122         double                  frequency;
123         String                  callsign;
124
125         public void update_state(AltosState state) {
126                 try {
127                         AltosConfigData config_data = new AltosConfigData(link);
128                         state.set_state(AltosLib.ao_flight_startup);
129                         state.set_serial(config_data.serial);
130                         state.set_callsign(config_data.callsign);
131                         state.set_ground_accel(config_data.accel_cal_plus);
132                         state.set_accel_g(config_data.accel_cal_plus, config_data.accel_cal_minus);
133                         for (AltosIdler idler : idlers) {
134                                 if (idler.matches(config_data)) {
135                                         idler.update_state(state, link, config_data);
136                                         break;
137                                 }
138                         }
139                         state.set_received_time(System.currentTimeMillis());
140                 } catch (InterruptedException ie) {
141                 } catch (TimeoutException te) {
142                 }
143                 
144         }
145
146         public AltosIdleFetch(AltosLink link) {
147                 this.link = link;
148         }
149 }