altos: Perform time comparisons using 16-bit arithmetic to handle wrap
[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_12;
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_mma655x = 4;
34         static final int        idle_ms5607 = 5;
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_emini1 = 13;
41         static final int        idle_sensor_emini2 = 14;
42         static final int        idle_sensor_tmini2 = 15;
43         static final int        idle_sensor_tgps = 16;
44         static final int        idle_sensor_tmini3 = 17;
45
46         public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException, TimeoutException, AltosUnknownProduct {
47                 for (int idler : idlers) {
48                         switch (idler) {
49                         case idle_gps:
50                                 AltosGPS.provide_data(listener, link);
51                                 break;
52                         case idle_imu:
53                                 AltosIMU.provide_data(listener, link);
54                                 break;
55                         case idle_mag:
56                                 AltosMag.provide_data(listener, link);
57                                 break;
58                         case idle_mma655x:
59                                 AltosMma655x.provide_data(listener, link);
60                                 break;
61                         case idle_ms5607:
62                                 AltosMs5607.provide_data(listener, link);
63                                 break;
64                         case idle_sensor_tm:
65                                 AltosSensorTM.provide_data(listener, link);
66                                 break;
67                         case idle_sensor_metrum:
68                                 AltosSensorMetrum.provide_data(listener, link);
69                                 break;
70                         case idle_sensor_mega:
71                                 AltosSensorMega.provide_data(listener, link);
72                                 break;
73                         case idle_sensor_emini1:
74                                 AltosSensorEMini.provide_data(listener, link, 1);
75                                 break;
76                         case idle_sensor_emini2:
77                                 AltosSensorEMini.provide_data(listener, link, 2);
78                                 break;
79                         case idle_sensor_tmini2:
80                                 AltosSensorTMini2.provide_data(listener, link);
81                                 break;
82                         case idle_sensor_tgps:
83                                 AltosSensorTGPS.provide_data(listener, link);
84                                 break;
85                         case idle_sensor_tmini3:
86                                 AltosSensorTMini3.provide_data(listener, link);
87                                 break;
88                         }
89                 }
90         }
91
92         public boolean matches(AltosConfigData config_data) {
93                 return config_data.product.startsWith(prefix);
94         }
95
96         public AltosIdler(String prefix, int ... idlers) {
97                 this.prefix = prefix;
98                 this.idlers = idlers;
99         }
100 }
101
102
103 public class AltosIdleFetch implements AltosDataProvider {
104
105         static final AltosIdler[] idlers = {
106
107                 new AltosIdler("EasyMini-v1",
108                                AltosIdler.idle_ms5607,
109                                AltosIdler.idle_sensor_emini1),
110
111                 new AltosIdler("EasyMini-v2",
112                                AltosIdler.idle_ms5607,
113                                AltosIdler.idle_sensor_emini2),
114
115                 new AltosIdler("TeleMini-v1",
116                                AltosIdler.idle_sensor_tm),
117
118                 new AltosIdler("TeleMini-v2",
119                                AltosIdler.idle_ms5607,
120                                AltosIdler.idle_sensor_tmini2),
121
122                 new AltosIdler("TeleMini-v3",
123                                AltosIdler.idle_ms5607,
124                                AltosIdler.idle_sensor_tmini3),
125
126                 new AltosIdler("TeleMetrum-v1",
127                                AltosIdler.idle_gps,
128                                AltosIdler.idle_sensor_tm),
129
130                 new AltosIdler("TeleMetrum-v2",
131                                AltosIdler.idle_gps,
132                                AltosIdler.idle_mma655x,
133                                AltosIdler.idle_ms5607,
134                                AltosIdler.idle_sensor_metrum),
135
136                 new AltosIdler("TeleMega",
137                                AltosIdler.idle_gps,
138                                AltosIdler.idle_mma655x,
139                                AltosIdler.idle_ms5607,
140                                AltosIdler.idle_imu, AltosIdler.idle_mag,
141                                AltosIdler.idle_sensor_mega),
142                 new AltosIdler("EasyMega",
143                                AltosIdler.idle_mma655x,
144                                AltosIdler.idle_ms5607,
145                                AltosIdler.idle_imu, AltosIdler.idle_mag,
146                                AltosIdler.idle_sensor_mega),
147                 new AltosIdler("TeleGPS",
148                                AltosIdler.idle_gps,
149                                AltosIdler.idle_sensor_tgps),
150         };
151
152         AltosLink               link;
153
154         public void provide_data(AltosDataListener listener) throws InterruptedException, AltosUnknownProduct {
155                 try {
156                         boolean matched = false;
157                         /* Fetch config data from remote */
158                         AltosConfigData config_data = new AltosConfigData(link);
159                         listener.set_state(AltosLib.ao_flight_stateless);
160                         for (AltosIdler idler : idlers) {
161                                 if (idler.matches(config_data)) {
162                                         idler.provide_data(listener, link);
163                                         matched = true;
164                                         break;
165                                 }
166                         }
167                         if (!matched)
168                                 throw new AltosUnknownProduct(config_data.product);
169                         listener.set_received_time(System.currentTimeMillis());
170                 } catch (TimeoutException te) {
171                 }
172
173         }
174
175         public AltosIdleFetch(AltosLink link) {
176                 this.link = link;
177         }
178 }