altos: Use installed pdclib
[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_2;
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                 System.out.printf ("parse %s\n", 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                                 System.out.printf ("raw_pres %d\n", raw_pres);
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                         System.out.printf ("found sens length %d\n", items.length);
103                         if (items.length >= 3) {
104                                 sens = Integer.parseInt(items[2]);
105                                 System.out.printf ("sens %d\n", sens);
106                         }
107                 } else if (line.startsWith("ms5607 off:")) {
108                         if (items.length >= 3)
109                                 off = Integer.parseInt(items[2]);
110                 } else if (line.startsWith("ms5607 tcs:")) {
111                         if (items.length >= 3)
112                                 tcs = Integer.parseInt(items[2]);
113                 } else if (line.startsWith("ms5607 tco:")) {
114                         if (items.length >= 3)
115                                 tco = Integer.parseInt(items[2]);
116                 } else if (line.startsWith("ms5607 tref:")) {
117                         if (items.length >= 3)
118                                 tref = Integer.parseInt(items[2]);
119                 } else if (line.startsWith("ms5607 tempsens:")) {
120                         if (items.length >= 3)
121                                 tempsens = Integer.parseInt(items[2]);
122                 } else if (line.startsWith("ms5607 crc:")) {
123                         if (items.length >= 3)
124                                 crc = Integer.parseInt(items[2]);
125                 } else if (line.startsWith("Altitude:")) {
126                         return false;
127                 }
128                 return true;
129         }
130
131         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) {
132                 try {
133                         AltosMs5607     ms5607 = new AltosMs5607(link);
134
135                         if (ms5607 != null) {
136                                 state.set_ms5607(ms5607);
137                                 return;
138                         }
139                 } catch (TimeoutException te) {
140                 } catch (InterruptedException ie) {
141                 }
142         }
143
144         public AltosMs5607() {
145                 raw_pres = AltosLib.MISSING;
146                 raw_temp = AltosLib.MISSING;
147                 pa = AltosLib.MISSING;
148                 cc = AltosLib.MISSING;
149         }
150
151         public AltosMs5607 (AltosLink link) throws InterruptedException, TimeoutException {
152                 this();
153                 link.printf("c s\nB\n");
154                 for (;;) {
155                         String line = link.get_reply_no_dialog(5000);
156                         if (line == null) {
157                                 throw new TimeoutException();
158                         }
159                         if (!parse_line(line)) {
160                                 System.out.printf ("stop parsing at %s\n", line);
161                                 break;
162                         }
163                 }
164                 System.out.printf ("sens %d off %d tcs %d tco %d tref %d tempsens %d crc %d pres %d temp %d\n",
165                                    sens, off, tcs, tco, tref, tempsens, crc, raw_pres, raw_temp);
166                 convert();
167                 System.out.printf ("pa %d cc %d\n", pa, cc);
168         }
169 }