altoslib: Fix gyro headings in CSV files
[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_13;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosMag implements Cloneable {
25         public int              x;
26         public int              z;
27         public int              y;
28
29         public static final double counts_per_gauss = 1090;
30
31         public static double convert_gauss(double counts) {
32                 return counts / counts_per_gauss;
33         }
34
35         public boolean parse_string(String line) {
36
37                 if (!line.startsWith("X:"))
38                         return false;
39
40                 String[] items = line.split("\\s+");
41
42                 if (items.length >= 6) {
43                         x = Integer.parseInt(items[1]);
44                         z = Integer.parseInt(items[3]);
45                         y = Integer.parseInt(items[5]);
46                 }
47                 return true;
48         }
49
50         public AltosMag clone() {
51                 AltosMag n = new AltosMag();
52
53                 n.x = x;
54                 n.z = z;
55                 n.y = y;
56                 return n;
57         }
58
59         public AltosMag() {
60                 x = AltosLib.MISSING;
61                 z = AltosLib.MISSING;
62                 y = AltosLib.MISSING;
63         }
64
65         static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
66                 try {
67                         AltosMag        mag = new AltosMag(link);
68                         AltosCalData    cal_data = listener.cal_data();
69
70                         if (mag != null)
71                                 listener.set_mag(cal_data.mag_along(mag.y),
72                                                  cal_data.mag_across(mag.x),
73                                                  cal_data.mag_through(mag.z));
74                 } catch (TimeoutException te) {
75                 }
76         }
77
78         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
79                 this();
80                 link.printf("M\n");
81                 for (;;) {
82                         String line = link.get_reply_no_dialog(5000);
83                         if (line == null) {
84                                 throw new TimeoutException();
85                         }
86                         if (parse_string(line))
87                                 break;
88                 }
89         }
90 }