altos: Need to use 16-bit counts for ao_xmem functions
[fw/altos] / altosui / AltosEepromMonitor.java
1 /*
2  * Copyright © 2010 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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosEepromMonitor extends AltosDialog {
33
34         Container       pane;
35         Box             box;
36         JLabel          serial_label;
37         JLabel          flight_label;
38         JLabel          file_label;
39         JLabel          serial_value;
40         JLabel          flight_value;
41         JLabel          file_value;
42         JButton         cancel;
43         JProgressBar    pbar;
44         int             min_state, max_state;
45
46         public AltosEepromMonitor(JFrame owner, int in_min_state, int in_max_state) {
47                 super (owner, "Download Flight Data", false);
48
49                 GridBagConstraints c;
50                 Insets il = new Insets(4,4,4,4);
51                 Insets ir = new Insets(4,4,4,4);
52
53                 pane = getContentPane();
54                 pane.setLayout(new GridBagLayout());
55
56                 c = new GridBagConstraints();
57                 c.gridx = 0; c.gridy = 0;
58                 c.fill = GridBagConstraints.NONE;
59                 c.anchor = GridBagConstraints.LINE_START;
60                 c.insets = il;
61                 serial_label = new JLabel("Serial:");
62                 pane.add(serial_label, c);
63
64                 c = new GridBagConstraints();
65                 c.gridx = 1; c.gridy = 0;
66                 c.fill = GridBagConstraints.HORIZONTAL;
67                 c.weightx = 1;
68                 c.anchor = GridBagConstraints.LINE_START;
69                 c.insets = ir;
70                 serial_value = new JLabel("");
71                 pane.add(serial_value, c);
72
73                 c = new GridBagConstraints();
74                 c.fill = GridBagConstraints.NONE;
75                 c.gridx = 0; c.gridy = 1;
76                 c.anchor = GridBagConstraints.LINE_START;
77                 c.insets = il;
78                 flight_label = new JLabel("Flight:");
79                 pane.add(flight_label, c);
80
81                 c = new GridBagConstraints();
82                 c.fill = GridBagConstraints.HORIZONTAL;
83                 c.weightx = 1;
84                 c.gridx = 1; c.gridy = 1;
85                 c.anchor = GridBagConstraints.LINE_START;
86                 c.insets = ir;
87                 flight_value = new JLabel("");
88                 pane.add(flight_value, c);
89
90                 c = new GridBagConstraints();
91                 c.fill = GridBagConstraints.NONE;
92                 c.gridx = 0; c.gridy = 2;
93                 c.anchor = GridBagConstraints.LINE_START;
94                 c.insets = il;
95                 file_label = new JLabel("File:");
96                 pane.add(file_label, c);
97
98                 c = new GridBagConstraints();
99                 c.fill = GridBagConstraints.HORIZONTAL;
100                 c.weightx = 1;
101                 c.gridx = 1; c.gridy = 2;
102                 c.anchor = GridBagConstraints.LINE_START;
103                 c.insets = ir;
104                 file_value = new JLabel("");
105                 pane.add(file_value, c);
106
107                 min_state = in_min_state;
108                 max_state = in_max_state;
109                 pbar = new JProgressBar();
110                 pbar.setMinimum(0);
111                 pbar.setMaximum((max_state - min_state) * 100);
112                 pbar.setValue(0);
113                 pbar.setString("startup");
114                 pbar.setStringPainted(true);
115                 pbar.setPreferredSize(new Dimension(600, 20));
116                 c = new GridBagConstraints();
117                 c.fill = GridBagConstraints.HORIZONTAL;
118                 c.anchor = GridBagConstraints.CENTER;
119                 c.gridx = 0; c.gridy = 3;
120                 c.gridwidth = GridBagConstraints.REMAINDER;
121                 Insets ib = new Insets(4,4,4,4);
122                 c.insets = ib;
123                 pane.add(pbar, c);
124
125
126                 cancel = new JButton("Cancel");
127                 c = new GridBagConstraints();
128                 c.fill = GridBagConstraints.NONE;
129                 c.anchor = GridBagConstraints.CENTER;
130                 c.gridx = 0; c.gridy = 4;
131                 c.gridwidth = GridBagConstraints.REMAINDER;
132                 Insets ic = new Insets(4,4,4,4);
133                 c.insets = ic;
134                 pane.add(cancel, c);
135
136                 pack();
137                 setLocationRelativeTo(owner);
138                 setVisible(true);
139         }
140
141         public void addActionListener (ActionListener l) {
142                 cancel.addActionListener(l);
143         }
144
145         private void set_value_internal(String state_name, int in_state, int in_block) {
146                 int block = in_block;
147                 int state = in_state;
148
149                 if (block > 100)
150                         block = 100;
151                 if (state < min_state) state = min_state;
152                 if (state >= max_state) state = max_state - 1;
153                 state -= min_state;
154
155                 int pos = state * 100 + block;
156
157                 pbar.setString(state_name);
158                 pbar.setValue(pos);
159         }
160
161         public void set_value(String in_state_name, int in_state, int in_block) {
162                 final String state_name = in_state_name;
163                 final int state = in_state;
164                 final int block = in_block;
165                 Runnable r = new Runnable() {
166                                 public void run() {
167                                         try {
168                                                 set_value_internal(state_name, state, block);
169                                         } catch (Exception ex) {
170                                         }
171                                 }
172                         };
173                 SwingUtilities.invokeLater(r);
174         }
175
176         private void set_serial_internal(int serial) {
177                 serial_value.setText(String.format("%d", serial));
178         }
179
180         public void set_serial(int in_serial) {
181                 final int serial = in_serial;
182                 Runnable r = new Runnable() {
183                                 public void run() {
184                                         try {
185                                                 set_serial_internal(serial);
186                                         } catch (Exception ex) {
187                                         }
188                                 }
189                         };
190                 SwingUtilities.invokeLater(r);
191         }
192
193         private void set_flight_internal(int flight) {
194                 flight_value.setText(String.format("%d", flight));
195         }
196
197         public void set_flight(int in_flight) {
198                 final int flight = in_flight;
199                 Runnable r = new Runnable() {
200                                 public void run() {
201                                         try {
202                                                 set_flight_internal(flight);
203                                         } catch (Exception ex) {
204                                         }
205                                 }
206                         };
207                 SwingUtilities.invokeLater(r);
208         }
209
210         private void set_file_internal(String file) {
211                 file_value.setText(String.format("%s", file));
212         }
213
214         public void set_file(String in_file) {
215                 final String file = in_file;
216                 Runnable r = new Runnable() {
217                                 public void run() {
218                                         try {
219                                                 set_file_internal(file);
220                                         } catch (Exception ex) {
221                                         }
222                                 }
223                         };
224                 SwingUtilities.invokeLater(r);
225         }
226
227         private void done_internal() {
228                 setVisible(false);
229                 dispose();
230         }
231
232         public void done() {
233                 Runnable r = new Runnable() {
234                                 public void run() {
235                                         try {
236                                                 done_internal();
237                                         } catch (Exception ex) {
238                                         }
239                                 }
240                         };
241                 SwingUtilities.invokeLater(r);
242         }
243
244         private void reset_internal() {
245                 set_value_internal("startup",min_state,0);
246                 set_flight_internal(0);
247                 set_file_internal("");
248         }
249
250         public void reset() {
251                 Runnable r = new Runnable() {
252                                 public void run() {
253                                         try {
254                                                 reset_internal();
255                                         } catch (Exception ex) {
256                                         }
257                                 }
258                         };
259                 SwingUtilities.invokeLater(r);
260         }
261 }