altoslib: Define 3.8 as a good battery and 3.5 as a good igniter
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabPad.java
1 /*
2  * Copyright © 2013 Mike Beattie <mike@ethernal.org>
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.AltosDroid;
19
20 import org.altusmetrum.altoslib_2.*;
21
22 import android.app.Activity;
23 import android.os.Bundle;
24 import android.support.v4.app.Fragment;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 import android.location.Location;
31
32 public class TabPad extends Fragment implements AltosDroidTab {
33         AltosDroid mAltosDroid;
34
35         private TextView mBatteryVoltageView;
36         private GoNoGoLights mBatteryLights;
37         private TextView mApogeeVoltageView;
38         private GoNoGoLights mApogeeLights;
39         private TextView mMainVoltageView;
40         private GoNoGoLights mMainLights;
41         private TextView mDataLoggingView;
42         private GoNoGoLights mDataLoggingLights;
43         private TextView mGPSLockedView;
44         private GoNoGoLights mGPSLockedLights;
45         private TextView mGPSReadyView;
46         private GoNoGoLights mGPSReadyLights;
47         private TextView mPadLatitudeView;
48         private TextView mPadLongitudeView;
49         private TextView mPadAltitudeView;
50
51         @Override
52         public void onAttach(Activity activity) {
53                 super.onAttach(activity);
54                 mAltosDroid = (AltosDroid) activity;
55                 mAltosDroid.registerTab(this);
56         }
57
58         @Override
59         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
60                 View v = inflater.inflate(R.layout.tab_pad, container, false);
61                 mBatteryVoltageView = (TextView) v.findViewById(R.id.battery_voltage_value);
62                 mBatteryLights = new GoNoGoLights((ImageView) v.findViewById(R.id.battery_redled),
63                                                   (ImageView) v.findViewById(R.id.battery_greenled),
64                                                   getResources());
65
66                 mApogeeVoltageView = (TextView) v.findViewById(R.id.apogee_voltage_value);
67                 mApogeeLights = new GoNoGoLights((ImageView) v.findViewById(R.id.apogee_redled),
68                                                  (ImageView) v.findViewById(R.id.apogee_greenled),
69                                                  getResources());
70
71                 mMainVoltageView = (TextView) v.findViewById(R.id.main_voltage_value);
72                 mMainLights = new GoNoGoLights((ImageView) v.findViewById(R.id.main_redled),
73                                                (ImageView) v.findViewById(R.id.main_greenled),
74                                                getResources());
75
76                 mDataLoggingView = (TextView) v.findViewById(R.id.logging_value);
77                 mDataLoggingLights = new GoNoGoLights((ImageView) v.findViewById(R.id.logging_redled),
78                                                       (ImageView) v.findViewById(R.id.logging_greenled),
79                                                       getResources());
80
81                 mGPSLockedView = (TextView) v.findViewById(R.id.gps_locked_value);
82                 mGPSLockedLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_locked_redled),
83                                                     (ImageView) v.findViewById(R.id.gps_locked_greenled),
84                                                     getResources());
85
86                 mGPSReadyView = (TextView) v.findViewById(R.id.gps_ready_value);
87                 mGPSReadyLights = new GoNoGoLights((ImageView) v.findViewById(R.id.gps_ready_redled),
88                                                    (ImageView) v.findViewById(R.id.gps_ready_greenled),
89                                                    getResources());
90
91                 mPadLatitudeView = (TextView) v.findViewById(R.id.pad_lat_value);
92                 mPadLongitudeView = (TextView) v.findViewById(R.id.pad_lon_value);
93                 mPadAltitudeView = (TextView) v.findViewById(R.id.pad_alt_value);
94         return v;
95         }
96
97         @Override
98         public void onDestroy() {
99                 super.onDestroy();
100                 mAltosDroid.unregisterTab(this);
101                 mAltosDroid = null;
102         }
103
104         public void update_ui(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
105                 if (state != null) {
106                         mBatteryVoltageView.setText(AltosDroid.number("%4.2f V", state.battery_voltage));
107                         mBatteryLights.set(state.battery_voltage > 3.7, state.battery_voltage == AltosLib.MISSING);
108
109                         mApogeeVoltageView.setText(AltosDroid.number("%4.2f V", state.apogee_voltage));
110                         mApogeeLights.set(state.apogee_voltage > 3.2, state.apogee_voltage == AltosLib.MISSING);
111
112                         mMainVoltageView.setText(AltosDroid.number("%4.2f V", state.main_voltage));
113                         mMainLights.set(state.main_voltage > 3.2, state.main_voltage == AltosLib.MISSING);
114
115                         if (state.flight != 0) {
116                                 if (state.state <= AltosLib.ao_flight_pad)
117                                         mDataLoggingView.setText("Ready to record");
118                                 else if (state.state < AltosLib.ao_flight_landed)
119                                         mDataLoggingView.setText("Recording data");
120                                 else
121                                         mDataLoggingView.setText("Recorded data");
122                         } else {
123                                 mDataLoggingView.setText("Storage full");
124                         }
125                         mDataLoggingLights.set(state.flight != 0, state.flight == AltosLib.MISSING);
126
127                         if (state.gps != null) {
128                                 mGPSLockedView.setText(AltosDroid.integer("%4d sats", state.gps.nsat));
129                                 mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4, false);
130                                 if (state.gps_ready)
131                                         mGPSReadyView.setText("Ready");
132                                 else
133                                         mGPSReadyView.setText(AltosDroid.integer("Waiting %d", state.gps_waiting));
134                         } else
135                                 mGPSLockedLights.set(false, true);
136                         mGPSReadyLights.set(state.gps_ready, state.gps == null);
137                 }
138
139                 if (receiver != null) {
140                         double altitude = 0;
141                         if (receiver.hasAltitude())
142                                 altitude = receiver.getAltitude();
143                         mPadLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
144                         mPadLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "W", "E"));
145                         mPadAltitudeView.setText(AltosDroid.number("%4.0f m", altitude));
146                 }
147         }
148
149 }