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