altoslib: Save eeprom data in new .eeprom format
[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_11;
16
17
18 public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord> {
19
20         AltosEepromNew          eeprom;
21
22         int                     wide_tick;
23
24         final int               start;
25         final int               length;
26
27         public final static int header_length = 4;
28
29         public int cmd() {
30                 return eeprom.data8(start);
31         }
32
33         public int tick() {
34                 return eeprom.data16(start+2);
35         }
36
37         public int data8(int i) {
38                 i += start + header_length;
39                 return eeprom.data8(i);
40         }
41
42         public int data16(int i) {
43                 return ((data8(i) | (data8(i+1) << 8)) << 16) >> 16;
44         }
45
46         public int data24(int i) {
47                 return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16);
48         }
49
50         public int data32(int i) {
51                 return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16) | (data8(i+3) << 24);
52         }
53
54         public boolean valid(int s) {
55                 return AltosConvert.checksum(eeprom.data, s, length) == 0;
56         }
57
58         public boolean valid() {
59                 return valid(start);
60         }
61
62         private int cmdi() {
63                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
64                         return 0;
65                 return 1;
66         }
67
68         public int compareTo(AltosEepromRecord o) {
69                 int     cmd_diff = cmdi() - o.cmdi();
70
71                 if (cmd_diff != 0)
72                         return cmd_diff;
73
74                 int     tick_diff = tick() - o.tick();
75
76                 if (tick_diff != 0)
77                         return tick_diff;
78                 return start - o.start;
79         }
80
81         public void update_state(AltosState state) {
82                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
83                         state.set_boost_tick(tick());
84                 else
85                         state.set_tick(tick());
86         }
87
88         public int next_start() {
89                 int     s = start + length;
90
91                 while (s + length < eeprom.data.size()) {
92                         if (valid(s))
93                                 return s;
94                         s += length;
95                 }
96                 return -1;
97         }
98
99         public boolean hasNext() {
100                 return next_start() >= 0;
101         }
102
103         public abstract AltosEepromRecord next();
104
105         public AltosEepromRecord(AltosEepromNew eeprom, int start, int length) {
106                 this.eeprom = eeprom;
107                 this.start = start;
108                 this.length = length;
109
110                 while (start + length < eeprom.data.size() && !valid())
111                         start += length;
112         }
113 }