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