2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_12;
21 import java.util.concurrent.*;
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;
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;
46 public AltosPresTemp pres_temp(int raw_pres, int raw_temp) {
53 if (raw_pres == AltosLib.MISSING || raw_temp == AltosLib.MISSING)
54 return new AltosPresTemp(AltosLib.MISSING, AltosLib.MISSING);
56 dT = raw_temp - ((int) tref << 8);
58 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
61 OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
63 SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
65 OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
67 SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
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;
76 int TEMPP = TEMP + 1500;
77 long TEMPP2 = (long) TEMPP * (long) TEMPP;
78 OFF2 = OFF2 + 15 * TEMPP2;
79 SENS2 = SENS2 + 8 * TEMPP2;
86 P = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
88 return new AltosPresTemp(P, TEMP / 100.0);
91 public AltosPresTemp pres_temp(AltosLink link) throws InterruptedException, TimeoutException {
92 int raw_pres = AltosLib.MISSING;
93 int raw_temp = AltosLib.MISSING;
98 String line = link.get_reply_no_dialog(5000);
100 throw new TimeoutException();
102 String[] items = line.split("\\s+");
103 if (line.startsWith("Pressure:")) {
104 if (items.length >= 2) {
105 raw_pres = Integer.parseInt(items[1]);
107 } else if (line.startsWith("Temperature:")) {
108 if (items.length >= 2)
109 raw_temp = Integer.parseInt(items[1]);
110 } else if (line.startsWith("Altitude:")) {
114 return pres_temp(raw_pres, raw_temp);
117 public AltosMs5607(boolean ms5611) {
118 this.ms5611 = ms5611;
121 public AltosMs5607() {
125 public AltosMs5607(AltosMs5607 old) {
126 reserved = old.reserved;
132 tempsens = old.tempsens;
136 static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
138 AltosCalData cal_data = listener.cal_data();
139 AltosMs5607 ms5607 = cal_data.ms5607;
141 if (ms5607 != null) {
142 AltosPresTemp pt = ms5607.pres_temp(link);
143 listener.set_temperature(pt.temp);
144 listener.set_pressure(pt.pres);
146 } catch (TimeoutException te) {
150 public AltosMs5607(AltosConfigData config_data) {
151 this(config_data.ms5607());
154 public AltosMs5607 (AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {