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