+ final JFileChooser deviceChooser = new JFileChooser();
+ final JFileChooser logdirChooser = new JFileChooser();
+ final String logdirName = "TeleMetrum";
+ File logdir = null;
+
+ private void setLogdir() {
+ if (logdir == null)
+ logdir = new File(logdirChooser.getCurrentDirectory(), logdirName);
+ logdirChooser.setCurrentDirectory(logdir);
+ }
+
+ private void makeLogdir() {
+ setLogdir();
+ if (!logdir.exists()) {
+ if (!logdir.mkdirs())
+ JOptionPane.showMessageDialog(AltosUI.this,
+ logdir.getName(),
+ "Cannot create directory",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (!logdir.isDirectory()) {
+ JOptionPane.showMessageDialog(AltosUI.this,
+ logdir.getName(),
+ "Is not a directory",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+
+ private void PickSerialDevice() {
+ java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
+ while (port_list.hasMoreElements()) {
+ CommPortIdentifier identifier = port_list.nextElement();
+ System.out.println("Serial port " + identifier.getName());
+ }
+ }
+
+ private void ConnectToDevice() {
+ PickSerialDevice();
+ int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
+
+ if (returnVal == JFileChooser.APPROVE_OPTION) {
+ File file = deviceChooser.getSelectedFile();
+ try {
+ serialLine.open(file);
+ } catch (FileNotFoundException ee) {
+ JOptionPane.showMessageDialog(AltosUI.this,
+ file.getName(),
+ "Cannot open serial port",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ }
+
+ String readline(FileInputStream s) throws IOException {
+ int c;
+ String line = "";
+
+ while ((c = s.read()) != -1) {
+ if (c == '\r')
+ continue;
+ if (c == '\n')
+ return line;
+ line = line + (char) c;
+ }
+ return null;
+ }
+
+ /*
+ * Open an existing telemetry file and replay it in realtime
+ */
+
+ private void Replay() {
+ setLogdir();
+ logdirChooser.setDialogTitle("Select Telemetry File");
+ logdirChooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
+ int returnVal = logdirChooser.showOpenDialog(AltosUI.this);
+
+ if (returnVal == JFileChooser.APPROVE_OPTION) {
+ File file = logdirChooser.getSelectedFile();
+ if (file == null)
+ System.out.println("No file selected?");
+ String filename = file.getName();
+ try {
+ FileInputStream replay = new FileInputStream(file);
+ String line;
+
+ try {
+ while ((line = readline(replay)) != null) {
+ try {
+ AltosTelemetry t = new AltosTelemetry(line);
+ System.out.println ("Version " + t.version + t.callsign);
+ } catch (ParseException pp) {
+ JOptionPane.showMessageDialog(AltosUI.this,
+ line,
+ "error parsing",
+ JOptionPane.ERROR_MESSAGE);
+ break;
+ }
+ }
+ } catch (IOException ee) {
+ JOptionPane.showMessageDialog(AltosUI.this,
+ filename,
+ "error reading",
+ JOptionPane.ERROR_MESSAGE);
+ } finally {
+ try {
+ replay.close();
+ } catch (IOException e) {}
+ }
+ } catch (FileNotFoundException ee) {
+ JOptionPane.showMessageDialog(AltosUI.this,
+ filename,
+ "Cannot open serial port",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ }
+
+ private void SaveFlightData() {
+ }
+