515efda75eb39447b22fa24c85d6f5bc996a2fe6
[fw/altos] / altoslib / AltosEepromIterable.java
1 /*
2  * Copyright © 2013 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_7;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 class AltosEepromOrdered implements Comparable<AltosEepromOrdered> {
25         AltosEeprom     eeprom;
26         int             index;
27         int             tick;
28
29         int cmdi() {
30                 if (eeprom.cmd == AltosLib.AO_LOG_FLIGHT)
31                         return 0;
32                 return 1;
33         }
34
35         public int compareTo(AltosEepromOrdered o) {
36                 int     cmd_diff = cmdi() - o.cmdi();
37
38                 if (cmd_diff != 0)
39                         return cmd_diff;
40
41                 if (eeprom.has_seconds() && o.eeprom.has_seconds()) {
42                         int     seconds_diff = eeprom.seconds() - o.eeprom.seconds();
43
44                         if (seconds_diff != 0)
45                                 return seconds_diff;
46                 }
47
48                 int     tick_diff = tick - o.tick;
49
50                 if (tick_diff != 0)
51                         return tick_diff;
52                 return index - o.index;
53         }
54
55         AltosEepromOrdered (AltosEeprom eeprom, int index, int tick) {
56                 this.eeprom = eeprom;
57                 this.index = index;
58                 this.tick = tick;
59         }
60 }
61
62 class AltosEepromOrderedIterator implements Iterator<AltosEeprom> {
63         TreeSet<AltosEepromOrdered>     olist;
64         Iterator<AltosEepromOrdered>    oiterator;
65
66         public AltosEepromOrderedIterator(Iterable<AltosEeprom> eeproms) {
67                 olist = new TreeSet<AltosEepromOrdered>();
68
69                 int     tick = 0;
70                 int     index = 0;
71                 boolean first = true;
72
73                 for (AltosEeprom e : eeproms) {
74                         int     t = e.tick;
75                         if (first)
76                                 tick = t;
77                         else {
78                                 while (t < tick - 32767)
79                                         t += 65536;
80                                 tick = t;
81                         }
82                         olist.add(new AltosEepromOrdered(e, index++, tick));
83                         first = false;
84                 }
85
86                 oiterator = olist.iterator();
87         }
88
89         public boolean hasNext() {
90                 return oiterator.hasNext();
91         }
92
93         public AltosEeprom next() {
94                 return oiterator.next().eeprom;
95         }
96
97         public void remove () {
98         }
99 }
100
101 public class AltosEepromIterable implements Iterable<AltosEeprom> {
102         public LinkedList<AltosEeprom> eeproms;
103
104         public void write(PrintStream out) {
105                 for (AltosEeprom eeprom : eeproms)
106                         eeprom.write(out);
107         }
108
109         public AltosState state() {
110                 AltosState      state = new AltosState();
111
112                 for (AltosEeprom header : eeproms)
113                         header.update_state(state);
114                 return state;
115         }
116
117         public AltosEepromIterable(LinkedList<AltosEeprom> eeproms) {
118                 this.eeproms = eeproms;
119         }
120
121         public Iterator<AltosEeprom> iterator() {
122                 if (eeproms == null)
123                         eeproms = new LinkedList<AltosEeprom>();
124                 return new AltosEepromOrderedIterator(eeproms);
125         }
126 }