]> git.gag.com Git - fw/altos/blob - telegps/TeleGPSConfig.java
Merge branch 'branch-1.5' into debian
[fw/altos] / telegps / TeleGPSConfig.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 org.altusmetrum.telegps;
19
20 import java.awt.event.*;
21 import javax.swing.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24 import java.text.*;
25 import org.altusmetrum.altoslib_5.*;
26 import org.altusmetrum.altosuilib_3.*;
27
28 public class TeleGPSConfig implements ActionListener {
29
30         class int_ref {
31                 int     value;
32
33                 public int get() {
34                         return value;
35                 }
36                 public void set(int i) {
37                         value = i;
38                 }
39                 public int_ref(int i) {
40                         value = i;
41                 }
42         }
43
44         class string_ref {
45                 String  value;
46
47                 public String get() {
48                         return value;
49                 }
50                 public void set(String i) {
51                         value = i;
52                 }
53                 public string_ref(String i) {
54                         value = i;
55                 }
56         }
57
58         JFrame          owner;
59         AltosDevice     device;
60         AltosSerial     serial_line;
61
62         AltosConfigData data;
63         TeleGPSConfigUI config_ui;
64         boolean         serial_started;
65         boolean         made_visible;
66
67         void start_serial() throws InterruptedException, TimeoutException {
68                 serial_started = true;
69         }
70
71         void stop_serial() throws InterruptedException {
72                 if (!serial_started)
73                         return;
74                 serial_started = false;
75         }
76
77         void update_ui() {
78                 data.set_values(config_ui);
79                 config_ui.set_clean();
80                 if (!made_visible) {
81                         made_visible = true;
82                         config_ui.make_visible();
83                 }
84         }
85
86         int     pyro;
87
88         final static int        serial_mode_read = 0;
89         final static int        serial_mode_save = 1;
90         final static int        serial_mode_reboot = 2;
91
92         class SerialData implements Runnable {
93                 TeleGPSConfig   config;
94                 int             serial_mode;
95
96                 void callback(String in_cmd) {
97                         final String cmd = in_cmd;
98                         Runnable r = new Runnable() {
99                                         public void run() {
100                                                 if (cmd.equals("abort")) {
101                                                         abort();
102                                                 } else if (cmd.equals("all finished")) {
103                                                         if (serial_line != null)
104                                                                 update_ui();
105                                                 }
106                                         }
107                                 };
108                         SwingUtilities.invokeLater(r);
109                 }
110
111                 void get_data() {
112                         data = null;
113                         try {
114                                 start_serial();
115                                 data = new AltosConfigData(config.serial_line);
116                         } catch (InterruptedException ie) {
117                         } catch (TimeoutException te) {
118                                 try {
119                                         stop_serial();
120                                         callback("abort");
121                                 } catch (InterruptedException ie) {
122                                 }
123                         } finally {
124                                 try {
125                                         stop_serial();
126                                 } catch (InterruptedException ie) {
127                                 }
128                         }
129                         callback("all finished");
130                 }
131
132                 void save_data() {
133                         try {
134                                 start_serial();
135                                 data.save(serial_line, false);
136                         } catch (InterruptedException ie) {
137                         } catch (TimeoutException te) {
138                         } finally {
139                                 try {
140                                         stop_serial();
141                                 } catch (InterruptedException ie) {
142                                 }
143                         }
144                 }
145
146                 void reboot() {
147                         try {
148                                 start_serial();
149                                 serial_line.printf("r eboot\n");
150                                 serial_line.flush_output();
151                         } catch (InterruptedException ie) {
152                         } catch (TimeoutException te) {
153                         } finally {
154                                 try {
155                                         stop_serial();
156                                         serial_line.close();
157                                 } catch (InterruptedException ie) {
158                                 }
159                         }
160                 }
161
162                 public void run () {
163                         switch (serial_mode) {
164                         case serial_mode_save:
165                                 save_data();
166                                 /* fall through ... */
167                         case serial_mode_read:
168                                 get_data();
169                                 break;
170                         case serial_mode_reboot:
171                                 reboot();
172                                 break;
173                         }
174                 }
175
176                 public SerialData(TeleGPSConfig in_config, int in_serial_mode) {
177                         config = in_config;
178                         serial_mode = in_serial_mode;
179                 }
180         }
181
182         void run_serial_thread(int serial_mode) {
183                 SerialData      sd = new SerialData(this, serial_mode);
184                 Thread          st = new Thread(sd);
185                 st.start();
186         }
187
188         void init_ui () throws InterruptedException, TimeoutException {
189                 config_ui = new TeleGPSConfigUI(owner);
190                 config_ui.addActionListener(this);
191                 serial_line.set_frame(owner);
192                 set_ui();
193         }
194
195         void abort() {
196                 if (serial_line != null) {
197                         serial_line.close();
198                         serial_line = null;
199                 }
200                 JOptionPane.showMessageDialog(owner,
201                                               String.format("Connection to \"%s\" failed",
202                                                             device.toShortString()),
203                                               "Connection Failed",
204                                               JOptionPane.ERROR_MESSAGE);
205                 config_ui.setVisible(false);
206         }
207
208         void set_ui() throws InterruptedException, TimeoutException {
209                 if (serial_line != null)
210                         run_serial_thread(serial_mode_read);
211                 else
212                         update_ui();
213         }
214
215         double frequency() {
216                 return AltosConvert.radio_to_frequency(data.radio_frequency,
217                                                        data.radio_setting,
218                                                        data.radio_calibration,
219                                                        data.radio_channel);
220         }
221
222         void save_data() {
223
224                 try {
225                         /* bounds check stuff */
226                         if (config_ui.flight_log_max() > data.log_space()/1024) {
227                                 JOptionPane.showMessageDialog(owner,
228                                                               String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
229                                                                             config_ui.flight_log_max(),
230                                                                             data.log_space()/1024),
231                                                               "Maximum Flight Log Too Large",
232                                                               JOptionPane.ERROR_MESSAGE);
233                                 return;
234                         }
235
236                         /* Pull data out of the UI and stuff back into our local data record */
237
238                         data.get_values(config_ui);
239                         run_serial_thread(serial_mode_save);
240                 } catch (AltosConfigDataException ae) {
241                         JOptionPane.showMessageDialog(owner,
242                                                       ae.getMessage(),
243                                                       "Configuration Data Error",
244                                                       JOptionPane.ERROR_MESSAGE);
245                 }
246         }
247
248         public void actionPerformed(ActionEvent e) {
249                 String  cmd = e.getActionCommand();
250                 try {
251                         if (cmd.equals("Save")) {
252                                 save_data();
253                         } else if (cmd.equals("Reset")) {
254                                 set_ui();
255                         } else if (cmd.equals("Reboot")) {
256                                 if (serial_line != null)
257                                         run_serial_thread(serial_mode_reboot);
258                         } else if (cmd.equals("Close")) {
259                                 if (serial_line != null)
260                                         serial_line.close();
261                         }
262                 } catch (InterruptedException ie) {
263                         abort();
264                 } catch (TimeoutException te) {
265                         abort();
266                 }
267         }
268
269         public TeleGPSConfig(JFrame given_owner) {
270                 owner = given_owner;
271
272                 device = AltosDeviceUIDialog.show(owner, AltosLib.product_telegps);
273                 if (device != null) {
274                         try {
275                                 serial_line = new AltosSerial(device);
276                                 try {
277                                         init_ui();
278                                 } catch (InterruptedException ie) {
279                                         abort();
280                                 } catch (TimeoutException te) {
281                                         abort();
282                                 }
283                         } catch (FileNotFoundException ee) {
284                                 JOptionPane.showMessageDialog(owner,
285                                                               ee.getMessage(),
286                                                               "Cannot open target device",
287                                                               JOptionPane.ERROR_MESSAGE);
288                         } catch (AltosSerialInUseException si) {
289                                 JOptionPane.showMessageDialog(owner,
290                                                               String.format("Device \"%s\" already in use",
291                                                                             device.toShortString()),
292                                                               "Device in use",
293                                                               JOptionPane.ERROR_MESSAGE);
294                         }
295                 }
296         }
297 }