]> git.gag.com Git - fw/altos/blob - altosui/AltosEepromDelete.java
altos: Make STM clock configuration per-product. Fix 32MHz CPU speed
[fw/altos] / altosui / AltosEepromDelete.java
1 /*
2  * Copyright © 2011 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.*;
30
31 import libaltosJNI.*;
32
33 public class AltosEepromDelete implements Runnable {
34         AltosEepromList         flights;
35         Thread                  eeprom_thread;
36         AltosSerial             serial_line;
37         boolean                 remote;
38         JFrame                  frame;
39         ActionListener          listener;
40         boolean                 success;
41
42         private void DeleteLog (AltosEepromLog log)
43                 throws IOException, InterruptedException, TimeoutException {
44
45                 if (flights.config_data.flight_log_max != 0 || flights.config_data.log_format != 0) {
46
47                         /* Devices with newer firmware can erase the
48                          * flash blocks containing each flight
49                          */
50                         serial_line.flush_input();
51                         serial_line.printf("d %d\n", log.flight);
52                         for (;;) {
53                                 /* It can take a while to erase the flash... */
54                                 String line = serial_line.get_reply(20000);
55                                 if (line == null)
56                                         throw new TimeoutException();
57                                 if (line.equals("Erased"))
58                                         break;
59                                 if (line.startsWith("No such"))
60                                         throw new IOException(line);
61                         }
62                 }
63         }
64
65         private void show_error_internal(String message, String title) {
66                 JOptionPane.showMessageDialog(frame,
67                                               message,
68                                               title,
69                                               JOptionPane.ERROR_MESSAGE);
70         }
71
72         private void show_error(String in_message, String in_title) {
73                 final String message = in_message;
74                 final String title = in_title;
75                 Runnable r = new Runnable() {
76                                 public void run() {
77                                         try {
78                                                 show_error_internal(message, title);
79                                         } catch (Exception ex) {
80                                         }
81                                 }
82                         };
83                 SwingUtilities.invokeLater(r);
84         }
85
86         public void run () {
87                 success = false;
88                 try {
89                         if (remote)
90                                 serial_line.start_remote();
91
92                         for (AltosEepromLog log : flights) {
93                                 if (log.selected) {
94                                         DeleteLog(log);
95                                 }
96                         }
97                         success = true;
98                 } catch (IOException ee) {
99                         show_error (ee.getLocalizedMessage(),
100                                     serial_line.device.toShortString());
101                 } catch (InterruptedException ie) {
102                 } catch (TimeoutException te) {
103                         show_error (String.format("Connection to \"%s\" failed",
104                                                   serial_line.device.toShortString()),
105                                     "Connection Failed");
106                 } finally {
107                         try {
108                                 if (remote)
109                                         serial_line.stop_remote();
110                         } catch (InterruptedException ie) {
111                         } finally {
112                                 serial_line.flush_output();
113                                 serial_line.close();
114                         }
115                 }
116                 if (listener != null) {
117                         Runnable r = new Runnable() {
118                                         public void run() {
119                                                 try {
120                                                         listener.actionPerformed(new ActionEvent(this,
121                                                                                                  success ? 1 : 0,
122                                                                                                  "delete"));
123                                                 } catch (Exception ex) {
124                                                 }
125                                         }
126                                 };
127                         SwingUtilities.invokeLater(r);
128                 }
129         }
130
131         public void start() {
132                 eeprom_thread = new Thread(this);
133                 eeprom_thread.start();
134         }
135
136         public void addActionListener(ActionListener l) {
137                 listener = l;
138         }
139
140         public AltosEepromDelete(JFrame given_frame,
141                                  AltosSerial given_serial_line,
142                                  boolean given_remote,
143                                  AltosEepromList given_flights) {
144                 frame = given_frame;
145                 serial_line = given_serial_line;
146                 remote = given_remote;
147                 flights = given_flights;
148                 success = false;
149         }
150 }