altoslib: Add new eeprom management code
[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() {
55                 return AltosConvert.checksum(eeprom.data, start, length) == 0;
56         }
57
58         private int cmdi() {
59                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
60                         return 0;
61                 return 1;
62         }
63
64         public int compareTo(AltosEepromRecord o) {
65                 int     cmd_diff = cmdi() - o.cmdi();
66
67                 if (cmd_diff != 0)
68                         return cmd_diff;
69
70                 int     tick_diff = tick() - o.tick();
71
72                 if (tick_diff != 0)
73                         return tick_diff;
74                 return start - o.start;
75         }
76
77         public void update_state(AltosState state) {
78                 if (cmd() == AltosLib.AO_LOG_FLIGHT)
79                         state.set_boost_tick(tick());
80                 else
81                         state.set_tick(tick());
82         }
83
84         public boolean hasNext() {
85                 return start + length * 2 < eeprom.data.size();
86         }
87
88         public abstract AltosEepromRecord next();
89
90         public AltosEepromRecord(AltosEepromNew eeprom, int start, int length) {
91                 this.eeprom = eeprom;
92                 this.start = start;
93                 this.length = length;
94         }
95 }