1fa8877b9c639b4d1b5bdd714be5687424dd70be
[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 int              along;
24         public int              across;
25         public int              through;
26
27         public static double counts_per_gauss = 1090;
28
29         public static double convert_gauss(double counts) {
30                 return counts / counts_per_gauss;
31         }
32
33         public boolean parse_string(String line) {
34 //              if (line.startsWith("Syntax error")) {
35 //                      along = across = through = 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                         along = Integer.parseInt(items[1]);
46                         across = Integer.parseInt(items[3]);
47                         through = Integer.parseInt(items[5]);
48                 }
49                 return true;
50         }
51
52         public AltosMag clone() {
53                 AltosMag n = new AltosMag();
54
55                 n.along = along;
56                 n.across = across;
57                 n.through = through;
58                 return n;
59         }
60
61         public AltosMag() {
62                 along = AltosLib.MISSING;
63                 across = AltosLib.MISSING;
64                 through = AltosLib.MISSING;
65         }
66
67         public AltosMag(int along, int across, int through) {
68                 this.along = along;
69                 this.across = across;
70                 this.through = through;
71         }
72
73         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
74                 try {
75                         AltosMag        mag = new AltosMag(link);
76
77                         if (mag != null)
78                                 state.set_mag(mag);
79                 } catch (TimeoutException te) {
80                 }
81         }
82
83         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
84                 this();
85                 link.printf("M\n");
86                 for (;;) {
87                         String line = link.get_reply_no_dialog(5000);
88                         if (line == null) {
89                                 throw new TimeoutException();
90                         }
91                         if (parse_string(line))
92                                 break;
93                 }
94         }
95 }