Build altosdroid
[fw/altos] / altosui / 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 altosui;
19
20 public class AltosMs5607 {
21         int     reserved;
22         int     sens;
23         int     off;
24         int     tcs;
25         int     tco;
26         int     tref;
27         int     tempsens;
28         int     crc;
29
30         int             raw_pres;
31         int             raw_temp;
32         public int      pa;
33         public int      cc;
34
35         void convert() {
36                 int     dT;
37                 int TEMP;
38                 long OFF;
39                 long SENS;
40                 int P;
41
42                 dT = raw_temp - ((int) tref << 8);
43         
44                 TEMP = (int) (2000 + (((long) dT * tempsens) >> 23));
45
46                 OFF = ((long) off << 17) + (((long) tco * dT) >> 6);
47
48                 SENS = ((long) sens << 16) + (((long) tcs * dT) >> 7);
49
50                 if (TEMP < 2000) {
51                         int     T2 = (int) (((long) dT * (long) dT) >> 31);
52                         int TEMPM = TEMP - 2000;
53                         long OFF2 = (61 * (long) TEMPM * (long) TEMPM) >> 4;
54                         long SENS2 = 2 * (long) TEMPM * (long) TEMPM;
55                         if (TEMP < 1500) {
56                                 int TEMPP = TEMP + 1500;
57                                 long TEMPP2 = TEMPP * TEMPP;
58                                 OFF2 = OFF2 + 15 * TEMPP2;
59                                 SENS2 = SENS2 + 8 * TEMPP2;
60                         }
61                         TEMP -= T2;
62                         OFF -= OFF2;
63                         SENS -= SENS2;
64                 }
65
66                 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
67                 cc = TEMP;
68         }
69
70         public int set(int in_pres, int in_temp) {
71                 raw_pres = in_pres;
72                 raw_temp = in_temp;
73                 convert();
74                 return pa;
75         }
76 }