altoslib: Add EasyTimer-v2 support
[fw/altos] / altoslib / AltosMag.java
1 /*
2  * Copyright © 2012 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  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosMag implements Cloneable {
25         public int              along = AltosLib.MISSING;
26         public int              across = AltosLib.MISSING;
27         public int              through = AltosLib.MISSING;
28
29         public static final int model_hmc5883 = 0;
30         public static final int model_mmc5983 = 1;
31
32         public int mag_model = AltosLib.MISSING;
33
34         public static final double counts_per_gauss_hmc5883 = 1090;
35         public static final double counts_per_gauss_mmc5983 = 4096;
36
37         public static double counts_per_gauss(int imu_type, int mag_model) {
38                 switch(mag_model) {
39                 case AltosLib.model_hmc5883:
40                         return counts_per_gauss_hmc5883;
41                 case AltosLib.model_mmc5983:
42                         return counts_per_gauss_mmc5983;
43                 }
44                 switch (imu_type) {
45                 case AltosIMU.imu_type_telemega_v1_v2:
46                 case AltosIMU.imu_type_easymega_v1:
47                         return counts_per_gauss_hmc5883;
48                 case AltosIMU.imu_type_easytimer_v2:
49                         return counts_per_gauss_mmc5983;
50                 }
51
52                 return AltosIMU.counts_per_gauss(imu_type, mag_model);
53         }
54
55         public static double convert_gauss(double counts, int imu_type, int mag_model) {
56                 double cpg = counts_per_gauss(imu_type, mag_model);
57
58                 if (cpg == AltosLib.MISSING)
59                         return AltosLib.MISSING;
60
61                 return counts / cpg;
62         }
63
64         public boolean parse_string(String line) {
65
66                 if (line.startsWith("X:")) {
67
68                         String[] items = line.split("\\s+");
69
70                         mag_model = model_hmc5883;
71
72                         if (items.length >= 6) {
73                                 across = Integer.parseInt(items[1]);
74                                 through = Integer.parseInt(items[3]);
75                                 along = Integer.parseInt(items[5]);
76                         }
77                         return true;
78                 }
79                 if (line.startsWith("MMC5983:")) {
80                         String[] items = line.split("\\s+");
81
82                         mag_model = model_mmc5983;
83
84                         if (items.length >= 4) {
85                                 along = Integer.parseInt(items[1]);
86                                 across = Integer.parseInt(items[2]);
87                                 through = Integer.parseInt(items[3]);
88                         }
89                         return true;
90                 }
91
92                 return false;
93         }
94
95         public AltosMag clone() {
96                 AltosMag n = new AltosMag();
97
98                 n.along = along;
99                 n.across = across;
100                 n.through = through;
101                 return n;
102         }
103
104         public AltosMag() {
105         }
106
107         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
108                 try {
109                         AltosMag        mag = new AltosMag(link);
110                         AltosCalData    cal_data = listener.cal_data();
111
112                         if (mag != null) {
113                                 if (mag.mag_model != AltosLib.MISSING)
114                                         cal_data.set_mag_model(mag.mag_model);
115                                 listener.set_mag(cal_data.mag_along(mag.along),
116                                                  cal_data.mag_across(mag.across),
117                                                  cal_data.mag_through(mag.through));
118                         }
119                 } catch (TimeoutException te) {
120                 }
121         }
122
123         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
124                 this();
125                 link.printf("M\n");
126                 for (;;) {
127                         String line = link.get_reply_no_dialog(5000);
128                         if (line == null) {
129                                 throw new TimeoutException();
130                         }
131                         if (parse_string(line))
132                                 break;
133                 }
134         }
135 }