628e18c31dd8c106d88fe8eac9169bc615afbfe8
[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 implements AltosDataProvider {
18         public static final int record_length = 2;
19
20         private int value() {
21                 return eeprom.data16(start);
22         }
23
24         public boolean valid(int s) {
25                 return eeprom.data16(s) != 0xffff;
26         }
27
28         public int cmd() {
29                 if (start == 0)
30                         return AltosLib.AO_LOG_FLIGHT;
31                 if ((value() & 0x8000) != 0)
32                         return AltosLib.AO_LOG_STATE;
33                 return AltosLib.AO_LOG_SENSOR;
34         }
35
36         public int tick() {
37                 int     tick = 0;
38                 int     step = 10;
39                 for (int i = 2; i < start; i += 2)
40                 {
41                         int v = eeprom.data16(i);
42
43                         if ((v & 0x8000) != 0) {
44                                 if ((v & 0x7fff) >= AltosLib.ao_flight_drogue)
45                                         step = 100;
46                         } else {
47                                 tick += step;
48                         }
49                 }
50                 return tick;
51         }
52
53         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
54                 int value = data16(-header_length);
55
56                 cal_data.set_tick(tick());
57                 listener.set_time(cal_data.time());
58                 switch (cmd()) {
59                 case AltosLib.AO_LOG_FLIGHT:
60                         listener.set_state(AltosLib.ao_flight_pad);
61                         cal_data.set_flight(value);
62                         cal_data.set_boost_tick();
63                         break;
64                 case AltosLib.AO_LOG_STATE:
65                         listener.set_state(value & 0x7fff);
66                         break;
67                 case AltosLib.AO_LOG_SENSOR:
68                         listener.set_pressure(AltosConvert.barometer_to_pressure(value));
69                         break;
70                 }
71         }
72
73         public AltosEepromRecord next() {
74                 int     s = next_start();
75                 if (s < 0)
76                         return null;
77                 return new AltosEepromRecordTiny(eeprom, s);
78         }
79
80         public AltosEepromRecordTiny(AltosEeprom eeprom, int start) {
81                 super(eeprom, start, record_length);
82         }
83
84         public AltosEepromRecordTiny(AltosEeprom eeprom) {
85                 this(eeprom, 0);
86         }
87 }