altoslib: Add EasyMotor EEPROM support
[fw/altos] / altoslib / AltosEepromRecordMotor.java
1 /*
2  * Copyright © 2020 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 AltosEepromRecordMotor extends AltosEepromRecord {
18         public static final int record_length = 16;
19
20         private int log_format;
21
22         /* AO_LOG_FLIGHT elements */
23         private int flight() { return data16(0); }
24         private int ground_accel() { return data16(2); }
25         private int ground_accel_along() { return data16(4); }
26         private int ground_accel_across() { return data16(6); }
27         private int ground_accel_through() { return data16(8); }
28
29         /* AO_LOG_STATE elements */
30         private int state() { return data16(0); }
31         private int reason() { return data16(2); }
32
33         /* AO_LOG_SENSOR elements */
34         private int motor_pres() { return data16(0); }
35         private int v_batt() { return data16(2); }
36         private int accel() { return data16(4); }
37         private int accel_across() { return data16(6); }
38         private int accel_along() { return data16(8); }
39         private int accel_through() { return data16(10); }
40
41         private int imu_type() {
42                 switch (log_format) {
43                 case AltosLib.AO_LOG_FORMAT_EASYMOTOR:
44                         return AltosIMU.imu_type_easymotor_v2;
45                 default:
46                         return AltosLib.MISSING;
47                 }
48         }
49
50         public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
51                 super.provide_data(listener, cal_data);
52
53                 cal_data.set_imu_type(imu_type());
54
55                 switch (cmd()) {
56                 case AltosLib.AO_LOG_FLIGHT:
57                         cal_data.set_flight(flight());
58                         cal_data.set_ground_accel(ground_accel());
59                         listener.set_accel_ground(cal_data.accel_along(ground_accel_along()),
60                                                   cal_data.accel_across(ground_accel_across()),
61                                                   cal_data.accel_through(ground_accel_through()));
62                         break;
63                 case AltosLib.AO_LOG_STATE:
64                         listener.set_state(state());
65                         break;
66                 case AltosLib.AO_LOG_SENSOR:
67                         AltosConfigData config_data = eeprom.config_data();
68
69                         listener.set_battery_voltage(AltosConvert.easy_mini_2_voltage(v_batt()));
70                         double pa = AltosConvert.easy_motor_2_motor_pressure(motor_pres());
71                         listener.set_motor_pressure(pa);
72
73                         int     accel_along = accel_along();
74                         int     accel_across = accel_across();
75                         int     accel_through = accel_through();
76
77                         listener.set_accel(cal_data.accel_along(accel_along),
78                                            cal_data.accel_across(accel_across),
79                                            cal_data.accel_through(accel_through));
80
81                         listener.set_acceleration(cal_data.acceleration(accel()));
82                         break;
83                 }
84         }
85
86         public AltosEepromRecord next() {
87                 int     s = next_start();
88                 if (s < 0)
89                         return null;
90                 return new AltosEepromRecordMotor(eeprom, s);
91         }
92
93         public AltosEepromRecordMotor(AltosEeprom eeprom, int start) {
94                 super(eeprom, start, record_length);
95                 log_format = eeprom.config_data().log_format;
96         }
97
98         public AltosEepromRecordMotor(AltosEeprom eeprom) {
99                 this(eeprom, 0);
100         }
101 }