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; version 2 of the License.
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.
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.
18 package org.altusmetrum.altoslib_11;
20 import java.util.concurrent.*;
23 public class AltosMs5607 implements AltosHashable {
38 static final boolean ms5611 = false;
47 dT = raw_temp - ((int) tref << 8);
49 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
52 OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
54 SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
56 OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
58 SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
62 int T2 = (int) (((long) dT * (long) dT) >> 31);
63 int TEMPM = TEMP - 2000;
64 long OFF2 = ((long) 61 * (long) TEMPM * (long) TEMPM) >> 4;
65 long SENS2 = (long) 2 * (long) TEMPM * (long) TEMPM;
67 int TEMPP = TEMP + 1500;
68 long TEMPP2 = (long) TEMPP * (long) TEMPP;
69 OFF2 = OFF2 + 15 * TEMPP2;
70 SENS2 = SENS2 + 8 * TEMPP2;
77 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
81 public int set(int in_pres, int in_temp) {
88 public boolean parse_line(String line) {
89 String[] items = line.split("\\s+");
90 if (line.startsWith("Pressure:")) {
91 if (items.length >= 2) {
92 raw_pres = Integer.parseInt(items[1]);
94 } else if (line.startsWith("Temperature:")) {
95 if (items.length >= 2)
96 raw_temp = Integer.parseInt(items[1]);
97 } else if (line.startsWith("ms5607 reserved:")) {
98 if (items.length >= 3)
99 reserved = Integer.parseInt(items[2]);
100 } else if (line.startsWith("ms5607 sens:")) {
101 if (items.length >= 3) {
102 sens = Integer.parseInt(items[2]);
104 } else if (line.startsWith("ms5607 off:")) {
105 if (items.length >= 3)
106 off = Integer.parseInt(items[2]);
107 } else if (line.startsWith("ms5607 tcs:")) {
108 if (items.length >= 3)
109 tcs = Integer.parseInt(items[2]);
110 } else if (line.startsWith("ms5607 tco:")) {
111 if (items.length >= 3)
112 tco = Integer.parseInt(items[2]);
113 } else if (line.startsWith("ms5607 tref:")) {
114 if (items.length >= 3)
115 tref = Integer.parseInt(items[2]);
116 } else if (line.startsWith("ms5607 tempsens:")) {
117 if (items.length >= 3)
118 tempsens = Integer.parseInt(items[2]);
119 } else if (line.startsWith("ms5607 crc:")) {
120 if (items.length >= 3)
121 crc = Integer.parseInt(items[2]);
122 } else if (line.startsWith("Altitude:")) {
128 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
130 AltosMs5607 ms5607 = new AltosMs5607(link, config_data);
132 if (ms5607 != null) {
133 state.set_ms5607(ms5607);
136 } catch (TimeoutException te) {
140 public AltosMs5607() {
141 raw_pres = AltosLib.MISSING;
142 raw_temp = AltosLib.MISSING;
143 pa = AltosLib.MISSING;
144 cc = AltosLib.MISSING;
147 public AltosMs5607 (AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
149 reserved = config_data.ms5607_reserved;
150 sens = config_data.ms5607_sens;
151 off = config_data.ms5607_off;
152 tcs = config_data.ms5607_tcs;
153 tco = config_data.ms5607_tco;
154 tref = config_data.ms5607_tref;
155 tempsens = config_data.ms5607_tempsens;
156 crc = config_data.ms5607_crc;
159 String line = link.get_reply_no_dialog(5000);
161 throw new TimeoutException();
163 if (!parse_line(line)) {
170 public AltosHashSet hashSet() {
171 AltosHashSet h = new AltosHashSet();
173 h.putInt("reserved", reserved);
174 h.putInt("sens", sens);
175 h.putInt("off", off);
176 h.putInt("tcs", tcs);
177 h.putInt("tco", tco);
178 h.putInt("tref", tref);
179 h.putInt("tempsens", tempsens);
180 h.putInt("crc", crc);
181 h.putInt("raw_pres", raw_pres);
182 h.putInt("raw_temp", raw_temp);
188 public AltosMs5607(AltosHashSet h) {
191 reserved = h.getInt("reserved", reserved);
192 sens = h.getInt("sens", sens);
193 off = h.getInt("off", off);
194 tcs = h.getInt("tcs", tcs);
195 tco = h.getInt("tco", tco);
196 tref = h.getInt("tref", tref);
197 tempsens = h.getInt("tempsens", tempsens);
198 crc = h.getInt("crc", crc);
199 raw_pres = h.getInt("raw_pres", raw_pres);
200 raw_temp = h.getInt("raw_temp", raw_temp);
201 pa = h.getInt("pa", pa);
202 cc = h.getInt("cc", cc);
205 public static AltosMs5607 fromHashSet(AltosHashSet h, AltosMs5607 def) {
209 return new AltosMs5607(h);