Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altoslib / AltosEepromRecord.java
1 /*
2  * Copyright © 2017 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
15 package org.altusmetrum.altoslib_12;
16
17 public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord> {
18
19         AltosEeprom             eeprom;
20
21         int                     wide_tick;
22
23         final int               start;
24         final int               length;
25
26         public final static int header_length = 4;
27
28         public int cmd() {
29                 return eeprom.data8(start);
30         }
31
32         public int tick() {
33                 return eeprom.data16(start+2);
34         }
35
36         public int data8(int i) {
37                 i += start + header_length;
38                 return eeprom.data8(i);
39         }
40
41         public int data16(int i) {
42                 return ((data8(i) | (data8(i+1) << 8)) << 16) >> 16;
43         }
44
45         public int data24(int i) {
46                 return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16);
47         }
48
49         public int data32(int i) {
50                 return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16) | (data8(i+3) << 24);
51         }
52
53         public boolean valid(int s) {
54                 return AltosConvert.checksum(eeprom.data, s, length) == 0;
55         }
56
57         public boolean valid() {
58                 return valid(start);
59         }
60
61         private int cmdi() {
62                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
63                         return 0;
64                 return 1;
65         }
66
67         public AltosConfigData config_data() {
68                 return eeprom.config_data();
69         }
70
71         public int compareTo(AltosEepromRecord o) {
72                 int     cmd_diff = cmdi() - o.cmdi();
73
74                 if (cmd_diff != 0)
75                         return cmd_diff;
76
77                 int     tick_diff = wide_tick - o.wide_tick;
78
79                 if (tick_diff != 0)
80                         return tick_diff;
81                 return start - o.start;
82         }
83
84         /* AltosDataProvider */
85         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
86                 cal_data.set_tick(tick());
87                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
88                         cal_data.set_boost_tick();
89                 listener.set_time(cal_data.time());
90
91                 /* Flush any pending GPS changes */
92                 if (!AltosLib.is_gps_cmd(cmd())) {
93                         AltosGPS gps = cal_data.temp_gps();
94                         if (gps != null) {
95                                 listener.set_gps(gps);
96                                 cal_data.reset_temp_gps();
97                         }
98                 }
99         }
100
101         public int next_start() {
102                 int     s = start + length;
103
104                 while (s + length <= eeprom.data.size()) {
105                         if (valid(s))
106                                 return s;
107                         s += length;
108                 }
109                 return -1;
110         }
111
112         public boolean hasNext() {
113                 return next_start() >= 0;
114         }
115
116         public abstract AltosEepromRecord next();
117
118         public AltosEepromRecord(AltosEeprom eeprom, int start, int length) {
119                 this.eeprom = eeprom;
120                 this.start = start;
121                 this.length = length;
122
123                 while (start + length < eeprom.data.size() && !valid())
124                         start += length;
125         }
126 }