summaryrefslogtreecommitdiff
path: root/altosui/altoslib/src
diff options
context:
space:
mode:
Diffstat (limited to 'altosui/altoslib/src')
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java44
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java49
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java5
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java126
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java56
-rw-r--r--altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java118
6 files changed, 396 insertions, 2 deletions
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java
new file mode 100644
index 00000000..d2e4f2f7
--- /dev/null
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+import java.lang.*;
+import java.io.File;
+import java.util.*;
+
+public class AltosFile extends File {
+
+ public AltosFile(int year, int month, int day, int serial, int flight, String extension) {
+ super (AltosPreferences.logdir(),
+ String.format("%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
+ year, month, day, serial, flight, extension));
+ }
+
+ public AltosFile(int serial, int flight, String extension) {
+ this(Calendar.getInstance().get(Calendar.YEAR),
+ Calendar.getInstance().get(Calendar.MONTH) + 1,
+ Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
+ serial,
+ flight,
+ extension);
+ }
+
+ public AltosFile(AltosRecord telem) {
+ this(telem.serial, telem.flight, "telem");
+ }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java
new file mode 100644
index 00000000..3fdea469
--- /dev/null
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+import java.lang.*;
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+
+public class AltosFlightReader {
+ public String name;
+
+ public int serial;
+
+ public void init() { }
+
+ public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
+
+ public void close(boolean interrupted) { }
+
+ public void set_frequency(double frequency) throws InterruptedException, TimeoutException { }
+
+ public void save_frequency() { }
+
+ public void set_telemetry(int telemetry) { }
+
+ public void save_telemetry() { }
+
+ public void update(AltosState state) throws InterruptedException { }
+
+ public boolean supports_telemetry(int telemetry) { return false; }
+
+ public File backing_file() { return null; }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java
index 49585975..9b80e916 100644
--- a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java
@@ -24,6 +24,8 @@ import java.util.*;
import java.text.*;
public abstract class AltosLink {
+ public abstract void print(String data);
+ public abstract void close();
public static boolean debug = false;
public static void set_debug(boolean in_debug) { debug = in_debug; }
@@ -43,8 +45,6 @@ public abstract class AltosLink {
set_monitor(false);
}
- public abstract void print(String data);
-
public void printf(String format, Object ... arguments) {
String line = String.format(format, arguments);
if (debug)
@@ -207,6 +207,7 @@ public abstract class AltosLink {
public boolean remote;
public int serial;
+ public String name;
public void start_remote() throws TimeoutException, InterruptedException {
if (debug)
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java
new file mode 100644
index 00000000..08c45ca8
--- /dev/null
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+import java.io.*;
+import java.lang.*;
+import java.util.*;
+import java.text.ParseException;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/*
+ * This creates a thread to capture telemetry data and write it to
+ * a log file
+ */
+class AltosLog implements Runnable {
+
+ LinkedBlockingQueue<AltosLine> input_queue;
+ LinkedBlockingQueue<String> pending_queue;
+ int serial;
+ int flight;
+ FileWriter log_file;
+ Thread log_thread;
+ AltosFile file;
+
+ private void close_log_file() {
+ if (log_file != null) {
+ try {
+ log_file.close();
+ } catch (IOException io) {
+ }
+ log_file = null;
+ }
+ }
+
+ void close() {
+ close_log_file();
+ if (log_thread != null) {
+ log_thread.interrupt();
+ log_thread = null;
+ }
+ }
+
+ File file() {
+ return file;
+ }
+
+ boolean open (AltosRecord telem) throws IOException {
+ AltosFile a = new AltosFile(telem);
+
+ System.out.printf("open %s\n", a.toString());
+ log_file = new FileWriter(a, true);
+ if (log_file != null) {
+ while (!pending_queue.isEmpty()) {
+ try {
+ String s = pending_queue.take();
+ log_file.write(s);
+ log_file.write('\n');
+ } catch (InterruptedException ie) {
+ }
+ }
+ log_file.flush();
+ file = a;
+ }
+ return log_file != null;
+ }
+
+ public void run () {
+ try {
+ AltosRecord previous = null;
+ for (;;) {
+ AltosLine line = input_queue.take();
+ if (line.line == null)
+ continue;
+ try {
+ AltosRecord telem = AltosTelemetry.parse(line.line, previous);
+ if (telem.serial != 0 && telem.flight != 0 &&
+ (telem.serial != serial || telem.flight != flight || log_file == null))
+ {
+ close_log_file();
+ serial = telem.serial;
+ flight = telem.flight;
+ open(telem);
+ }
+ previous = telem;
+ } catch (ParseException pe) {
+ } catch (AltosCRCException ce) {
+ }
+ if (log_file != null) {
+ log_file.write(line.line);
+ log_file.write('\n');
+ log_file.flush();
+ } else
+ pending_queue.put(line.line);
+ }
+ } catch (InterruptedException ie) {
+ } catch (IOException ie) {
+ }
+ close();
+ }
+
+ public AltosLog (AltosLink link) {
+ pending_queue = new LinkedBlockingQueue<String> ();
+ input_queue = new LinkedBlockingQueue<AltosLine> ();
+ link.add_monitor(input_queue);
+ serial = -1;
+ flight = -1;
+ log_file = null;
+ log_thread = new Thread(this);
+ log_thread.start();
+ }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java
new file mode 100644
index 00000000..1585f9eb
--- /dev/null
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/*
+ * Open an existing telemetry file and replay it in realtime
+ */
+
+public class AltosReplayReader extends AltosFlightReader {
+ Iterator<AltosRecord> iterator;
+ File file;
+
+ public AltosRecord read() {
+ if (iterator.hasNext())
+ return iterator.next();
+ return null;
+ }
+
+ public void close (boolean interrupted) {
+ }
+
+ public void update(AltosState state) throws InterruptedException {
+ /* Make it run in realtime after the rocket leaves the pad */
+ if (state.state > AltosLib.ao_flight_pad)
+ Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
+ }
+
+ public File backing_file() { return file; }
+
+ public AltosReplayReader(Iterator<AltosRecord> in_iterator, File in_file) {
+ iterator = in_iterator;
+ file = in_file;
+ name = file.getName();
+ }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java
new file mode 100644
index 00000000..2cc2822a
--- /dev/null
+++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+import java.lang.*;
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+
+public class AltosTelemetryReader extends AltosFlightReader {
+ AltosLink link;
+ AltosLog log;
+ AltosRecord previous;
+ double frequency;
+ int telemetry;
+
+ LinkedBlockingQueue<AltosLine> telem;
+
+ public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
+ AltosLine l = telem.take();
+ if (l.line == null)
+ throw new IOException("IO error");
+ AltosRecord next = AltosTelemetry.parse(l.line, previous);
+ previous = next;
+ return next;
+ }
+
+ public void flush() {
+ telem.clear();
+ }
+
+ public void close(boolean interrupted) {
+ link.remove_monitor(telem);
+ log.close();
+ link.close();
+ }
+
+ public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
+ frequency = in_frequency;
+ link.set_radio_frequency(frequency);
+ }
+
+ public boolean supports_telemetry(int telemetry) {
+
+ try {
+ /* Version 1.0 or later firmware supports all telemetry formats */
+ if (serial.config_data().compare_version("1.0") >= 0)
+ return true;
+
+ /* Version 0.9 firmware only supports 0.9 telemetry */
+ if (serial.config_data().compare_version("0.9") >= 0) {
+ if (telemetry == Altos.ao_telemetry_0_9)
+ return true;
+ else
+ return false;
+ }
+
+ /* Version 0.8 firmware only supports 0.8 telemetry */
+ if (telemetry == Altos.ao_telemetry_0_8)
+ return true;
+ else
+ return false;
+ } catch (InterruptedException ie) {
+ return true;
+ } catch (TimeoutException te) {
+ return true;
+ }
+ }
+
+ public void save_frequency() {
+ AltosPreferences.set_frequency(link.serial, frequency);
+ }
+
+ public void set_telemetry(int in_telemetry) {
+ telemetry = in_telemetry;
+ link.set_telemetry(telemetry);
+ }
+
+ public void save_telemetry() {
+ AltosPreferences.set_telemetry(link.serial, telemetry);
+ }
+
+ public void set_monitor(boolean monitor) {
+ link.set_monitor(monitor);
+ }
+
+ public File backing_file() {
+ return log.file();
+ }
+
+ public AltosTelemetryReader (AltosLink in_link)
+ throws IOException, InterruptedException, TimeoutException {
+ log = new AltosLog(link);
+ name = link.name;
+ previous = null;
+ telem = new LinkedBlockingQueue<AltosLine>();
+ frequency = AltosPreferences.frequency(link.serial);
+ set_frequency(frequency);
+ telemetry = AltosPreferences.telemetry(link.serial);
+ set_telemetry(telemetry);
+ link.add_monitor(telem);
+ }
+}