Move libaltos to top level
[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;
19
20 public class AltosMs5607 {
21         public int      reserved;
22         public int      sens;
23         public int      off;
24         public int      tcs;
25         public int      tco;
26         public int      tref;
27         public int      tempsens;
28         public int      crc;
29
30         public int      raw_pres;
31         public int      raw_temp;
32         public int      pa;
33         public int      cc;
34
35         static final boolean    ms5611 = false;
36
37         void convert() {
38                 int     dT;
39                 int TEMP;
40                 long OFF;
41                 long SENS;
42                 //int P;
43
44                 dT = raw_temp - ((int) tref << 8);
45         
46                 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
47
48                 if (ms5611) {
49                         OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
50
51                         SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
52                 } else {
53                         OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
54
55                         SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
56                 } 
57
58                 if (TEMP < 2000) {
59                         int     T2 = (int) (((long) dT * (long) dT) >> 31);
60                         int TEMPM = TEMP - 2000;
61                         long OFF2 = ((long) 61 * (long) TEMPM * (long) TEMPM) >> 4;
62                         long SENS2 = (long) 2 * (long) TEMPM * (long) TEMPM;
63                         if (TEMP < 1500) {
64                                 int TEMPP = TEMP + 1500;
65                                 long TEMPP2 = (long) TEMPP * (long) TEMPP;
66                                 OFF2 = OFF2 + 15 * TEMPP2;
67                                 SENS2 = SENS2 + 8 * TEMPP2;
68                         }
69                         TEMP -= T2;
70                         OFF -= OFF2;
71                         SENS -= SENS2;
72                 }
73
74                 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
75                 cc = TEMP;
76         }
77
78         public int set(int in_pres, int in_temp) {
79                 raw_pres = in_pres;
80                 raw_temp = in_temp;
81                 convert();
82                 return pa;
83         }
84
85         public AltosMs5607() {
86                 raw_pres = AltosRecord.MISSING;
87                 raw_temp = AltosRecord.MISSING;
88                 pa = AltosRecord.MISSING;
89                 cc = AltosRecord.MISSING;
90         }
91 }