altoslib/altosui: Further AltosState transition work
[fw/altos] / altoslib / AltosEepromMega.java
1 /*
2  * Copyright © 2011 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_1;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromMega extends AltosEeprom {
25         public static final int record_length = 32;
26
27         public int record_length() { return record_length; }
28
29         /* AO_LOG_FLIGHT elements */
30         public int flight() { return data16(0); }
31         public int ground_accel() { return data16(2); }
32         public int ground_pres() { return data32(4); }
33         public int ground_temp() { return data32(8); }
34
35         /* AO_LOG_STATE elements */
36         public int state() { return data16(0); }
37         public int reason() { return data16(2); }
38
39         /* AO_LOG_SENSOR elements */
40         public int pres() { return data32(0); }
41         public int temp() { return data32(4); }
42         public int accel_x() { return data16(8); }
43         public int accel_y() { return data16(10); }
44         public int accel_z() { return data16(12); }
45         public int gyro_x() { return data16(14); }
46         public int gyro_y() { return data16(16); }
47         public int gyro_z() { return data16(18); }
48         public int mag_x() { return data16(20); }
49         public int mag_y() { return data16(22); }
50         public int mag_z() { return data16(24); }
51         public int accel() { return data16(26); }
52
53         /* AO_LOG_TEMP_VOLT elements */
54         public int v_batt() { return data16(0); }
55         public int v_pbatt() { return data16(2); }
56         public int nsense() { return data16(4); }
57         public int sense(int i) { return data16(6 + i * 2); }
58         public int pyro() { return data16(26); }
59
60         /* AO_LOG_GPS_TIME elements */
61         public int latitude() { return data32(0); }
62         public int longitude() { return data32(4); }
63         public int altitude() { return data16(8); }
64         public int hour() { return data8(10); }
65         public int minute() { return data8(11); }
66         public int second() { return data8(12); }
67         public int flags() { return data8(13); }
68         public int year() { return data8(14); }
69         public int month() { return data8(15); }
70         public int day() { return data8(16); }
71         
72         /* AO_LOG_GPS_SAT elements */
73         public int nsat() { return data16(0); }
74         public int svid(int n) { return data8(2 + n * 2); }
75         public int c_n(int n) { return data8(2 + n * 2 + 1); }
76
77         public AltosEepromMega (AltosEepromChunk chunk, int start) throws ParseException {
78                 parse_chunk(chunk, start);
79         }
80
81         public void update_state(AltosState state) {
82                 AltosGPS        gps;
83
84                 /* Flush any pending GPS changes */
85                 if (state.gps_pending) {
86                         switch (cmd) {
87                         case AltosLib.AO_LOG_GPS_LAT:
88                         case AltosLib.AO_LOG_GPS_LON:
89                         case AltosLib.AO_LOG_GPS_ALT:
90                         case AltosLib.AO_LOG_GPS_SAT:
91                         case AltosLib.AO_LOG_GPS_DATE:
92                                 break;
93                         default:
94                                 state.set_temp_gps();
95                                 break;
96                         }
97                 }
98
99                 switch (cmd) {
100                 case AltosLib.AO_LOG_FLIGHT:
101                         state.set_boost_tick(tick);
102                         state.set_flight(flight());
103                         state.set_ground_accel(ground_accel());
104                         state.set_ground_pressure(ground_pres());
105                         state.set_temperature(ground_temp() / 100.0);
106                         break;
107                 case AltosLib.AO_LOG_STATE:
108                         state.set_tick(tick);
109                         state.set_state(state());
110                         break;
111                 case AltosLib.AO_LOG_SENSOR:
112                         state.set_tick(tick);
113                         state.set_ms5607(pres(), temp());
114
115                         AltosIMU imu = new AltosIMU();
116                         imu.accel_x = accel_x();
117                         imu.accel_y = accel_y();
118                         imu.accel_z = accel_z();
119
120                         imu.gyro_x = gyro_x();
121                         imu.gyro_y = gyro_y();
122                         imu.gyro_z = gyro_z();
123                         state.imu = imu;
124
125                         AltosMag mag = new AltosMag();
126                         mag.x = mag_x();
127                         mag.y = mag_y();
128                         mag.z = mag_z();
129
130                         state.mag = mag;
131
132                         state.set_accel(accel());
133
134                         break;
135                 case AltosLib.AO_LOG_TEMP_VOLT:
136                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(v_batt()));
137                         state.set_pyro_voltage(AltosConvert.mega_pyro_voltage(v_pbatt()));
138
139                         int nsense = nsense();
140
141                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-2)));
142                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sense(nsense-1)));
143
144                         double voltages[] = new double[nsense-2];
145                         for (int i = 0; i < nsense-2; i++)
146                                 voltages[i] = AltosConvert.mega_pyro_voltage(sense(i));
147
148                         state.set_ignitor_voltage(voltages);
149                         break;
150                 case AltosLib.AO_LOG_GPS_TIME:
151                         state.set_tick(tick);
152                         gps = state.make_temp_gps();
153                         gps.lat = latitude() / 1e7;
154                         gps.lon = longitude() / 1e7;
155                         gps.alt = altitude();
156
157                         gps.hour = hour();
158                         gps.minute = minute();
159                         gps.second = second();
160
161                         int flags = flags();
162
163                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
164                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
165                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
166                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
167
168                         gps.year = year();
169                         gps.month = month();
170                         gps.day = day();
171                         break;
172                 case AltosLib.AO_LOG_GPS_SAT:
173                         state.set_tick(tick);
174                         gps = state.make_temp_gps();
175
176                         int n = nsat();
177                         for (int i = 0; i < n; i++)
178                                 gps.add_sat(svid(i), c_n(i));
179                         break;
180                 }
181         }
182
183         public AltosEepromMega (String line) {
184                 parse_string(line);
185         }
186
187         static public LinkedList<AltosEeprom> read(FileInputStream input) {
188                 LinkedList<AltosEeprom> megas = new LinkedList<AltosEeprom>();
189
190                 for (;;) {
191                         try {
192                                 String line = AltosLib.gets(input);
193                                 if (line == null)
194                                         break;
195                                 try {
196                                         AltosEepromMega mega = new AltosEepromMega(line);
197                                         if (mega.cmd != AltosLib.AO_LOG_INVALID)
198                                                 megas.add(mega);
199                                 } catch (Exception e) {
200                                         System.out.printf ("exception\n");
201                                 }
202                         } catch (IOException ie) {
203                                 break;
204                         }
205                 }
206
207                 return megas;
208         }
209 }