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_2;
20 import java.util.concurrent.*;
22 public class AltosMs5607 {
37 static final boolean ms5611 = false;
46 dT = raw_temp - ((int) tref << 8);
48 TEMP = (int) (2000 + (((long) dT * (long) tempsens) >> 23));
51 OFF = ((long) off << 16) + (((long) tco * (long) dT) >> 7);
53 SENS = ((long) sens << 15) + (((long) tcs * (long) dT) >> 8);
55 OFF = ((long) off << 17) + (((long) tco * (long) dT) >> 6);
57 SENS = ((long) sens << 16) + (((long) tcs * (long) dT) >> 7);
61 int T2 = (int) (((long) dT * (long) dT) >> 31);
62 int TEMPM = TEMP - 2000;
63 long OFF2 = ((long) 61 * (long) TEMPM * (long) TEMPM) >> 4;
64 long SENS2 = (long) 2 * (long) TEMPM * (long) TEMPM;
66 int TEMPP = TEMP + 1500;
67 long TEMPP2 = (long) TEMPP * (long) TEMPP;
68 OFF2 = OFF2 + 15 * TEMPP2;
69 SENS2 = SENS2 + 8 * TEMPP2;
76 pa = (int) (((((long) raw_pres * SENS) >> 21) - OFF) >> 15);
80 public int set(int in_pres, int in_temp) {
87 public boolean parse_line(String line) {
88 System.out.printf ("parse %s\n", line);
89 String[] items = line.split("\\s+");
90 if (line.startsWith("Pressure:")) {
91 if (items.length >= 2) {
92 raw_pres = Integer.parseInt(items[1]);
93 System.out.printf ("raw_pres %d\n", raw_pres);
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 System.out.printf ("found sens length %d\n", items.length);
103 if (items.length >= 3) {
104 sens = Integer.parseInt(items[2]);
105 System.out.printf ("sens %d\n", sens);
107 } else if (line.startsWith("ms5607 off:")) {
108 if (items.length >= 3)
109 off = Integer.parseInt(items[2]);
110 } else if (line.startsWith("ms5607 tcs:")) {
111 if (items.length >= 3)
112 tcs = Integer.parseInt(items[2]);
113 } else if (line.startsWith("ms5607 tco:")) {
114 if (items.length >= 3)
115 tco = Integer.parseInt(items[2]);
116 } else if (line.startsWith("ms5607 tref:")) {
117 if (items.length >= 3)
118 tref = Integer.parseInt(items[2]);
119 } else if (line.startsWith("ms5607 tempsens:")) {
120 if (items.length >= 3)
121 tempsens = Integer.parseInt(items[2]);
122 } else if (line.startsWith("ms5607 crc:")) {
123 if (items.length >= 3)
124 crc = Integer.parseInt(items[2]);
125 } else if (line.startsWith("Altitude:")) {
131 static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) throws InterruptedException {
133 AltosMs5607 ms5607 = new AltosMs5607(link);
135 if (ms5607 != null) {
136 state.set_ms5607(ms5607);
139 } catch (TimeoutException te) {
143 public AltosMs5607() {
144 raw_pres = AltosLib.MISSING;
145 raw_temp = AltosLib.MISSING;
146 pa = AltosLib.MISSING;
147 cc = AltosLib.MISSING;
150 public AltosMs5607 (AltosLink link) throws InterruptedException, TimeoutException {
152 link.printf("c s\nB\n");
154 String line = link.get_reply_no_dialog(5000);
156 throw new TimeoutException();
158 if (!parse_line(line)) {
159 System.out.printf ("stop parsing at %s\n", line);
163 System.out.printf ("sens %d off %d tcs %d tco %d tref %d tempsens %d crc %d pres %d temp %d\n",
164 sens, off, tcs, tco, tref, tempsens, crc, raw_pres, raw_temp);
166 System.out.printf ("pa %d cc %d\n", pa, cc);