Merge remote-tracking branch 'origin/master'
[fw/altos] / altoslib / AltosSensorMega.java
1 /*
2  * Copyright © 2013 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_2;
19
20 import java.util.concurrent.TimeoutException;
21
22 class AltosSensorMega {
23         int             tick;
24         int[]           sense;
25         int             v_batt;
26         int             v_pbatt;
27         int             temp;
28
29         public AltosSensorMega() {
30                 sense = new int[6];
31         }
32
33         public AltosSensorMega(AltosLink link) throws InterruptedException, TimeoutException {
34                 this();
35                 String[] items = link.adc();
36                 for (int i = 0; i < items.length;) {
37                         if (items[i].equals("tick:")) {
38                                 tick = Integer.parseInt(items[i+1]);
39                                 i += 2;
40                                 continue;
41                         }
42                         if (items[i].equals("A:")) {
43                                 sense[0] = Integer.parseInt(items[i+1]);
44                                 i += 2;
45                                 continue;
46                         }
47                         if (items[i].equals("B:")) {
48                                 sense[1] = Integer.parseInt(items[i+1]);
49                                 i += 2;
50                                 continue;
51                         }
52                         if (items[i].equals("C:")) {
53                                 sense[2] = Integer.parseInt(items[i+1]);
54                                 i += 2;
55                                 continue;
56                         }
57                         if (items[i].equals("D:")) {
58                                 sense[3] = Integer.parseInt(items[i+1]);
59                                 i += 2;
60                                 continue;
61                         }
62                         if (items[i].equals("drogue:")) {
63                                 sense[4] = Integer.parseInt(items[i+1]);
64                                 i += 2;
65                                 continue;
66                         }
67                         if (items[i].equals("main:")) {
68                                 sense[5] = Integer.parseInt(items[i+1]);
69                                 i += 2;
70                                 continue;
71                         }
72                         if (items[i].equals("batt:")) {
73                                 v_batt = Integer.parseInt(items[i+1]);
74                                 i += 2;
75                                 continue;
76                         }
77                         if (items[i].equals("pbatt:")) {
78                                 v_pbatt = Integer.parseInt(items[i+1]);
79                                 i += 2;
80                                 continue;
81                         }
82                         if (items[i].equals("temp:")) {
83                                 temp = Integer.parseInt(items[i+1]);
84                                 i += 2;
85                                 continue;
86                         }
87                         i++;
88                 }
89         }
90
91         static public void update_state(AltosState state, AltosLink link, AltosConfigData config_data) {
92                 try {
93                         AltosSensorMega sensor_mega = new AltosSensorMega(link);
94
95                         state.set_battery_voltage(AltosConvert.mega_battery_voltage(sensor_mega.v_batt));
96                         state.set_apogee_voltage(AltosConvert.mega_pyro_voltage(sensor_mega.sense[4]));
97                         state.set_main_voltage(AltosConvert.mega_pyro_voltage(sensor_mega.sense[5]));
98
99                         double[]        ignitor_voltage = new double[4];
100                         for (int i = 0; i < 4; i++)
101                                 ignitor_voltage[i] = AltosConvert.mega_pyro_voltage(sensor_mega.sense[i]);
102                         state.set_ignitor_voltage(ignitor_voltage);
103
104                 } catch (TimeoutException te) {
105                 } catch (InterruptedException ie) {
106                 }
107         }
108 }
109