altoslib: Add Ms5607 constructor that just takes config_data
[fw/altos] / altoslib / AltosMs5607.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 AltosMs5607 {
25         public int      reserved;
26         public int      sens;
27         public int      off;
28         public int      tcs;
29         public int      tco;
30         public int      tref;
31         public int      tempsens;
32         public int      crc;
33
34         public int      raw_pres;
35         public int      raw_temp;
36         public int      pa;
37         public int      cc;
38
39         static final boolean    ms5611 = false;
40
41         void convert() {
42                 int     dT;
43                 int TEMP;
44                 long OFF;
45                 long SENS;
46                 //int P;
47
48                 dT = raw_temp - ((int) tref << 8);
49
50                 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
51
52                 if (ms5611) {
53                         OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
54
55                         SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
56                 } else {
57                         OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
58
59                         SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
60                 }
61
62                 if (TEMP < 2000) {
63                         int     T2 = (int) (((long) dT * (long) dT) >> 31);
64                         int TEMPM = TEMP - 2000;
65                         long OFF2 = ((long) 61 * (long) TEMPM * (long) TEMPM) >> 4;
66                         long SENS2 = (long) 2 * (long) TEMPM * (long) TEMPM;
67                         if (TEMP < -1500) {
68                                 int TEMPP = TEMP + 1500;
69                                 long TEMPP2 = (long) TEMPP * (long) TEMPP;
70                                 OFF2 = OFF2 + 15 * TEMPP2;
71                                 SENS2 = SENS2 + 8 * TEMPP2;
72                         }
73                         TEMP -= T2;
74                         OFF -= OFF2;
75                         SENS -= SENS2;
76                 }
77
78                 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
79                 cc = TEMP;
80         }
81
82         public int set(int in_pres, int in_temp) {
83                 raw_pres = in_pres;
84                 raw_temp = in_temp;
85                 convert();
86                 return pa;
87         }
88
89         public boolean parse_line(String line) {
90                 String[] items = line.split("\\s+");
91                 if (line.startsWith("Pressure:")) {
92                         if (items.length >= 2) {
93                                 raw_pres = Integer.parseInt(items[1]);
94                         }
95                 } else if (line.startsWith("Temperature:")) {
96                         if (items.length >= 2)
97                                 raw_temp = Integer.parseInt(items[1]);
98                 } else if (line.startsWith("ms5607 reserved:")) {
99                         if (items.length >= 3)
100                                 reserved = Integer.parseInt(items[2]);
101                 } else if (line.startsWith("ms5607 sens:")) {
102                         if (items.length >= 3) {
103                                 sens = Integer.parseInt(items[2]);
104                         }
105                 } else if (line.startsWith("ms5607 off:")) {
106                         if (items.length >= 3)
107                                 off = Integer.parseInt(items[2]);
108                 } else if (line.startsWith("ms5607 tcs:")) {
109                         if (items.length >= 3)
110                                 tcs = Integer.parseInt(items[2]);
111                 } else if (line.startsWith("ms5607 tco:")) {
112                         if (items.length >= 3)
113                                 tco = Integer.parseInt(items[2]);
114                 } else if (line.startsWith("ms5607 tref:")) {
115                         if (items.length >= 3)
116                                 tref = Integer.parseInt(items[2]);
117                 } else if (line.startsWith("ms5607 tempsens:")) {
118                         if (items.length >= 3)
119                                 tempsens = Integer.parseInt(items[2]);
120                 } else if (line.startsWith("ms5607 crc:")) {
121                         if (items.length >= 3)
122                                 crc = Integer.parseInt(items[2]);
123                 } else if (line.startsWith("Altitude:")) {
124                         return false;
125                 }
126                 return true;
127         }
128
129         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
130                 try {
131                         AltosMs5607     ms5607 = new AltosMs5607(link, config_data);
132
133                         if (ms5607 != null) {
134                                 state.set_ms5607(ms5607);
135                                 return;
136                         }
137                 } catch (TimeoutException te) {
138                 }
139         }
140
141         public boolean valid_config() {
142                 return reserved != AltosLib.MISSING &&
143                         sens != AltosLib.MISSING &&
144                         off != AltosLib.MISSING &&
145                         tcs != AltosLib.MISSING &&
146                         tco != AltosLib.MISSING &&
147                         tref != AltosLib.MISSING &&
148                         tempsens != AltosLib.MISSING &&
149                         crc  != AltosLib.MISSING;
150         }
151
152         public AltosMs5607() {
153                 raw_pres = AltosLib.MISSING;
154                 raw_temp = AltosLib.MISSING;
155                 pa = AltosLib.MISSING;
156                 cc = AltosLib.MISSING;
157         }
158
159         public AltosMs5607(AltosConfigData config_data) {
160                 this();
161                 reserved = config_data.ms5607_reserved;
162                 sens = config_data.ms5607_sens;
163                 off = config_data.ms5607_off;
164                 tcs = config_data.ms5607_tcs;
165                 tco = config_data.ms5607_tco;
166                 tref = config_data.ms5607_tref;
167                 tempsens = config_data.ms5607_tempsens;
168                 crc = config_data.ms5607_crc;
169         }
170
171         public AltosMs5607 (AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
172                 this(config_data);
173                 link.printf("B\n");
174                 for (;;) {
175                         String line = link.get_reply_no_dialog(5000);
176                         if (line == null) {
177                                 throw new TimeoutException();
178                         }
179                         if (!parse_line(line)) {
180                                 break;
181                         }
182                 }
183                 convert();
184         }
185 }