summaryrefslogtreecommitdiff
path: root/altoslib/AltosEepromRecordTiny.java
blob: 705fbd9e63fcf1511ee5481d729c8b9c1085f9ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package org.altusmetrum.altoslib_11;

public class AltosEepromRecordTiny extends AltosEepromRecord implements AltosDataProvider {
	public static final int	record_length = 2;

	private int value() {
		return eeprom.data16(start);
	}

	public boolean valid(int s) {
		return eeprom.data16(s) != 0xffff;
	}

	public int cmd() {
		if (start == 0)
			return AltosLib.AO_LOG_FLIGHT;
		if ((value() & 0x8000) != 0)
			return AltosLib.AO_LOG_STATE;
		return AltosLib.AO_LOG_SENSOR;
	}

	public int tick() {
		int	tick = 0;
		int	step = 10;
		for (int i = 2; i < start; i += 2)
		{
			int v = eeprom.data16(i);

			if ((v & 0x8000) != 0) {
				if ((v & 0x7fff) >= AltosLib.ao_flight_drogue)
					step = 100;
			} else {
				tick += step;
			}
		}
		return tick;
	}

	public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
		int value = data16(-header_length);

		cal_data.set_tick(tick());
		switch (cmd()) {
		case AltosLib.AO_LOG_FLIGHT:
			listener.set_state(AltosLib.ao_flight_pad);
			cal_data.set_flight(value);
			cal_data.set_boost_tick();
			break;
		case AltosLib.AO_LOG_STATE:
			listener.set_state(value & 0x7fff);
			break;
		case AltosLib.AO_LOG_SENSOR:
			listener.set_pressure(AltosConvert.barometer_to_pressure(value));
			break;
		}
	}

	public AltosEepromRecord next() {
		int	s = next_start();
		if (s < 0)
			return null;
		return new AltosEepromRecordTiny(eeprom, s);
	}

	public AltosEepromRecordTiny(AltosEepromNew eeprom, int start) {
		super(eeprom, start, record_length);
	}

	public AltosEepromRecordTiny(AltosEepromNew eeprom) {
		this(eeprom, 0);
	}
}