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