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