altoslib: Do data analysis on raw values rather than AltosState
[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 provide_data(AltosDataListener listener, AltosLink link, AltosCalData cal_data) throws InterruptedException {
76                 try {
77                         AltosMag        mag = new AltosMag(link);
78
79                         if (mag != null)
80                                 listener.set_mag(cal_data.mag_along(mag.along),
81                                                  cal_data.mag_across(mag.across),
82                                                  cal_data.mag_through(mag.through));
83                 } catch (TimeoutException te) {
84                 }
85         }
86
87         public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
88                 this();
89                 link.printf("M\n");
90                 for (;;) {
91                         String line = link.get_reply_no_dialog(5000);
92                         if (line == null) {
93                                 throw new TimeoutException();
94                         }
95                         if (parse_string(line))
96                                 break;
97                 }
98         }
99 }