java: Bump java library versions for next release
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.util.concurrent.*;
21
22 public class AltosMag implements Cloneable {
23         public double           x;
24         public double           y;
25         public double           z;
26
27         public static double counts_per_gauss = 1090;
28
29         public static double convert_gauss(int counts) {
30                 return (double) counts / counts_per_gauss;
31         }
32
33         public boolean parse_string(String line) {
34 //              if (line.startsWith("Syntax error")) {
35 //                      x = y = z = 0;
36 //                      return true;
37 //              }
38
39                 if (!line.startsWith("X:"))
40                         return false;
41
42                 String[] items = line.split("\\s+");
43
44                 if (items.length >= 6) {
45                         x = convert_gauss(Integer.parseInt(items[1]));
46                         y = convert_gauss(Integer.parseInt(items[3]));
47                         z = convert_gauss(Integer.parseInt(items[5]));
48                 }
49                 return true;
50         }
51
52         public AltosMag clone() {
53                 AltosMag n = new AltosMag();
54
55                 n.x = x;
56                 n.y = y;
57                 n.z = z;
58                 return n;
59         }
60
61         public AltosMag() {
62                 x = AltosLib.MISSING;
63                 y = AltosLib.MISSING;
64                 z = AltosLib.MISSING;
65         }
66
67         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
68                 try {
69                         AltosMag        mag = new AltosMag(link);
70
71                         if (mag != null)
72                                 state.set_mag(mag);
73                 } catch (TimeoutException te) {
74                 }
75         }
76
77         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
78                 this();
79                 link.printf("M\n");
80                 for (;;) {
81                         String line = link.get_reply_no_dialog(5000);
82                         if (line == null) {
83                                 throw new TimeoutException();
84                         }
85                         if (parse_string(line))
86                                 break;
87                 }
88         }
89 }