rusefi/java_console/ui/src/main/java/com/rusefi/SensorSnifferPane.java

194 lines
6.9 KiB
Java
Raw Normal View History

2015-07-10 06:01:56 -07:00
package com.rusefi;
2022-02-12 13:20:38 -08:00
import com.devexperts.logging.Logging;
import com.rusefi.config.generated.Fields;
2015-07-10 06:01:56 -07:00
import com.rusefi.ui.RpmLabel;
import com.rusefi.ui.RpmModel;
2020-06-25 17:18:02 -07:00
import com.rusefi.ui.UIContext;
2016-06-20 15:01:52 -07:00
import com.rusefi.ui.config.ConfigField;
2015-07-10 06:01:56 -07:00
import com.rusefi.ui.config.EnumConfigField;
import com.rusefi.ui.engine.EngineSnifferPanel;
import com.rusefi.core.preferences.storage.Node;
2015-07-10 06:01:56 -07:00
import com.rusefi.ui.util.URLLabel;
2016-06-20 15:01:52 -07:00
import com.rusefi.ui.util.UiUtils;
2015-09-18 15:01:25 -07:00
import com.rusefi.ui.widgets.AnyCommand;
2015-07-10 06:01:56 -07:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;
2022-02-12 13:20:38 -08:00
import static com.devexperts.logging.Logging.getLogging;
2015-07-10 06:01:56 -07:00
/**
* Date: 12/21/13
2020-06-09 17:08:16 -07:00
* Andrey Belomutskiy, (c) 2013-2020
2015-07-10 06:01:56 -07:00
*/
public class SensorSnifferPane {
2022-02-12 13:20:38 -08:00
private static final Logging log = getLogging(SensorSnifferPane.class);
2015-07-10 06:01:56 -07:00
private static final String HELP_URL = "http://rusefi.com/wiki/index.php?title=Manual:DevConsole#Analog_Chart";
private final TreeMap<Double, Double> values = new TreeMap<>();
2015-09-13 15:01:23 -07:00
private final SensorSnifferCanvas canvas = new SensorSnifferCanvas();
2015-07-10 06:01:56 -07:00
private double minX;
private double maxX;
private double minY;
private double maxY;
private final JPanel content = new JPanel(new BorderLayout());
2017-01-29 06:06:38 -08:00
private final AnyCommand command;
2015-07-10 06:01:56 -07:00
private boolean paused = false;
2020-06-25 17:18:02 -07:00
public SensorSnifferPane(UIContext uiContext, Node config) {
2020-06-25 20:12:29 -07:00
uiContext.sensorSnifferCentral.addListener(new SensorSnifferCentral.AnalogChartListener() {
2015-07-10 06:01:56 -07:00
@Override
public void onAnalogChart(final String message) {
// this callback is invoked from the connectivity thread, need to handle in AWT for thread-safety
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
2017-05-21 15:02:58 -07:00
unpackValues(message, values);
2015-07-10 06:01:56 -07:00
if (!paused) {
processValues();
UiUtils.trueRepaint(canvas);
}
}
});
}
});
final JPanel upperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
2016-11-19 09:02:47 -08:00
final JButton pauseButton = UiUtils.createPauseButton();
2015-07-10 06:01:56 -07:00
2016-11-19 09:02:47 -08:00
JButton clearButton = UiUtils.createClearButton();
2015-07-10 06:01:56 -07:00
clearButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
clear();
UiUtils.trueRepaint(canvas);
2016-11-19 09:02:47 -08:00
setPaused(pauseButton, false);
2015-07-10 06:01:56 -07:00
}
});
upperPanel.add(clearButton);
JButton saveImageButton = UiUtils.createSaveImageButton();
upperPanel.add(saveImageButton);
saveImageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rpm = RpmModel.getInstance().getValue();
2022-02-12 13:16:11 -08:00
String fileName = FileLog.getDate() + "_rpm_" + rpm + "_sensor" + ".png";
2015-07-10 06:01:56 -07:00
UiUtils.saveImageWithPrompt(fileName, upperPanel, canvas);
}
}
);
upperPanel.add(pauseButton);
2020-06-25 17:51:09 -07:00
upperPanel.add(new RpmLabel(uiContext,2).getContent());
2015-07-10 06:01:56 -07:00
2020-06-25 18:05:06 -07:00
command = AnyCommand.createField(uiContext, config, true, false);
2017-01-29 06:06:38 -08:00
upperPanel.add(command.getContent());
2015-09-18 15:01:25 -07:00
2015-07-10 06:01:56 -07:00
upperPanel.add(new URLLabel(EngineSnifferPanel.HELP_TEXT, HELP_URL));
pauseButton.addActionListener(new
ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2016-11-19 09:02:47 -08:00
setPaused(pauseButton, !paused);
2015-07-10 06:01:56 -07:00
}
}
);
// upperPanel.setBorder(BorderFactory.createLineBorder(Color.orange));
content.add(upperPanel, BorderLayout.NORTH);
content.add(canvas, BorderLayout.CENTER);
final JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
lowerPanel.setBorder(BorderFactory.createLineBorder(Color.cyan));
content.add(lowerPanel, BorderLayout.SOUTH);
2020-06-25 17:18:02 -07:00
lowerPanel.add(new EnumConfigField(uiContext, Fields.SENSORCHARTMODE, "Mode").getContent());
2021-03-04 10:42:52 -08:00
lowerPanel.add(new ConfigField(uiContext, Fields.SENSORSNIFFERRPMTHRESHOLD, "RPM threshold").getContent());
2015-07-10 06:01:56 -07:00
}
2016-11-19 09:02:47 -08:00
private void setPaused(JButton pauseButton, boolean isPaused) {
paused = isPaused;
UiUtils.setPauseButtonText(pauseButton, paused);
}
2015-07-10 06:01:56 -07:00
private void clear() {
minX = maxX = minY = maxY = 0;
values.clear();
}
private void processValues() {
List<Double> keys = new ArrayList<>(values.keySet());
minX = keys.get(0);
maxX = keys.get(keys.size() - 1);
2022-02-12 13:20:38 -08:00
log.info("Analog chart from " + minX + " to " + maxX);
2015-07-10 06:01:56 -07:00
TreeSet<Double> sortedValues = new TreeSet<>(values.values());
2015-07-10 06:01:56 -07:00
List<Double> values = new ArrayList<>(sortedValues);
minY = values.get(0);
maxY = values.get(values.size() - 1);
}
public JComponent getPanel() {
return content;
}
2017-01-29 06:06:38 -08:00
public ActionListener getTabSelectedListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
command.requestFocus();
}
};
}
2015-09-13 15:01:23 -07:00
private class SensorSnifferCanvas extends JComponent {
2015-07-10 06:01:56 -07:00
@Override
public void paint(Graphics g) {
super.paint(g);
Dimension size = getSize();
g.drawString("X range from " + minX + " to " + maxX, 4, 20);
g.drawString("Y range from " + minY + " to " + maxY, 4, 40);
int prevX = 0;
int prevY = size.height;
double bX = size.width / (maxX - minX);
double bY = size.height / (maxY - minY);
for (Map.Entry<Double, Double> e : values.entrySet()) {
int x = (int) ((e.getKey() - minX) * bX);
int y = size.height - (int) ((e.getValue() - minY) * bY);
g.drawLine(prevX, prevY, x, y);
prevX = x;
prevY = y;
}
}
}
2017-05-21 15:02:58 -07:00
private static void unpackValues(String chart, TreeMap<Double, Double> destination) {
destination.clear();
2015-07-10 06:01:56 -07:00
String[] tokens = chart.split("\\|");
for (int i = 0; i < tokens.length - 1; ) {
String key = tokens[i++];
String value = tokens[i++];
2017-05-21 15:02:58 -07:00
destination.put(Double.parseDouble(key), Double.parseDouble(value));
2015-07-10 06:01:56 -07:00
}
}
}