altoslib: Create new abstraction underneath AltosState for recording values
[fw/altos] / altoslib / AltosEepromRecordSet.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 import java.io.*;
18 import java.util.*;
19
20 public class AltosEepromRecordSet implements Iterable<AltosState>, AltosRecordSet {
21         AltosEepromNew                  eeprom;
22         TreeSet<AltosEepromRecord>      ordered;
23         AltosState                      start_state;
24
25         class RecordIterator implements Iterator<AltosState> {
26                 Iterator<AltosEepromRecord> riterator;
27                 AltosState state;
28                 boolean started;
29
30                 public boolean hasNext() {
31                         return state == null || riterator.hasNext();
32                 }
33
34                 public AltosState next() {
35                         if (state == null)
36                                 state = start_state.clone();
37                         else {
38                                 state = state.clone();
39                                 AltosEepromRecord       r = riterator.next();
40                                 r.update_state(state);
41                         }
42                         return state;
43                 }
44
45                 public RecordIterator() {
46                         riterator = ordered.iterator();
47                         state = null;
48                 }
49         }
50
51         public Iterator<AltosState> iterator() {
52                 return new RecordIterator();
53         }
54
55         public void capture_series(AltosFlightSeries series) {
56                 series.set_config_data(eeprom.config_data());
57                 for (AltosEepromRecord record : ordered) {
58                         record.update_state(series);
59                 }
60         }
61
62         public AltosEepromRecordSet(AltosEepromNew eeprom) {
63                 this.eeprom = eeprom;
64
65                 AltosConfigData         config_data = eeprom.config_data();
66
67                 AltosEepromRecord       record = null;
68
69                 switch (config_data.log_format) {
70                 case AltosLib.AO_LOG_FORMAT_FULL:
71                         record = new AltosEepromRecordFull(eeprom);
72                         break;
73                 case AltosLib.AO_LOG_FORMAT_TINY:
74                         record = new AltosEepromRecordTiny(eeprom);
75                         break;
76                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
77                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
78                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
79                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
80                         record = new AltosEepromRecordMega(eeprom);
81                         break;
82                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
83                         record = new AltosEepromRecordMetrum(eeprom);
84                         break;
85                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
86                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
87                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
88                         record = new AltosEepromRecordMini(eeprom);
89                         break;
90                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
91                         record = new AltosEepromRecordGps(eeprom);
92                         break;
93                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
94                         record = new AltosEepromRecordFireTwo(eeprom);
95                         break;
96                 }
97
98                 if (record == null) {
99                         System.out.printf("failed to parse log format %d\n", config_data.log_format);
100                         return;
101                 }
102                 ordered = new TreeSet<AltosEepromRecord>();
103                 int     tick = 0;
104                 boolean first = true;
105
106                 start_state = new AltosState();
107                 start_state.set_config_data(record.eeprom.config_data());
108
109                 for (;;) {
110                         int     t = record.tick();
111
112                         if (first) {
113                                 tick = t;
114                                 first = false;
115                         } else {
116                                 while (t < tick - 32767)
117                                         t += 65536;
118                                 tick = t;
119                         }
120                         record.wide_tick = tick;
121                         ordered.add(record);
122                         if (!record.hasNext())
123                                 break;
124                         record = record.next();
125                 }
126         }
127
128         public AltosEepromRecordSet(Reader input) throws IOException {
129                 this(new AltosEepromNew(input));
130         }
131 }