e89ec0debf5742abff50960451dd0006b02b3493
[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_11;
20
21 import java.util.concurrent.*;
22 import java.io.*;
23
24 public class AltosMag implements Cloneable {
25         public int              along;
26         public int              across;
27         public int              through;
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 //              if (line.startsWith("Syntax error")) {
37 //                      along = across = through = 0;
38 //                      return true;
39 //              }
40
41                 if (!line.startsWith("X:"))
42                         return false;
43
44                 String[] items = line.split("\\s+");
45
46                 if (items.length >= 6) {
47                         along = Integer.parseInt(items[1]);
48                         across = Integer.parseInt(items[3]);
49                         through = Integer.parseInt(items[5]);
50                 }
51                 return true;
52         }
53
54         public AltosMag clone() {
55                 AltosMag n = new AltosMag();
56
57                 n.along = along;
58                 n.across = across;
59                 n.through = through;
60                 return n;
61         }
62
63         public AltosMag() {
64                 along = AltosLib.MISSING;
65                 across = AltosLib.MISSING;
66                 through = AltosLib.MISSING;
67         }
68
69         public AltosMag(int along, int across, int through) {
70                 this.along = along;
71                 this.across = across;
72                 this.through = through;
73         }
74
75         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
76                 try {
77                         AltosMag        mag = new AltosMag(link);
78
79                         if (mag != null)
80                                 state.set_mag(mag);
81                 } catch (TimeoutException te) {
82                 }
83         }
84
85         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
86                 this();
87                 link.printf("M\n");
88                 for (;;) {
89                         String line = link.get_reply_no_dialog(5000);
90                         if (line == null) {
91                                 throw new TimeoutException();
92                         }
93                         if (parse_string(line))
94                                 break;
95                 }
96         }
97 }