altos/telefireone-v1.0: Track ao_led_init API change
[fw/altos] / src / drivers / ao_ms5607_convert.c
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 #include <ao_ms5607.h>
20
21 void
22 ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value)
23 {
24         int32_t dT;
25         int32_t TEMP;
26         int64_t OFF;
27         int64_t SENS;
28
29         dT = sample->temp - ((int32_t) ao_ms5607_prom.tref << 8);
30         
31         TEMP = 2000 + (((int64_t) dT * ao_ms5607_prom.tempsens) >> 23);
32
33 #if HAS_MS5611
34         OFF = ((int64_t) ao_ms5607_prom.off << 16) + (((int64_t) ao_ms5607_prom.tco * dT) >> 7);
35         SENS = ((int64_t) ao_ms5607_prom.sens << 15) + (((int64_t) ao_ms5607_prom.tcs * dT) >> 8);
36 #else
37         OFF = ((int64_t) ao_ms5607_prom.off << 17) + (((int64_t) ao_ms5607_prom.tco * dT) >> 6);
38         SENS = ((int64_t) ao_ms5607_prom.sens << 16) + (((int64_t) ao_ms5607_prom.tcs * dT) >> 7);
39 #endif
40
41         if (TEMP < 2000) {
42                 int32_t T2 = ((int64_t) dT * (int64_t) dT) >> 31;
43                 int32_t TEMPM = TEMP - 2000;
44                 int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4;
45                 int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM;
46                 if (TEMP < -1500) {
47                         int32_t TEMPP = TEMP + 1500;
48                         /* You'd think this would need a 64-bit int, but
49                          * that would imply a temperature below -327.67°C...
50                          */
51                         int32_t TEMPP2 = TEMPP * TEMPP;
52                         OFF2 = OFF2 + (int64_t) 15 * TEMPP2;
53                         SENS2 = SENS2 + (int64_t) 8 * TEMPP2;
54                 }
55                 TEMP -= T2;
56                 OFF -= OFF2;
57                 SENS -= SENS2;
58         }
59
60         value->pres = ((((int64_t) sample->pres * SENS) >> 21) - OFF) >> 15;
61         value->temp = TEMP;
62 }