Merge branch 'master' into droid-gps
[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_1.*;
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                 mBatteryVoltageView.setText(String.format("%4.2f V", state.battery));
106                 mBatteryLights.set(state.battery > 3.7);
107
108                 mApogeeVoltageView.setText(String.format("%4.2f V", state.drogue_sense));
109                 mApogeeLights.set(state.drogue_sense > 3.2);
110
111                 mMainVoltageView.setText(String.format("%4.2f V", state.main_sense));
112                 mMainLights.set(state.main_sense > 3.2);
113
114                 if (state.data.flight != 0) {
115                         if (state.data.state <= AltosLib.ao_flight_pad)
116                                 mDataLoggingView.setText("Ready to record");
117                         else if (state.data.state < AltosLib.ao_flight_landed)
118                                 mDataLoggingView.setText("Recording data");
119                         else
120                                 mDataLoggingView.setText("Recorded data");
121                 } else {
122                         mDataLoggingView.setText("Storage full");
123                 }
124                 mDataLoggingLights.set(state.data.flight != 0);
125
126                 if (state.gps != null) {
127                         mGPSLockedView.setText(String.format("%4d sats", state.gps.nsat));
128                         mGPSLockedLights.set(state.gps.locked && state.gps.nsat >= 4);
129                         if (state.gps_ready)
130                                 mGPSReadyView.setText("Ready");
131                         else
132                                 mGPSReadyView.setText(String.format("Waiting %d", state.gps_waiting));
133                         mGPSReadyLights.set(state.gps_ready);
134                 }
135
136                 if (receiver != null) {
137                         double altitude = 0;
138                         if (receiver.hasAltitude())
139                                 altitude = receiver.getAltitude();
140                         mPadLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
141                         mPadLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "W", "E"));
142                         mPadAltitudeView.setText(String.format("%4.0f m", altitude));
143                 }
144         }
145
146 }