altoslib: Add new eeprom management code
[fw/altos] / altoslib / AltosEepromRecordTiny.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 public class AltosEepromRecordTiny extends AltosEepromRecord {
18         public static final int record_length = 2;
19
20         private int value() {
21                 return eeprom.data16(start);
22         }
23
24         public int cmd() {
25                 if (start == 0)
26                         return AltosLib.AO_LOG_FLIGHT;
27                 if ((value() & 0x8000) != 0)
28                         return AltosLib.AO_LOG_STATE;
29                 return AltosLib.AO_LOG_SENSOR;
30         }
31
32         public int tick() {
33                 int     tick = 0;
34                 int     step = 10;
35                 for (int i = 2; i < start; i += 2)
36                 {
37                         int v = eeprom.data16(i);
38
39                         if ((v & 0x8000) != 0) {
40                                 if ((v & 0x7fff) >= AltosLib.ao_flight_drogue)
41                                         step = 100;
42                         } else {
43                                 tick += step;
44                         }
45                 }
46                 return tick;
47         }
48
49         public void update_state(AltosState state) {
50                 int value = data16(-header_length);
51
52                 state.set_tick(tick());
53                 switch (cmd()) {
54                 case AltosLib.AO_LOG_FLIGHT:
55                         state.set_state(AltosLib.ao_flight_pad);
56                         state.set_flight(value);
57                         state.set_boost_tick(0);
58                         break;
59                 case AltosLib.AO_LOG_STATE:
60                         state.set_state(value & 0x7fff);
61                         break;
62                 case AltosLib.AO_LOG_SENSOR:
63                         state.set_pressure(AltosConvert.barometer_to_pressure(value));
64                         break;
65                 }
66         }
67
68         public AltosEepromRecord next() {
69                 if (start + record_length * 2 < eeprom.data.size())
70                         return new AltosEepromRecordTiny(eeprom, start + record_length);
71                 return null;
72         }
73
74         public AltosEepromRecordTiny(AltosEepromNew eeprom, int start) {
75                 super(eeprom, start, record_length);
76         }
77
78         public AltosEepromRecordTiny(AltosEepromNew eeprom) {
79                 this(eeprom, 0);
80         }
81 }