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