From bf8e1b6eecb2bae12ffdbd730bd6ec12ccdaf23a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 25 Dec 2012 14:23:29 -0800 Subject: Start building MicroPeak GUI tool Download, save and analyze MicroPeak flight data Signed-off-by: Keith Packard --- micropeak/MicroData.java | 270 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 micropeak/MicroData.java (limited to 'micropeak/MicroData.java') diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java new file mode 100644 index 00000000..783ae40f --- /dev/null +++ b/micropeak/MicroData.java @@ -0,0 +1,270 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +import java.lang.*; +import java.io.*; +import java.util.*; +import org.altusmetrum.AltosLib.*; + +public class MicroData { + public int ground_pressure; + public int min_pressure; + public int[] pressures; + private double time_step; + private double ground_altitude; + private ArrayList bytes; + + + class FileEndedException extends Exception { + } + + class NonHexcharException extends Exception { + } + + class InvalidCrcException extends Exception { + } + + private int getc(InputStream f) throws IOException, FileEndedException { + int c = f.read(); + + if (c == -1) + throw new FileEndedException(); + bytes.add(c); + return c; + } + + private int get_nonwhite(InputStream f) throws IOException, FileEndedException { + int c; + + for (;;) { + c = getc(f); + if (!Character.isWhitespace(c)) + return c; + } + } + + private int get_hexc(InputStream f) throws IOException, FileEndedException, NonHexcharException { + int c = get_nonwhite(f); + + if ('0' <= c && c <= '9') + return c - '0'; + if ('a' <= c && c <= 'f') + return c - 'a' + 10; + if ('A' <= c && c <= 'F') + return c - 'A' + 10; + throw new NonHexcharException(); + } + + private static final int POLY = 0x8408; + + private int log_crc(int crc, int b) { + int i; + + for (i = 0; i < 8; i++) { + if (((crc & 0x0001) ^ (b & 0x0001)) != 0) + crc = (crc >> 1) ^ POLY; + else + crc = crc >> 1; + b >>= 1; + } + return crc & 0xffff; + } + + int file_crc; + + private int get_hex(InputStream f) throws IOException, FileEndedException, NonHexcharException { + int a = get_hexc(f); + int b = get_hexc(f); + + int h = (a << 4) + b; + + file_crc = log_crc(file_crc, h); + return h; + } + + private boolean find_header(InputStream f) throws IOException { + try { + for (;;) { + if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P') + return true; + } + } catch (FileEndedException fe) { + return false; + } + } + + private int get_32(InputStream f) throws IOException, FileEndedException, NonHexcharException { + int v = 0; + for (int i = 0; i < 4; i++) { + v += get_hex(f) << (i * 8); + } + return v; + } + + private int get_16(InputStream f) throws IOException, FileEndedException, NonHexcharException { + int v = 0; + for (int i = 0; i < 2; i++) { + v += get_hex(f) << (i * 8); + } + return v; + } + + private int swap16(int i) { + return ((i << 8) & 0xff00) | ((i >> 8) & 0xff); + } + + public boolean crc_valid; + + int mix_in (int high, int low) { + return high - (high & 0xffff) + low; + } + + boolean closer (int target, int a, int b) { + return Math.abs (target - a) < Math.abs(target - b); + } + + public double altitude(int i) { + return AltosConvert.pressure_to_altitude(pressures[i]); + } + + int fact(int n) { + if (n == 0) + return 1; + return n * fact(n-1); + } + + int choose(int n, int k) { + return fact(n) / (fact(k) * fact(n-k)); + } + + + public double avg_altitude(int center, int dist) { + int start = center - dist; + int stop = center + dist; + + if (start < 0) + start = 0; + if (stop >= pressures.length) + stop = pressures.length - 1; + + double sum = 0; + double div = 0; + + int n = dist * 2; + + for (int i = start; i <= stop; i++) { + int k = i - (center - dist); + int c = choose (n, k); + + sum += c * pressures[i]; + div += c; + } + + double pres = sum / div; + + double alt = AltosConvert.pressure_to_altitude(pres); + return alt; + } + + public double height(int i) { + return altitude(i) - ground_altitude; + } + + static final int speed_avg = 3; + static final int accel_avg = 5; + + private double avg_speed(int center, int dist) { + if (center == 0) + return 0; + + double ai = avg_altitude(center, dist); + double aj = avg_altitude(center - 1, dist); + double s = (ai - aj) / time_step; + + return s; + } + + public double speed(int i) { + return avg_speed(i, speed_avg); + } + + public double acceleration(int i) { + if (i == 0) + return 0; + return (avg_speed(i, accel_avg) - avg_speed(i-1, accel_avg)) / time_step; + } + + public double time(int i) { + return i * time_step; + } + + public void save (OutputStream f) throws IOException { + for (int c : bytes) + f.write(c); + } + + public MicroData (InputStream f) throws IOException { + bytes = new ArrayList(); + if (!find_header(f)) + throw new IOException(); + try { + file_crc = 0xffff; + ground_pressure = get_32(f); + min_pressure = get_32(f); + int nsamples = get_16(f); + pressures = new int[nsamples + 1]; + + ground_altitude = AltosConvert.pressure_to_altitude(ground_pressure); + int cur = ground_pressure; + pressures[0] = cur; + for (int i = 0; i < nsamples; i++) { + int k = get_16(f); + int same = mix_in(cur, k); + int up = mix_in(cur + 0x10000, k); + int down = mix_in(cur - 0x10000, k); + + if (closer (cur, same, up)) { + if (closer (cur, same, down)) + cur = same; + else + cur = down; + } else { + if (closer (cur, up, down)) + cur = up; + else + cur = down; + } + + pressures[i+1] = cur; + } + + int current_crc = swap16(~file_crc & 0xffff); + int crc = get_16(f); + + crc_valid = crc == current_crc; + + time_step = 0.192; + } catch (FileEndedException fe) { + throw new IOException(); + } catch (NonHexcharException ne) { + throw new IOException(); + } + } + +} -- cgit v1.2.3 From 2bd6aca54fc465995d6985c8799cd0d016c9a543 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 31 Dec 2012 14:17:26 -0800 Subject: micropeak: Add flight stats pane Shows graph or stats in alternate panes Signed-off-by: Keith Packard --- altosuilib/AltosUILib.java | 18 ++-- micropeak/Makefile.am | 2 + micropeak/MicroData.java | 100 ++++++++++++++++++++++ micropeak/MicroPeak.java | 15 +++- micropeak/MicroStats.java | 184 +++++++++++++++++++++++++++++++++++++++++ micropeak/MicroStatsTable.java | 138 +++++++++++++++++++++++++++++++ 6 files changed, 445 insertions(+), 12 deletions(-) create mode 100644 micropeak/MicroStats.java create mode 100644 micropeak/MicroStatsTable.java (limited to 'micropeak/MicroData.java') diff --git a/altosuilib/AltosUILib.java b/altosuilib/AltosUILib.java index 717678ba..5d5f9aaa 100644 --- a/altosuilib/AltosUILib.java +++ b/altosuilib/AltosUILib.java @@ -24,17 +24,17 @@ import org.altusmetrum.AltosLib.*; public class AltosUILib extends AltosLib { - static final int tab_elt_pad = 5; + public static final int tab_elt_pad = 5; - static Font label_font; - static Font value_font; - static Font status_font; - static Font table_label_font; - static Font table_value_font; + public static Font label_font; + public static Font value_font; + public static Font status_font; + public static Font table_label_font; + public static Font table_value_font; - final static int font_size_small = 1; - final static int font_size_medium = 2; - final static int font_size_large = 3; + final public static int font_size_small = 1; + final public static int font_size_medium = 2; + final public static int font_size_large = 3; static void set_fonts(int size) { int brief_size; diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index fde981a6..e0de690c 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -13,6 +13,8 @@ micropeak_JAVA= \ MicroFrame.java \ MicroGraph.java \ MicroSerial.java \ + MicroStats.java \ + MicroStatsTable.java \ MicroFileChooser.java \ MicroUSB.java diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 783ae40f..ec9b83d8 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -22,6 +22,87 @@ import java.io.*; import java.util.*; import org.altusmetrum.AltosLib.*; +abstract class MicroIterator implements Iterator { + int i; + MicroData data; + + public boolean hasNext() { + return i < data.pressures.length; + } + + public MicroIterator (MicroData data) { + this.data = data; + i = 0; + } + + public void remove() { + } +} + +class MicroHeightIterator extends MicroIterator { + public Double next() { + return data.height(i++); + } + + public MicroHeightIterator(MicroData data) { + super(data); + } +} + +class MicroHeightIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroHeightIterator(data); + } + + public MicroHeightIterable(MicroData data) { + this.data = data; + } +} + +class MicroSpeedIterator extends MicroIterator { + public Double next() { + return data.speed(i++); + } + public MicroSpeedIterator(MicroData data) { + super(data); + } +} + +class MicroSpeedIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroSpeedIterator(data); + } + + public MicroSpeedIterable(MicroData data) { + this.data = data; + } +} + +class MicroAccelIterator extends MicroIterator { + public Double next() { + return data.acceleration(i++); + } + public MicroAccelIterator(MicroData data) { + super(data); + } +} + +class MicroAccelIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroAccelIterator(data); + } + + public MicroAccelIterable(MicroData data) { + this.data = data; + } +} + public class MicroData { public int ground_pressure; public int min_pressure; @@ -143,6 +224,18 @@ public class MicroData { return AltosConvert.pressure_to_altitude(pressures[i]); } + public Iterable heights() { + return new MicroHeightIterable(this); + } + + public Iterable speeds() { + return new MicroSpeedIterable(this); + } + + public Iterable accels() { + return new MicroAccelIterable(this); + } + int fact(int n) { if (n == 0) return 1; @@ -266,5 +359,12 @@ public class MicroData { throw new IOException(); } } + + public MicroData() { + ground_pressure = 101000; + min_pressure = 101000; + pressures = new int[1]; + pressures[0] = 101000; + } } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index 463238c8..c69f7167 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -30,13 +30,16 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene File filename; MicroGraph graph; + MicroStatsTable stats; MicroData data; - Container pane; + Container container; + JTabbedPane pane; private void RunFile(InputStream input) { try { data = new MicroData(input); graph.setData(data); + stats.setData(data); } catch (IOException ioe) { } try { @@ -90,7 +93,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene AltosUIPreferences.set_component(this); - pane = getContentPane(); + container = getContentPane(); + pane = new JTabbedPane(); setTitle("MicroPeak"); @@ -129,9 +133,14 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene }); graph = new MicroGraph(); - pane.add(graph.panel); + stats = new MicroStatsTable(); + pane.add(graph.panel, "Graph"); + pane.add(stats, "Statistics"); pane.doLayout(); pane.validate(); + container.add(pane); + container.doLayout(); + container.validate(); doLayout(); validate(); Insets i = getInsets(); diff --git a/micropeak/MicroStats.java b/micropeak/MicroStats.java new file mode 100644 index 00000000..6ae8a2b2 --- /dev/null +++ b/micropeak/MicroStats.java @@ -0,0 +1,184 @@ +/* + * Copyright © 2011 Keith Packard + * + * 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.micropeak; + +import java.io.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroStats { + double coast_height; + double coast_time; + + double apogee_height; + double apogee_time; + + double landed_height; + double landed_time; + + double max_speed; + double max_accel; + + MicroData data; + + void find_landing() { + landed_height = 0; + + int t = 0; + for (double height : data.heights()) { + landed_height = height; + t++; + } + landed_time = data.time(t); + + t = 0; + boolean above = false; + for (double height : data.heights()) { + if (height > landed_height + 10) { + above = true; + } else { + if (above && height < landed_height + 2) { + above = false; + landed_time = data.time(t); + } + } + t++; + } + } + + void find_apogee() { + apogee_height = 0; + apogee_time = 0; + + int t = 0; + for (double height : data.heights()) { + if (height > apogee_height) { + apogee_height = height; + apogee_time = data.time(t); + } + t++; + } + } + + void find_coast() { + coast_height = 0; + coast_time = 0; + + int t = 0; + for (double accel : data.accels()) { + if (accel < -9.8) + break; + t++; + } + coast_time = data.time(t); + + int coast_t = t; + t = 0; + for (double height : data.heights()) { + if (t >= coast_t) { + coast_height = height; + break; + } + t++; + } + } + + void find_max_speed() { + max_speed = 0; + int t = 0; + for (double speed : data.speeds()) { + if (data.time(t) > apogee_time) + break; + if (speed > max_speed) + max_speed = speed; + t++; + } + } + + void find_max_accel() { + max_accel = 0; + + int t = 0; + for (double accel : data.accels()) { + if (data.time(t) > apogee_time) + break; + if (accel > max_accel) + max_accel = accel; + t++; + } + } + + double boost_duration() { + return coast_time; + } + + double boost_height() { + return coast_height; + } + + double boost_speed() { + return coast_height / coast_time; + } + + double boost_accel() { + return boost_speed() / boost_duration(); + } + + double coast_duration() { + return apogee_time - coast_time; + } + + double coast_height() { + return apogee_height - coast_height; + } + + double coast_speed() { + return coast_height() / coast_duration(); + } + + double coast_accel() { + return coast_speed() / coast_duration(); + } + + double descent_duration() { + return landed_time - apogee_time; + } + + double descent_height() { + return apogee_height - landed_height; + } + + double descent_speed() { + return descent_height() / descent_duration(); + } + + public MicroStats(MicroData data) { + + this.data = data; + + find_coast(); + find_apogee(); + find_landing(); + find_max_speed(); + find_max_accel(); + } + + public MicroStats() { + this(new MicroData()); + } +} diff --git a/micropeak/MicroStatsTable.java b/micropeak/MicroStatsTable.java new file mode 100644 index 00000000..f373e25d --- /dev/null +++ b/micropeak/MicroStatsTable.java @@ -0,0 +1,138 @@ +/* + * Copyright © 2011 Keith Packard + * + * 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.micropeak; + +import java.awt.*; +import javax.swing.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroStatsTable extends JComponent { + GridBagLayout layout; + + class MicroStat { + JLabel label; + JTextField[] texts; + + public void set_values(String ... values) { + for (int j = 0; j < values.length; j++) { + texts[j].setText(values[j]); + } + } + + public MicroStat(GridBagLayout layout, int y, String label_text, String ... values) { + GridBagConstraints c = new GridBagConstraints(); + c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad); + c.weighty = 1; + + label = new JLabel(label_text); + label.setFont(AltosUILib.label_font); + label.setHorizontalAlignment(SwingConstants.LEFT); + c.gridx = 0; c.gridy = y; + c.anchor = GridBagConstraints.WEST; + c.fill = GridBagConstraints.VERTICAL; + c.weightx = 0; + layout.setConstraints(label, c); + add(label); + + texts = new JTextField[values.length]; + for (int j = 0; j < values.length; j++) { + JTextField value = new JTextField(values[j]); + value.setFont(AltosUILib.value_font); + value.setHorizontalAlignment(SwingConstants.RIGHT); + texts[j] = value; + c.gridx = j+1; c.gridy = y; + c.anchor = GridBagConstraints.EAST; + c.fill = GridBagConstraints.BOTH; + c.weightx = 1; + layout.setConstraints(value, c); + add(value); + } + } + } + + MicroStat max_height, max_speed; + MicroStat max_accel, avg_accel; + MicroStat boost_duration; + MicroStat coast_duration; + MicroStat descent_speed; + MicroStat descent_duration; + MicroStat flight_time; + + public void setStats(MicroStats stats) { + max_height.set_values(String.format("%5.0f m", stats.apogee_height), + String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height))); + max_speed.set_values(String.format("%5.0f m/s", stats.max_speed), + String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)), + String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed))); + max_accel.set_values(String.format("%5.0f m/s²", stats.max_accel), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel))); + avg_accel.set_values(String.format("%5.0f m/s²", stats.boost_accel(), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel())))); + boost_duration.set_values(String.format("%6.1f s", stats.boost_duration())); + coast_duration.set_values(String.format("%6.1f s", stats.coast_duration())); + descent_speed.set_values(String.format("%5.0f m/s", stats.descent_speed()), + String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed()))); + descent_duration.set_values(String.format("%6.1f s", stats.descent_duration())); + flight_time.set_values(String.format("%6.1f s", stats.landed_time)); + } + + public void setData(MicroData data) { + setStats(new MicroStats(data)); + } + + public MicroStatsTable(MicroStats stats) { + layout = new GridBagLayout(); + + setLayout(layout); + int y = 0; + max_height = new MicroStat(layout, y++, "Maximum height", + String.format("%5.0f m", stats.apogee_height), + String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height))); + max_speed = new MicroStat(layout, y++, "Maximum speed", + String.format("%5.0f m/s", stats.max_speed), + String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)), + String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed))); + max_accel = new MicroStat(layout, y++, "Maximum boost acceleration", + String.format("%5.0f m/s²", stats.max_accel), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel))); + avg_accel = new MicroStat(layout, y++, "Average boost acceleration", + String.format("%5.0f m/s²", stats.boost_accel(), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel())))); + boost_duration = new MicroStat(layout, y++, "Boost duration", + String.format("%6.0f s", stats.boost_duration())); + coast_duration = new MicroStat(layout, y++, "Coast duration", + String.format("%6.1f s", stats.coast_duration())); + descent_speed = new MicroStat(layout, y++, "Descent rate", + String.format("%5.0f m/s", stats.descent_speed()), + String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed()))); + descent_duration = new MicroStat(layout, y++, "Descent duration", + String.format("%6.1f s", stats.descent_duration())); + flight_time = new MicroStat(layout, y++, "Flight Time", + String.format("%6.0f s", stats.landed_time)); + } + + public MicroStatsTable() { + this(new MicroStats()); + } + +} \ No newline at end of file -- cgit v1.2.3 From d83587c3c66b730cc54ca153714eee520ee40b2c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 1 Jan 2013 15:30:11 -0800 Subject: micropeak is code complete now. Added save and download functionality. Removed 'new' from file menu. Signed-off-by: Keith Packard --- altoslib/AltosLib.java | 1 + altosuilib/AltosDevice.java | 30 +++++++ altosuilib/AltosDeviceDialog.java | 163 ++++++++++++++++++++++++++++++++++++++ altosuilib/AltosUSBDevice.java | 112 ++++++++++++++++++++++++++ altosuilib/Makefile.am | 3 + libaltos/libaltos.c | 4 + micropeak/Makefile.am | 3 + micropeak/MicroData.java | 15 +++- micropeak/MicroDeviceDialog.java | 50 ++++++++++++ micropeak/MicroDownload.java | 139 ++++++++++++++++++++++++++++++++ micropeak/MicroGraph.java | 5 ++ micropeak/MicroPeak.java | 81 +++++++++++++++---- micropeak/MicroSave.java | 102 ++++++++++++++++++++++++ micropeak/MicroSerial.java | 12 ++- micropeak/MicroUSB.java | 19 +++-- 15 files changed, 708 insertions(+), 31 deletions(-) create mode 100644 altosuilib/AltosDevice.java create mode 100644 altosuilib/AltosDeviceDialog.java create mode 100644 altosuilib/AltosUSBDevice.java create mode 100644 micropeak/MicroDeviceDialog.java create mode 100644 micropeak/MicroDownload.java create mode 100644 micropeak/MicroSave.java (limited to 'micropeak/MicroData.java') diff --git a/altoslib/AltosLib.java b/altoslib/AltosLib.java index 07516aeb..67138450 100644 --- a/altoslib/AltosLib.java +++ b/altoslib/AltosLib.java @@ -97,6 +97,7 @@ public class AltosLib { public final static int product_any = 0x10000; public final static int product_basestation = 0x10000 + 1; public final static int product_altimeter = 0x10000 + 2; + public final static int product_micropeak_serial = 0x10000 + 3; /* Bluetooth "identifier" (bluetooth sucks) */ public final static String bt_product_telebt = "TeleBT"; diff --git a/altosuilib/AltosDevice.java b/altosuilib/AltosDevice.java new file mode 100644 index 00000000..69b025ba --- /dev/null +++ b/altosuilib/AltosDevice.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2011 Keith Packard + * + * 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.altosuilib; + +import libaltosJNI.*; + +public interface AltosDevice { + public abstract String toString(); + public abstract String toShortString(); + public abstract int getSerial(); + public abstract String getPath(); + public abstract boolean matchProduct(int product); + public abstract String getErrorString(); + public SWIGTYPE_p_altos_file open(); +} diff --git a/altosuilib/AltosDeviceDialog.java b/altosuilib/AltosDeviceDialog.java new file mode 100644 index 00000000..82620b8b --- /dev/null +++ b/altosuilib/AltosDeviceDialog.java @@ -0,0 +1,163 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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.altosuilib; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener { + + private AltosDevice value; + private JList list; + private JButton cancel_button; + private JButton select_button; + private JButton manage_bluetooth_button; + private Frame frame; + private int product; + + public AltosDevice getValue() { + return value; + } + + public abstract AltosDevice[] devices(); + + private void update_devices() { + AltosDevice[] devices = devices(); + list.setListData(devices); + select_button.setEnabled(devices.length > 0); + } + + public AltosDeviceDialog (Frame in_frame, Component location, int in_product) { + super(in_frame, "Device Selection", true); + + product = in_product; + frame = in_frame; + value = null; + + AltosDevice[] devices = devices(); + + cancel_button = new JButton("Cancel"); + cancel_button.setActionCommand("cancel"); + cancel_button.addActionListener(this); + +// manage_bluetooth_button = new JButton("Manage Bluetooth"); +// manage_bluetooth_button.setActionCommand("manage"); +// manage_bluetooth_button.addActionListener(this); + + select_button = new JButton("Select"); + select_button.setActionCommand("select"); + select_button.addActionListener(this); + if (devices.length == 0) + select_button.setEnabled(false); + getRootPane().setDefaultButton(select_button); + + list = new JList(devices) { + //Subclass JList to workaround bug 4832765, which can cause the + //scroll pane to not let the user easily scroll up to the beginning + //of the list. An alternative would be to set the unitIncrement + //of the JScrollBar to a fixed value. You wouldn't get the nice + //aligned scrolling, but it should work. + public int getScrollableUnitIncrement(Rectangle visibleRect, + int orientation, + int direction) { + int row; + if (orientation == SwingConstants.VERTICAL && + direction < 0 && (row = getFirstVisibleIndex()) != -1) { + Rectangle r = getCellBounds(row, row); + if ((r.y == visibleRect.y) && (row != 0)) { + Point loc = r.getLocation(); + loc.y--; + int prevIndex = locationToIndex(loc); + Rectangle prevR = getCellBounds(prevIndex, prevIndex); + + if (prevR == null || prevR.y >= r.y) { + return 0; + } + return prevR.height; + } + } + return super.getScrollableUnitIncrement( + visibleRect, orientation, direction); + } + }; + + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + list.setLayoutOrientation(JList.HORIZONTAL_WRAP); + list.setVisibleRowCount(-1); + list.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + select_button.doClick(); //emulate button click + } + } + }); + JScrollPane listScroller = new JScrollPane(list); + listScroller.setPreferredSize(new Dimension(400, 80)); + listScroller.setAlignmentX(LEFT_ALIGNMENT); + + //Create a container so that we can add a title around + //the scroll pane. Can't add a title directly to the + //scroll pane because its background would be white. + //Lay out the label and scroll pane from top to bottom. + JPanel listPane = new JPanel(); + listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS)); + + JLabel label = new JLabel("Select Device"); + label.setLabelFor(list); + listPane.add(label); + listPane.add(Box.createRigidArea(new Dimension(0,5))); + listPane.add(listScroller); + listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); + + //Lay out the buttons from left to right. + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); + buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); + buttonPane.add(Box.createHorizontalGlue()); + buttonPane.add(cancel_button); + buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); +// buttonPane.add(manage_bluetooth_button); +// buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); + buttonPane.add(select_button); + + //Put everything together, using the content pane's BorderLayout. + Container contentPane = getContentPane(); + contentPane.add(listPane, BorderLayout.CENTER); + contentPane.add(buttonPane, BorderLayout.PAGE_END); + + //Initialize values. + if (devices != null && devices.length != 0) + list.setSelectedValue(devices[0], true); + pack(); + setLocationRelativeTo(location); + } + + //Handle clicks on the Set and Cancel buttons. + public void actionPerformed(ActionEvent e) { + if ("select".equals(e.getActionCommand())) + value = (AltosDevice)(list.getSelectedValue()); +// if ("manage".equals(e.getActionCommand())) { +// AltosBTManage.show(frame, AltosBTKnown.bt_known()); +// update_devices(); +// return; +// } + setVisible(false); + } + +} diff --git a/altosuilib/AltosUSBDevice.java b/altosuilib/AltosUSBDevice.java new file mode 100644 index 00000000..2f4e0dc6 --- /dev/null +++ b/altosuilib/AltosUSBDevice.java @@ -0,0 +1,112 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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.altosuilib; + +import java.util.*; +import libaltosJNI.*; + +public class AltosUSBDevice extends altos_device implements AltosDevice { + + public String toString() { + String name = getName(); + if (name == null) + name = "Altus Metrum"; + return String.format("%-20.20s %4d %s", + name, getSerial(), getPath()); + } + + public String toShortString() { + String name = getName(); + if (name == null) + name = "Altus Metrum"; + return String.format("%s %d %s", + name, getSerial(), getPath()); + + } + + public String getErrorString() { + altos_error error = new altos_error(); + + libaltos.altos_get_last_error(error); + return String.format("%s (%d)", error.getString(), error.getCode()); + } + + public SWIGTYPE_p_altos_file open() { + return libaltos.altos_open(this); + } + + private boolean isAltusMetrum() { + if (getVendor() != AltosUILib.vendor_altusmetrum) + return false; + if (getProduct() < AltosUILib.product_altusmetrum_min) + return false; + if (getProduct() > AltosUILib.product_altusmetrum_max) + return false; + return true; + } + + public boolean matchProduct(int want_product) { + + if (!isAltusMetrum()) + return false; + + if (want_product == AltosUILib.product_any) + return true; + + if (want_product == AltosUILib.product_basestation) + return matchProduct(AltosUILib.product_teledongle) || + matchProduct(AltosUILib.product_teleterra) || + matchProduct(AltosUILib.product_telebt) || + matchProduct(AltosUILib.product_megadongle); + + if (want_product == AltosUILib.product_altimeter) + return matchProduct(AltosUILib.product_telemetrum) || + matchProduct(AltosUILib.product_megametrum); + + int have_product = getProduct(); + + if (have_product == AltosUILib.product_altusmetrum) /* old devices match any request */ + return true; + + if (want_product == have_product) + return true; + + return false; + } + + static java.util.List list(int product) { + if (!AltosUILib.load_library()) + return null; + + SWIGTYPE_p_altos_list list = libaltos.altos_list_start(); + + ArrayList device_list = new ArrayList(); + if (list != null) { + for (;;) { + AltosUSBDevice device = new AltosUSBDevice(); + if (libaltos.altos_list_next(list, device) == 0) + break; + if (device.matchProduct(product)) + device_list.add(device); + } + libaltos.altos_list_finish(list); + } + + return device_list; + } +} \ No newline at end of file diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index 26aee7c4..e2ff8cb5 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -11,6 +11,9 @@ AltosUILibdir = $(datadir)/java AltosUILib_JAVA = \ AltosConfigureUI.java \ + AltosDevice.java \ + AltosDeviceDialog.java \ + AltosUSBDevice.java \ AltosFontListener.java \ AltosUIDialog.java \ AltosUIFrame.java \ diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index d7b266cf..6e884c80 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -116,6 +116,7 @@ altos_open(struct altos_device *device) return NULL; } + printf ("open\n"); // altos_set_last_error(12, "yeah yeah, failed again"); // free(file); // return NULL; @@ -148,6 +149,8 @@ altos_open(struct altos_device *device) return NULL; } cfmakeraw(&term); + cfsetospeed(&term, B9600); + cfsetispeed(&term, B9600); #ifdef USE_POLL term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; @@ -609,6 +612,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device) { struct altos_usbdev *dev; if (list->current >= list->ndev) { + printf ("end\n"); return 0; } dev = list->dev[list->current]; diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index e0de690c..a54b78a5 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -10,12 +10,15 @@ micropeakdir=$(datadir)/java micropeak_JAVA= \ MicroPeak.java \ MicroData.java \ + MicroDownload.java \ MicroFrame.java \ MicroGraph.java \ + MicroSave.java \ MicroSerial.java \ MicroStats.java \ MicroStatsTable.java \ MicroFileChooser.java \ + MicroDeviceDialog.java \ MicroUSB.java JFREECHART_CLASS= \ diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index ec9b83d8..8ccd5fd8 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -110,6 +110,7 @@ public class MicroData { private double time_step; private double ground_altitude; private ArrayList bytes; + String name; class FileEndedException extends Exception { @@ -310,12 +311,18 @@ public class MicroData { public void save (OutputStream f) throws IOException { for (int c : bytes) f.write(c); + f.write('\n'); } - public MicroData (InputStream f) throws IOException { + public void set_name(String name) { + this.name = name; + } + + public MicroData (InputStream f, String name) throws IOException, InterruptedException { + this.name = name; bytes = new ArrayList(); if (!find_header(f)) - throw new IOException(); + throw new IOException("No MicroPeak data header found"); try { file_crc = 0xffff; ground_pressure = get_32(f); @@ -354,9 +361,9 @@ public class MicroData { time_step = 0.192; } catch (FileEndedException fe) { - throw new IOException(); + throw new IOException("File Ended Unexpectedly"); } catch (NonHexcharException ne) { - throw new IOException(); + throw new IOException("Non hexadecimal character found"); } } diff --git a/micropeak/MicroDeviceDialog.java b/micropeak/MicroDeviceDialog.java new file mode 100644 index 00000000..7b8a630c --- /dev/null +++ b/micropeak/MicroDeviceDialog.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import org.altusmetrum.altosuilib.*; + +public class MicroDeviceDialog extends AltosDeviceDialog { + + public AltosDevice[] devices() { + java.util.List list = MicroUSB.list(); + int num_devices = list.size(); + AltosDevice[] devices = new AltosDevice[num_devices]; + + for (int i = 0; i < num_devices; i++) + devices[i] = list.get(i); + return devices; + } + + public MicroDeviceDialog (Frame in_frame, Component location) { + super(in_frame, location, 0); + } + + public static AltosDevice show (Component frameComp) { + Frame frame = JOptionPane.getFrameForComponent(frameComp); + MicroDeviceDialog dialog; + + dialog = new MicroDeviceDialog (frame, frameComp); + dialog.setVisible(true); + return dialog.getValue(); + } +} diff --git a/micropeak/MicroDownload.java b/micropeak/MicroDownload.java new file mode 100644 index 00000000..2e328b4a --- /dev/null +++ b/micropeak/MicroDownload.java @@ -0,0 +1,139 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.io.*; +import java.util.concurrent.*; +import java.util.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener { + MicroPeak owner; + Container pane; + AltosDevice device; + JButton cancel; + MicroData data; + MicroSerial serial; + + private void done_internal() { + setVisible(false); + if (data != null) { + owner = owner.SetData(data); + MicroSave save = new MicroSave(owner, data); + if (save.runDialog()) + owner.SetName(data.name); + } + dispose(); + } + + public void done() { + Runnable r = new Runnable() { + public void run() { + try { + done_internal(); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + public void run() { + try { + data = new MicroData(serial, device.toShortString()); + serial.close(); + } catch (FileNotFoundException fe) { + } catch (IOException ioe) { + } catch (InterruptedException ie) { + } + done(); + } + + Thread serial_thread; + + public void start() { + try { + serial = new MicroSerial(device); + } catch (FileNotFoundException fe) { + return; + } + serial_thread = new Thread(this); + serial_thread.start(); + } + + public void actionPerformed(ActionEvent ae) { + System.out.printf ("command %s\n", ae.getActionCommand()); + if (serial_thread != null) { + System.out.printf ("Interrupting serial_thread\n"); + serial.close(); + serial_thread.interrupt(); + } + } + + public MicroDownload(MicroPeak owner, AltosDevice device) { + super (owner, "Download MicroPeak Data", false); + + GridBagConstraints c; + Insets il = new Insets(4,4,4,4); + Insets ir = new Insets(4,4,4,4); + + this.owner = owner; + this.device = device; + + pane = getContentPane(); + pane.setLayout(new GridBagLayout()); + + c = new GridBagConstraints(); + c.gridx = 0; c.gridy = 0; + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + JLabel device_label = new JLabel("Device:"); + pane.add(device_label, c); + + c = new GridBagConstraints(); + c.gridx = 1; c.gridy = 0; + c.fill = GridBagConstraints.HORIZONTAL; + c.weightx = 1; + c.anchor = GridBagConstraints.LINE_START; + c.insets = ir; + JLabel device_value = new JLabel(device.toString()); + pane.add(device_value, c); + + cancel = new JButton("Cancel"); + c = new GridBagConstraints(); + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.CENTER; + c.gridx = 0; c.gridy = 1; + c.gridwidth = GridBagConstraints.REMAINDER; + Insets ic = new Insets(4,4,4,4); + c.insets = ic; + pane.add(cancel, c); + + cancel.addActionListener(this); + + pack(); + setLocationRelativeTo(owner); + setVisible(true); + this.start(); + } +} diff --git a/micropeak/MicroGraph.java b/micropeak/MicroGraph.java index 38f54fe0..b9b084f8 100644 --- a/micropeak/MicroGraph.java +++ b/micropeak/MicroGraph.java @@ -111,8 +111,13 @@ public class MicroGraph implements AltosUnitsListener { } } + public void setName (String name) { + chart.setTitle(name); + } + public void setData (MicroData data) { this.data = data; + chart.setTitle(data.name); resetData(); } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index c69f7167..5e375057 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -34,13 +34,36 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene MicroData data; Container container; JTabbedPane pane; + static int number_of_windows; - private void RunFile(InputStream input) { + MicroPeak SetData(MicroData data) { + MicroPeak mp = this; + if (this.data != null) { + mp = new MicroPeak(); + return mp.SetData(data); + } + this.data = data; + graph.setData(data); + stats.setData(data); + setTitle(data.name); + return this; + } + + void SetName(String name) { + graph.setName(name); + setTitle(name); + } + + private void RunFile(InputStream input, String name) { try { - data = new MicroData(input); - graph.setData(data); - stats.setData(data); + MicroData data = new MicroData(input, name); + SetData(data); } catch (IOException ioe) { + JOptionPane.showMessageDialog(this, + ioe.getMessage(), + "File Read Error", + JOptionPane.ERROR_MESSAGE); + } catch (InterruptedException ie) { } try { input.close(); @@ -50,8 +73,12 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene private void OpenFile(File filename) { try { - RunFile (new FileInputStream(filename)); + RunFile (new FileInputStream(filename), filename.getName()); } catch (FileNotFoundException fne) { + JOptionPane.showMessageDialog(this, + fne.getMessage(), + "Cannot open file", + JOptionPane.ERROR_MESSAGE); } } @@ -60,30 +87,44 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene InputStream input = chooser.runDialog(); if (input != null) - RunFile(input); + RunFile(input, chooser.filename); } private void Preferences() { new AltosConfigureUI(this); } - + private void DownloadData() { - java.util.List devices = MicroUSB.list(); - for (MicroUSB device : devices) - System.out.printf("device %s\n", device.toString()); + AltosDevice device = MicroDeviceDialog.show(this); + + if (device != null) + new MicroDownload(this, device); } + private void Save() { + if (data == null) { + JOptionPane.showMessageDialog(this, + "No data available", + "No data", + JOptionPane.INFORMATION_MESSAGE); + return; + } + MicroSave save = new MicroSave (this, data); + if (save.runDialog()) + SetName(data.name); + } + public void actionPerformed(ActionEvent ev) { if ("Exit".equals(ev.getActionCommand())) System.exit(0); else if ("Open".equals(ev.getActionCommand())) SelectFile(); - else if ("New".equals(ev.getActionCommand())) - new MicroPeak(); else if ("Download".equals(ev.getActionCommand())) DownloadData(); else if ("Preferences".equals(ev.getActionCommand())) Preferences(); + else if ("Save a Copy".equals(ev.getActionCommand())) + Save(); } public void itemStateChanged(ItemEvent e) { @@ -91,6 +132,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene public MicroPeak() { + ++number_of_windows; + AltosUIPreferences.set_component(this); container = getContentPane(); @@ -104,10 +147,6 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); - JMenuItem newAction = new JMenuItem("New"); - fileMenu.add(newAction); - newAction.addActionListener(this); - JMenuItem openAction = new JMenuItem("Open"); fileMenu.add(openAction); openAction.addActionListener(this); @@ -116,6 +155,10 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene fileMenu.add(downloadAction); downloadAction.addActionListener(this); + JMenuItem saveAction = new JMenuItem("Save a Copy"); + fileMenu.add(saveAction); + saveAction.addActionListener(this); + JMenuItem preferencesAction = new JMenuItem("Preferences"); fileMenu.add(preferencesAction); preferencesAction.addActionListener(this); @@ -128,7 +171,11 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { - System.exit(0); + setVisible(false); + dispose(); + --number_of_windows; + if (number_of_windows == 0) + System.exit(0); } }); diff --git a/micropeak/MicroSave.java b/micropeak/MicroSave.java new file mode 100644 index 00000000..cb4b4221 --- /dev/null +++ b/micropeak/MicroSave.java @@ -0,0 +1,102 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.io.*; +import java.util.concurrent.*; +import java.util.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroSave extends JFileChooser { + + JFrame frame; + MicroData data; + + public boolean runDialog() { + int ret; + + for (;;) { + ret = showSaveDialog(frame); + if (ret != APPROVE_OPTION) + return false; + File file; + String filename; + file = getSelectedFile(); + if (file == null) + continue; + if (!file.getName().contains(".")) { + String fullname = file.getPath(); + file = new File(fullname.concat(".mpd")); + } + filename = file.getName(); + if (file.exists()) { + if (file.isDirectory()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is a directory", + filename), + "Directory", + JOptionPane.ERROR_MESSAGE); + continue; + } + int r = JOptionPane.showConfirmDialog(frame, + String.format("\"%s\" already exists. Overwrite?", + filename), + "Overwrite file?", + JOptionPane.YES_NO_OPTION); + if (r != JOptionPane.YES_OPTION) + continue; + + if (!file.canWrite()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is not writable", + filename), + "File not writable", + JOptionPane.ERROR_MESSAGE); + continue; + } + } + try { + FileOutputStream fos = new FileOutputStream(file); + data.save(fos); + fos.close(); + data.set_name(filename); + return true; + } catch (FileNotFoundException fe) { + JOptionPane.showMessageDialog(frame, + fe.getMessage(), + "Cannot create file", + JOptionPane.ERROR_MESSAGE); + } catch (IOException ioe) { + } + } + } + + public MicroSave(JFrame frame, MicroData data) { + this.frame = frame; + this.data = data; + setDialogTitle("Save MicroPeak Data File"); + setFileFilter(new FileNameExtensionFilter("MicroPeak data file", + "mpd")); + setCurrentDirectory(AltosUIPreferences.logdir()); + } +} diff --git a/micropeak/MicroSerial.java b/micropeak/MicroSerial.java index a1a77a1d..15ef8582 100644 --- a/micropeak/MicroSerial.java +++ b/micropeak/MicroSerial.java @@ -27,6 +27,10 @@ public class MicroSerial extends InputStream { public int read() { int c = libaltos.altos_getchar(file, 0); + if (Thread.interrupted()) + return -1; + if (c == -1) + return -1; if (AltosUIPreferences.serial_debug) System.out.printf("%c", c); return c; @@ -39,12 +43,12 @@ public class MicroSerial extends InputStream { } } - public MicroSerial(MicroUSB usb) throws FileNotFoundException { - file = usb.open(); + public MicroSerial(AltosDevice device) throws FileNotFoundException { + file = device.open(); if (file == null) { - final String message = usb.getErrorString(); + final String message = device.getErrorString(); throw new FileNotFoundException(String.format("%s (%s)", - usb.toShortString(), + device.toShortString(), message)); } } diff --git a/micropeak/MicroUSB.java b/micropeak/MicroUSB.java index d48610fe..244f7bc0 100644 --- a/micropeak/MicroUSB.java +++ b/micropeak/MicroUSB.java @@ -16,10 +16,12 @@ */ package org.altusmetrum.micropeak; + import java.util.*; import libaltosJNI.*; +import org.altusmetrum.altosuilib.*; -public class MicroUSB extends altos_device { +public class MicroUSB extends altos_device implements AltosDevice { static boolean initialized = false; static boolean loaded_library = false; @@ -48,16 +50,16 @@ public class MicroUSB extends altos_device { String name = getName(); if (name == null) name = "Altus Metrum"; - return String.format("%-20.20s %4d %s", - name, getSerial(), getPath()); + return String.format("%-20.20s %s", + name, getPath()); } public String toShortString() { String name = getName(); if (name == null) name = "Altus Metrum"; - return String.format("%s %d %s", - name, getSerial(), getPath()); + return String.format("%s %s", + name, getPath()); } @@ -75,11 +77,15 @@ public class MicroUSB extends altos_device { private boolean isMicro() { if (getVendor() != 0x0403) return false; - if (getProduct() != 0x6001) + if (getProduct() != 0x6015) return false; return true; } + public boolean matchProduct(int product) { + return isMicro(); + } + static java.util.List list() { if (!load_library()) return null; @@ -92,6 +98,7 @@ public class MicroUSB extends altos_device { MicroUSB device = new MicroUSB(); if (libaltos.altos_list_next(list, device) == 0) break; + System.out.printf("Device %s\n", device.toString()); if (device.isMicro()) device_list.add(device); } -- cgit v1.2.3 From 93d640de65a1ecedfef89c96521c21632f96f372 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 11:22:11 -0800 Subject: micropoint: Add MicroDataPoint This holds height/speed/accel data all in one place Signed-off-by: Keith Packard --- micropeak/Makefile.am | 1 + micropeak/MicroData.java | 77 +++++++------------------------------------ micropeak/MicroDataPoint.java | 39 ++++++++++++++++++++++ micropeak/MicroGraph.java | 9 +++-- micropeak/MicroStats.java | 68 +++++++++++++------------------------- 5 files changed, 78 insertions(+), 116 deletions(-) create mode 100644 micropeak/MicroDataPoint.java (limited to 'micropeak/MicroData.java') diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index 2dd9c69c..26431bd5 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -10,6 +10,7 @@ micropeakdir=$(datadir)/java micropeak_JAVA= \ MicroPeak.java \ MicroData.java \ + MicroDataPoint.java \ MicroDownload.java \ MicroFrame.java \ MicroGraph.java \ diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 8ccd5fd8..2afd3cd7 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -22,7 +22,7 @@ import java.io.*; import java.util.*; import org.altusmetrum.AltosLib.*; -abstract class MicroIterator implements Iterator { +class MicroIterator implements Iterator { int i; MicroData data; @@ -30,6 +30,10 @@ abstract class MicroIterator implements Iterator { return i < data.pressures.length; } + public MicroDataPoint next() { + return new MicroDataPoint(data, i++); + } + public MicroIterator (MicroData data) { this.data = data; i = 0; @@ -39,66 +43,15 @@ abstract class MicroIterator implements Iterator { } } -class MicroHeightIterator extends MicroIterator { - public Double next() { - return data.height(i++); - } - - public MicroHeightIterator(MicroData data) { - super(data); - } -} - -class MicroHeightIterable implements Iterable { - MicroData data; - - public Iterator iterator() { - return new MicroHeightIterator(data); - } - - public MicroHeightIterable(MicroData data) { - this.data = data; - } -} - -class MicroSpeedIterator extends MicroIterator { - public Double next() { - return data.speed(i++); - } - public MicroSpeedIterator(MicroData data) { - super(data); - } -} - -class MicroSpeedIterable implements Iterable { - MicroData data; - - public Iterator iterator() { - return new MicroSpeedIterator(data); - } - - public MicroSpeedIterable(MicroData data) { - this.data = data; - } -} +class MicroIterable implements Iterable { -class MicroAccelIterator extends MicroIterator { - public Double next() { - return data.acceleration(i++); - } - public MicroAccelIterator(MicroData data) { - super(data); - } -} - -class MicroAccelIterable implements Iterable { MicroData data; - public Iterator iterator() { - return new MicroAccelIterator(data); + public Iterator iterator() { + return new MicroIterator(data); } - public MicroAccelIterable(MicroData data) { + public MicroIterable(MicroData data) { this.data = data; } } @@ -225,16 +178,8 @@ public class MicroData { return AltosConvert.pressure_to_altitude(pressures[i]); } - public Iterable heights() { - return new MicroHeightIterable(this); - } - - public Iterable speeds() { - return new MicroSpeedIterable(this); - } - - public Iterable accels() { - return new MicroAccelIterable(this); + public Iterable points() { + return new MicroIterable(this); } int fact(int n) { diff --git a/micropeak/MicroDataPoint.java b/micropeak/MicroDataPoint.java new file mode 100644 index 00000000..3fd1e641 --- /dev/null +++ b/micropeak/MicroDataPoint.java @@ -0,0 +1,39 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +public class MicroDataPoint { + public double time; + public double height; + public double speed; + public double accel; + + public MicroDataPoint (double height, double speed, double accel, double time) { + this.height = height; + this.speed = speed; + this.accel = accel; + this.time = time; + } + + public MicroDataPoint(MicroData data, int i) { + this(data.height(i), + data.speed(i), + data.acceleration(i), + data.time(i)); + } +} \ No newline at end of file diff --git a/micropeak/MicroGraph.java b/micropeak/MicroGraph.java index c5580634..8330a67b 100644 --- a/micropeak/MicroGraph.java +++ b/micropeak/MicroGraph.java @@ -106,11 +106,10 @@ public class MicroGraph implements AltosUnitsListener { heightSeries.clear(); speedSeries.clear(); accelSeries.clear(); - for (int i = 0; i < data.pressures.length; i++) { - double x = data.time(i); - heightSeries.add(x, AltosConvert.height.value(data.height(i))); - speedSeries.add(x, AltosConvert.speed.value(data.speed(i))); - accelSeries.add(x, AltosConvert.accel.value(data.acceleration(i))); + for (MicroDataPoint point : data.points()) { + heightSeries.add(point.time, AltosConvert.height.value(point.height)); + speedSeries.add(point.time, AltosConvert.speed.value(point.speed)); + accelSeries.add(point.time, AltosConvert.accel.value(point.accel)); } } diff --git a/micropeak/MicroStats.java b/micropeak/MicroStats.java index 6ae8a2b2..056fac7d 100644 --- a/micropeak/MicroStats.java +++ b/micropeak/MicroStats.java @@ -39,25 +39,21 @@ public class MicroStats { void find_landing() { landed_height = 0; - int t = 0; - for (double height : data.heights()) { - landed_height = height; - t++; + for (MicroDataPoint point : data.points()) { + landed_height = point.height; + landed_time = point.time; } - landed_time = data.time(t); - t = 0; boolean above = false; - for (double height : data.heights()) { - if (height > landed_height + 10) { + for (MicroDataPoint point : data.points()) { + if (point.height > landed_height + 10) { above = true; } else { - if (above && height < landed_height + 2) { + if (above && point.height < landed_height + 2) { above = false; - landed_time = data.time(t); + landed_time = point.time; } } - t++; } } @@ -65,13 +61,11 @@ public class MicroStats { apogee_height = 0; apogee_time = 0; - int t = 0; - for (double height : data.heights()) { - if (height > apogee_height) { - apogee_height = height; - apogee_time = data.time(t); + for (MicroDataPoint point : data.points()) { + if (point.height > apogee_height) { + apogee_height = point.height; + apogee_time = point.time; } - t++; } } @@ -79,47 +73,31 @@ public class MicroStats { coast_height = 0; coast_time = 0; - int t = 0; - for (double accel : data.accels()) { - if (accel < -9.8) + for (MicroDataPoint point : data.points()) { + if (point.accel < -9.8) break; - t++; - } - coast_time = data.time(t); - - int coast_t = t; - t = 0; - for (double height : data.heights()) { - if (t >= coast_t) { - coast_height = height; - break; - } - t++; + coast_time = point.time; + coast_height = point.height; } } void find_max_speed() { max_speed = 0; - int t = 0; - for (double speed : data.speeds()) { - if (data.time(t) > apogee_time) + for (MicroDataPoint point : data.points()) { + if (point.time > apogee_time) break; - if (speed > max_speed) - max_speed = speed; - t++; + if (point.speed > max_speed) + max_speed = point.speed; } } void find_max_accel() { max_accel = 0; - - int t = 0; - for (double accel : data.accels()) { - if (data.time(t) > apogee_time) + for (MicroDataPoint point : data.points()) { + if (point.time > apogee_time) break; - if (accel > max_accel) - max_accel = accel; - t++; + if (point.accel > max_accel) + max_accel = point.accel; } } -- cgit v1.2.3 From 2c423d9287c6b9ea7233f5e3430682cb1c865da1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 11:44:32 -0800 Subject: micropeak: Add CSV export Signed-off-by: Keith Packard --- micropeak/Makefile.am | 1 + micropeak/MicroData.java | 4 ++ micropeak/MicroDataPoint.java | 7 ++- micropeak/MicroExport.java | 106 ++++++++++++++++++++++++++++++++++++++++++ micropeak/MicroPeak.java | 22 ++++++++- 5 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 micropeak/MicroExport.java (limited to 'micropeak/MicroData.java') diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index 26431bd5..f5f8ccd9 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -12,6 +12,7 @@ micropeak_JAVA= \ MicroData.java \ MicroDataPoint.java \ MicroDownload.java \ + MicroExport.java \ MicroFrame.java \ MicroGraph.java \ MicroSave.java \ diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 2afd3cd7..836d3c35 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -221,6 +221,10 @@ public class MicroData { return alt; } + public double pressure(int i) { + return pressures[i]; + } + public double height(int i) { return altitude(i) - ground_altitude; } diff --git a/micropeak/MicroDataPoint.java b/micropeak/MicroDataPoint.java index 3fd1e641..c58708e6 100644 --- a/micropeak/MicroDataPoint.java +++ b/micropeak/MicroDataPoint.java @@ -19,11 +19,13 @@ package org.altusmetrum.micropeak; public class MicroDataPoint { public double time; + public double pressure; public double height; public double speed; public double accel; - public MicroDataPoint (double height, double speed, double accel, double time) { + public MicroDataPoint (double pressure, double height, double speed, double accel, double time) { + this.pressure = pressure; this.height = height; this.speed = speed; this.accel = accel; @@ -31,7 +33,8 @@ public class MicroDataPoint { } public MicroDataPoint(MicroData data, int i) { - this(data.height(i), + this(data.pressure(i), + data.height(i), data.speed(i), data.acceleration(i), data.time(i)); diff --git a/micropeak/MicroExport.java b/micropeak/MicroExport.java new file mode 100644 index 00000000..06a03469 --- /dev/null +++ b/micropeak/MicroExport.java @@ -0,0 +1,106 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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.micropeak; + +import java.io.*; +import java.util.ArrayList; + +import java.awt.*; +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroExport extends JFileChooser { + + JFrame frame; + MicroData data; + + public boolean runDialog() { + int ret; + + setSelectedFile(new File(AltosLib.replace_extension(data.name, ".csv"))); + for (;;) { + ret = showSaveDialog(frame); + if (ret != APPROVE_OPTION) + return false; + File file; + String filename; + file = getSelectedFile(); + if (file == null) + continue; + if (!file.getName().contains(".")) { + String fullname = file.getPath(); + file = new File(fullname.concat(".csv")); + } + filename = file.getName(); + if (file.exists()) { + if (file.isDirectory()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is a directory", + filename), + "Directory", + JOptionPane.ERROR_MESSAGE); + continue; + } + int r = JOptionPane.showConfirmDialog(frame, + String.format("\"%s\" already exists. Overwrite?", + filename), + "Overwrite file?", + JOptionPane.YES_NO_OPTION); + if (r != JOptionPane.YES_OPTION) + continue; + + if (!file.canWrite()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is not writable", + filename), + "File not writable", + JOptionPane.ERROR_MESSAGE); + continue; + } + } + try { + FileWriter fw = new FileWriter(file); + PrintWriter pw = new PrintWriter(fw); + pw.printf(" Time, Press, Height, Speed, Accel\n"); + for (MicroDataPoint point : data.points()) { + pw.printf("%5.2f,%6.0f,%7.1f,%7.2f,%7.2f\n", + point.time, point.pressure, point.height, point.speed, point.accel); + } + fw.close(); + return true; + } catch (FileNotFoundException fe) { + JOptionPane.showMessageDialog(frame, + fe.getMessage(), + "Cannot create file", + JOptionPane.ERROR_MESSAGE); + } catch (IOException ioe) { + } + } + } + + public MicroExport(JFrame frame, MicroData data) { + this.frame = frame; + this.data = data; + setDialogTitle("Export MicroPeak Data File"); + setFileFilter(new FileNameExtensionFilter("MicroPeak CSV file", + "csv")); + setCurrentDirectory(AltosUIPreferences.logdir()); + } +} diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index 544f3ae0..f2f09a10 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -101,12 +101,15 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene new MicroDownload(this, device); } - private void Save() { - if (data == null) { + private void no_data() { JOptionPane.showMessageDialog(this, "No data available", "No data", JOptionPane.INFORMATION_MESSAGE); + } + private void Save() { + if (data == null) { + no_data(); return; } MicroSave save = new MicroSave (this, data); @@ -114,6 +117,15 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene SetName(data.name); } + private void Export() { + if (data == null) { + no_data(); + return; + } + MicroExport export = new MicroExport (this, data); + export.runDialog(); + } + private void Close() { setVisible(false); dispose(); @@ -131,6 +143,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene SelectFile(); else if ("Download".equals(ev.getActionCommand())) DownloadData(); + else if ("Export".equals(ev.getActionCommand())) + Export(); else if ("Preferences".equals(ev.getActionCommand())) Preferences(); else if ("Save a Copy".equals(ev.getActionCommand())) @@ -169,6 +183,10 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene fileMenu.add(saveAction); saveAction.addActionListener(this); + JMenuItem exportAction = new JMenuItem("Export"); + fileMenu.add(exportAction); + exportAction.addActionListener(this); + JMenuItem preferencesAction = new JMenuItem("Preferences"); fileMenu.add(preferencesAction); preferencesAction.addActionListener(this); -- cgit v1.2.3 From f20781010a6560b7b359af269c502d098917c446 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 3 Jan 2013 17:31:01 -0800 Subject: micropeak: Add command line export option micropeak --export will create full of useful data. Signed-off-by: Keith Packard --- micropeak/MicroData.java | 9 ++++ micropeak/MicroExport.java | 15 +++---- micropeak/MicroFileChooser.java | 18 ++------ micropeak/MicroPeak.java | 94 ++++++++++++++++++++++++++++++++--------- micropeak/MicroSave.java | 10 +++-- 5 files changed, 100 insertions(+), 46 deletions(-) (limited to 'micropeak/MicroData.java') diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 836d3c35..fdfb2dc4 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -263,6 +263,15 @@ public class MicroData { f.write('\n'); } + public void export (Writer f) throws IOException { + PrintWriter pw = new PrintWriter(f); + pw.printf(" Time, Press, Height, Speed, Accel\n"); + for (MicroDataPoint point : points()) { + pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", + point.time, point.pressure, point.height, point.speed, point.accel); + } + } + public void set_name(String name) { this.name = name; } diff --git a/micropeak/MicroExport.java b/micropeak/MicroExport.java index 219184da..4b83bb4d 100644 --- a/micropeak/MicroExport.java +++ b/micropeak/MicroExport.java @@ -31,6 +31,12 @@ public class MicroExport extends JFileChooser { JFrame frame; MicroData data; + public static void export(File file, MicroData data) throws FileNotFoundException, IOException { + FileWriter fw = new FileWriter(file); + data.export(fw); + fw.close(); + } + public boolean runDialog() { int ret; @@ -76,14 +82,7 @@ public class MicroExport extends JFileChooser { } } try { - FileWriter fw = new FileWriter(file); - PrintWriter pw = new PrintWriter(fw); - pw.printf(" Time, Press, Height, Speed, Accel\n"); - for (MicroDataPoint point : data.points()) { - pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", - point.time, point.pressure, point.height, point.speed, point.accel); - } - fw.close(); + export(file, data); return true; } catch (FileNotFoundException fe) { JOptionPane.showMessageDialog(frame, diff --git a/micropeak/MicroFileChooser.java b/micropeak/MicroFileChooser.java index d2540987..21ddb0f8 100644 --- a/micropeak/MicroFileChooser.java +++ b/micropeak/MicroFileChooser.java @@ -36,24 +36,12 @@ public class MicroFileChooser extends JFileChooser { return file; } - public InputStream runDialog() { + public File runDialog() { int ret; ret = showOpenDialog(frame); - if (ret == APPROVE_OPTION) { - file = getSelectedFile(); - if (file == null) - return null; - filename = file.getName(); - try { - return new FileInputStream(file); - } catch (FileNotFoundException fe) { - JOptionPane.showMessageDialog(frame, - fe.getMessage(), - "Cannot open file", - JOptionPane.ERROR_MESSAGE); - } - } + if (ret == APPROVE_OPTION) + return getSelectedFile(); return null; } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index d4252fa9..185fa67e 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -56,40 +56,41 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene setTitle(name); } - private void RunFile(InputStream input, String name) { + private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException { + MicroData data = null; + FileInputStream fis = new FileInputStream(filename); try { - MicroData data = new MicroData(input, name); - SetData(data); - } catch (IOException ioe) { - JOptionPane.showMessageDialog(this, - ioe.getMessage(), - "File Read Error", - JOptionPane.ERROR_MESSAGE); + data = new MicroData((InputStream) fis, filename.getName()); } catch (InterruptedException ie) { + data = null; + } finally { + fis.close(); } - try { - input.close(); - } catch (IOException ioe) { - } + return data; } private void OpenFile(File filename) { try { - RunFile (new FileInputStream(filename), filename.getName()); + SetData(ReadFile(filename)); } catch (FileNotFoundException fne) { JOptionPane.showMessageDialog(this, fne.getMessage(), "Cannot open file", JOptionPane.ERROR_MESSAGE); + } catch (IOException ioe) { + JOptionPane.showMessageDialog(this, + ioe.getMessage(), + "File Read Error", + JOptionPane.ERROR_MESSAGE); } } private void SelectFile() { MicroFileChooser chooser = new MicroFileChooser(this); - InputStream input = chooser.runDialog(); + File file = chooser.runDialog(); - if (input != null) - RunFile(input, chooser.filename); + if (file != null) + OpenFile(file); } private void Preferences() { @@ -109,6 +110,7 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene "No data", JOptionPane.INFORMATION_MESSAGE); } + private void Save() { if (data == null) { no_data(); @@ -128,6 +130,30 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene export.runDialog(); } + private static void CommandGraph(File file) { + MicroPeak m = new MicroPeak(); + m.OpenFile(file); + } + + private static void CommandExport(File file) { + try { + MicroData d = ReadFile(file); + if (d != null) { + File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv")); + try { + System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath()); + MicroExport.export(csv, d); + } catch (FileNotFoundException fe) { + System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage()); + } catch (IOException ie) { + System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage()); + } + } + } catch (IOException ie) { + System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage()); + } + } + private void Close() { setVisible(false); dispose(); @@ -233,8 +259,17 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene setVisible(true); } + public static void help(int code) { + System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n"); + System.out.printf(" Options:\n"); + System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n"); + System.out.printf(" --graph\tgraph a flight\n"); + System.exit(code); + } + public static void main(final String[] args) { boolean opened = false; + boolean graphing = true; try { UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel()); @@ -242,11 +277,30 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene } for (int i = 0; i < args.length; i++) { - MicroPeak m = new MicroPeak(); - m.OpenFile(new File(args[i])); - opened = true; + System.out.printf ("Arg %d: %s\n", i, args[i]); + if (args[i].equals("--help")) + help(0); + else if (args[i].equals("--export")) + graphing = false; + else if (args[i].equals("--graph")) + graphing = true; + else if (args[i].startsWith("--")) + help(1); + else { + File file = new File(args[i]); + try { + if (graphing) + CommandGraph(file); + else + CommandExport(file); + opened = true; + } catch (Exception e) { + System.err.printf("Error processing \"%s\": %s\n", + file.getName(), e.getMessage()); + } + } } if (!opened) new MicroPeak(); } -} \ No newline at end of file +} diff --git a/micropeak/MicroSave.java b/micropeak/MicroSave.java index cb4b4221..2664f170 100644 --- a/micropeak/MicroSave.java +++ b/micropeak/MicroSave.java @@ -32,6 +32,12 @@ public class MicroSave extends JFileChooser { JFrame frame; MicroData data; + public static void save(File file, MicroData data) throws FileNotFoundException, IOException { + FileOutputStream fos = new FileOutputStream(file); + data.save(fos); + fos.close(); + } + public boolean runDialog() { int ret; @@ -76,9 +82,7 @@ public class MicroSave extends JFileChooser { } } try { - FileOutputStream fos = new FileOutputStream(file); - data.save(fos); - fos.close(); + save(file, data); data.set_name(filename); return true; } catch (FileNotFoundException fe) { -- cgit v1.2.3 From 81088b42b3ea899c8d1b3f09ee4fe24378fa03c9 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 3 Jan 2013 17:40:19 -0800 Subject: micropeak: Export in lots of units meters, feet, mach and gs Signed-off-by: Keith Packard --- micropeak/MicroData.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'micropeak/MicroData.java') diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index fdfb2dc4..d3c8c43e 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -265,10 +265,19 @@ public class MicroData { public void export (Writer f) throws IOException { PrintWriter pw = new PrintWriter(f); - pw.printf(" Time, Press, Height, Speed, Accel\n"); + pw.printf(" Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(ft/s), Speed(mach), Accel(m/s²), Accel(ft/s²), Accel(g)\n"); for (MicroDataPoint point : points()) { - pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", - point.time, point.pressure, point.height, point.speed, point.accel); + pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%12.2f,%12.4f,%12.2f,%13.2f,%10.4f\n", + point.time, + point.pressure, + point.height, + AltosConvert.meters_to_feet(point.height), + point.speed, + AltosConvert.meters_to_feet(point.speed), + AltosConvert.meters_to_mach(point.speed), + point.accel, + AltosConvert.meters_to_feet(point.accel), + AltosConvert.meters_to_g(point.accel)); } } -- cgit v1.2.3 From ca284d8bef2f4bd360eaec58048ba9abdafc55bd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 3 Jan 2013 18:14:40 -0800 Subject: micropeak: Use data.export for Raw display. Change to MPH data.export already knows how to format stuff, so use that to construct the raw data presentation for the GUI too. Signed-off-by: Keith Packard --- altoslib/AltosSpeed.java | 6 +++--- micropeak/MicroData.java | 6 +++--- micropeak/MicroRaw.java | 14 ++++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'micropeak/MicroData.java') diff --git a/altoslib/AltosSpeed.java b/altoslib/AltosSpeed.java index af63ed17..4e2daf5a 100644 --- a/altoslib/AltosSpeed.java +++ b/altoslib/AltosSpeed.java @@ -21,19 +21,19 @@ public class AltosSpeed extends AltosUnits { public double value(double v) { if (AltosConvert.imperial_units) - return AltosConvert.meters_to_feet(v); + return AltosConvert.meters_to_mph(v); return v; } public String show_units() { if (AltosConvert.imperial_units) - return "ft/s"; + return "mph"; return "m/s"; } public String say_units() { if (AltosConvert.imperial_units) - return "feet per second"; + return "miles per hour"; return "meters per second"; } diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index d3c8c43e..f1204e11 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -265,15 +265,15 @@ public class MicroData { public void export (Writer f) throws IOException { PrintWriter pw = new PrintWriter(f); - pw.printf(" Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(ft/s), Speed(mach), Accel(m/s²), Accel(ft/s²), Accel(g)\n"); + pw.printf(" Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(mph), Speed(mach), Accel(m/s²), Accel(ft/s²), Accel(g)\n"); for (MicroDataPoint point : points()) { - pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%12.2f,%12.4f,%12.2f,%13.2f,%10.4f\n", + pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n", point.time, point.pressure, point.height, AltosConvert.meters_to_feet(point.height), point.speed, - AltosConvert.meters_to_feet(point.speed), + AltosConvert.meters_to_mph(point.speed), AltosConvert.meters_to_mach(point.speed), point.accel, AltosConvert.meters_to_feet(point.accel), diff --git a/micropeak/MicroRaw.java b/micropeak/MicroRaw.java index f5bea76f..dd480bfe 100644 --- a/micropeak/MicroRaw.java +++ b/micropeak/MicroRaw.java @@ -18,6 +18,7 @@ package org.altusmetrum.micropeak; import java.awt.*; +import java.io.*; import javax.swing.*; import org.altusmetrum.AltosLib.*; import org.altusmetrum.altosuilib.*; @@ -25,12 +26,13 @@ import org.altusmetrum.altosuilib.*; public class MicroRaw extends JTextArea { public void setData(MicroData data) { - setRows(data.pressures.length); - setText(" Time, Press, Height, Speed, Accel\n"); - for (MicroDataPoint point : data.points()) { - append(String.format( - "%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", - point.time, point.pressure, point.height, point.speed, point.accel)); + StringWriter sw = new StringWriter(); + try { + data.export(sw); + setRows(data.pressures.length + 1); + setText(sw.toString()); + } catch (IOException ie) { + setText(String.format("Error writing data: %s", ie.getMessage())); } } -- cgit v1.2.3