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_11;
21 import java.util.concurrent.*;
24 public class AltosMs5607 {
39 static final boolean ms5611 = false;
48 dT = raw_temp - ((int) tref << 8);
50 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
53 OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
55 SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
57 OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
59 SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
63 int T2 = (int) (((long) dT * (long) dT) >> 31);
64 int TEMPM = TEMP - 2000;
65 long OFF2 = ((long) 61 * (long) TEMPM * (long) TEMPM) >> 4;
66 long SENS2 = (long) 2 * (long) TEMPM * (long) TEMPM;
68 int TEMPP = TEMP + 1500;
69 long TEMPP2 = (long) TEMPP * (long) TEMPP;
70 OFF2 = OFF2 + 15 * TEMPP2;
71 SENS2 = SENS2 + 8 * TEMPP2;
78 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
82 public int set(int in_pres, int in_temp) {
89 public boolean parse_line(String line) {
90 String[] items = line.split("\\s+");
91 if (line.startsWith("Pressure:")) {
92 if (items.length >= 2) {
93 raw_pres = Integer.parseInt(items[1]);
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 if (items.length >= 3) {
103 sens = Integer.parseInt(items[2]);
105 } else if (line.startsWith("ms5607 off:")) {
106 if (items.length >= 3)
107 off = Integer.parseInt(items[2]);
108 } else if (line.startsWith("ms5607 tcs:")) {
109 if (items.length >= 3)
110 tcs = Integer.parseInt(items[2]);
111 } else if (line.startsWith("ms5607 tco:")) {
112 if (items.length >= 3)
113 tco = Integer.parseInt(items[2]);
114 } else if (line.startsWith("ms5607 tref:")) {
115 if (items.length >= 3)
116 tref = Integer.parseInt(items[2]);
117 } else if (line.startsWith("ms5607 tempsens:")) {
118 if (items.length >= 3)
119 tempsens = Integer.parseInt(items[2]);
120 } else if (line.startsWith("ms5607 crc:")) {
121 if (items.length >= 3)
122 crc = Integer.parseInt(items[2]);
123 } else if (line.startsWith("Altitude:")) {
129 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
131 AltosMs5607 ms5607 = new AltosMs5607(link, config_data);
133 if (ms5607 != null) {
134 state.set_ms5607(ms5607);
137 } catch (TimeoutException te) {
141 public boolean valid_config() {
142 return reserved != AltosLib.MISSING &&
143 sens != AltosLib.MISSING &&
144 off != AltosLib.MISSING &&
145 tcs != AltosLib.MISSING &&
146 tco != AltosLib.MISSING &&
147 tref != AltosLib.MISSING &&
148 tempsens != AltosLib.MISSING &&
149 crc != AltosLib.MISSING;
152 public AltosMs5607() {
153 raw_pres = AltosLib.MISSING;
154 raw_temp = AltosLib.MISSING;
155 pa = AltosLib.MISSING;
156 cc = AltosLib.MISSING;
159 public AltosMs5607(AltosConfigData config_data) {
161 reserved = config_data.ms5607_reserved;
162 sens = config_data.ms5607_sens;
163 off = config_data.ms5607_off;
164 tcs = config_data.ms5607_tcs;
165 tco = config_data.ms5607_tco;
166 tref = config_data.ms5607_tref;
167 tempsens = config_data.ms5607_tempsens;
168 crc = config_data.ms5607_crc;
171 public AltosMs5607 (AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
175 String line = link.get_reply_no_dialog(5000);
177 throw new TimeoutException();
179 if (!parse_line(line)) {