Bump java lib versions in preparation for 1.9.2
[fw/altos] / altoslib / AltosEepromRecordMicroPeak2.java
1 /*
2  * Copyright © 2019 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_14;
16
17 public class AltosEepromRecordMicroPeak2 extends AltosEepromRecord {
18         public static final int record_length = 2;
19
20         private static final int PA_GROUND_OFFSET = 0;
21         private static final int PA_MIN_OFFSET = 4;
22         private static final int N_SAMPLES_OFFSET = 8;
23         private static final int STARTING_LOG_OFFSET = 10;
24
25         private static final int LOG_ID_MICROPEAK = 0;
26         private static final int LOG_ID_MICROKITE = 1;
27         private static final int LOG_ID_MICROPEAK2 = 2;
28
29         private int value16(int o) {
30                 return eeprom.data16(o);
31         }
32
33         private int value32(int o) {
34                 return eeprom.data32(o);
35         }
36
37         public int cmd() {
38                 if (start == 0)
39                         return AltosLib.AO_LOG_FLIGHT;
40                 return AltosLib.AO_LOG_SENSOR;
41         }
42
43         private int pa_ground() {
44                 return value32(PA_GROUND_OFFSET);
45         }
46
47         private int pa_min() {
48                 return value32(PA_MIN_OFFSET);
49         }
50
51         private int log_id() {
52                 return value16(N_SAMPLES_OFFSET) >> 12;
53         }
54
55         private int n_samples() {
56                 return value16(N_SAMPLES_OFFSET) & 0xfff;
57         }
58
59         private int ticks_per_sample() {
60                 int log_id = log_id();
61
62                 if (log_id == LOG_ID_MICROPEAK)
63                         return 2;
64                 if (log_id == LOG_ID_MICROKITE)
65                         return 200;
66                 if (log_id == LOG_ID_MICROPEAK2)
67                         return 10;
68                 return 1;
69         }
70
71         public int tick() {
72                 if (start <= STARTING_LOG_OFFSET)
73                         return 0;
74                 return ((start - STARTING_LOG_OFFSET) / 2) * ticks_per_sample();
75         }
76
77         public double ticks_per_sec() {
78                 int log_id = log_id();
79
80                 if (log_id == LOG_ID_MICROPEAK)
81                         return 1000.0/96.0;
82                 if (log_id == LOG_ID_MICROKITE)
83                         return 1000 / 96.0;
84                 if (log_id == LOG_ID_MICROPEAK2)
85                         return 100.0;
86                 return 100.0;
87         }
88
89         int mix_in (int high, int low) {
90                 return  high - (high & 0xffff) + low;
91         }
92
93         boolean closer (int target, int a, int b) {
94                 return Math.abs (target - a) < Math.abs(target - b);
95         }
96
97         private int pressure() {
98                 int cur = value32(PA_GROUND_OFFSET);
99                 for (int s = STARTING_LOG_OFFSET; s <= start; s += 2) {
100                         int     k = value16(s);
101                         int     same = mix_in(cur, k);
102                         int     up = mix_in(cur + 0x10000, k);
103                         int     down = mix_in(cur - 0x10000, k);
104
105                         if (closer (cur, same, up)) {
106                                 if (closer (cur, same, down))
107                                         cur = same;
108                                 else
109                                         cur = down;
110                         } else {
111                                 if (closer (cur, up, down))
112                                         cur = up;
113                                 else
114                                         cur = down;
115                         }
116                 }
117                 return cur;
118         }
119
120         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
121                 listener.set_tick(tick());
122                 switch (cmd()) {
123                 case AltosLib.AO_LOG_FLIGHT:
124                         int pa_ground = pa_ground();
125                         int pa_min = pa_min();
126                         int n_samples = n_samples();
127                         int log_id = log_id();
128                         listener.set_state(AltosLib.ao_flight_pad);
129                         listener.cal_data().set_ground_pressure(pa_ground);
130                         listener.cal_data().set_ticks_per_sec(ticks_per_sec());
131                         listener.cal_data().set_boost_tick();
132                         listener.set_avoid_duplicate_files();
133                         break;
134                 case AltosLib.AO_LOG_SENSOR:
135                         listener.set_state(AltosLib.ao_flight_boost);
136                         listener.set_pressure(pressure());
137                         break;
138                 }
139         }
140
141         public int next_start() {
142                 if (start == 0)
143                         return STARTING_LOG_OFFSET;
144                 if (start + 2 >= STARTING_LOG_OFFSET + 2 * n_samples())
145                         return -1;
146                 return start + 2;
147         }
148
149         public AltosEepromRecord next() {
150                 int     s = next_start();
151                 if (s < 0)
152                         return null;
153                 return new AltosEepromRecordMicroPeak2(eeprom, s);
154         }
155
156         public AltosEepromRecordMicroPeak2(AltosEeprom eeprom, int start) {
157                 super(eeprom, start, record_length);
158         }
159
160         public AltosEepromRecordMicroPeak2(AltosEeprom eeprom) {
161                 this(eeprom, 0);
162         }
163 }