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