declarative convertors added to logger config file, JEP expression parsing moved into a utility class

git-svn-id: http://svn.3splooges.com/romraider-arch/trunk@243 d2e2e1cd-ba16-0410-be16-b7c4453c7c2d
This commit is contained in:
kascade 2006-08-16 13:14:41 +00:00
parent 466a0755b3
commit 7b23479b4a
17 changed files with 326 additions and 272 deletions

View File

@ -3,52 +3,63 @@
<protocols>
<protocol id="SSM">
<parameters>
<parameter id="Coolant Temperature" desc="Engine coolant temperature reading.">
<address>
<byte>0x000008</byte>
</address>
<convertor>enginuity.logger.definition.convertor.GenericTemperatureConvertor</convertor>
</parameter>
<parameter id="Engine Speed" desc="Engine speed in Revolutions Per Minute.">
<address>
<byte>0x00000E</byte>
<byte>0x00000F</byte>
</address>
<convertor>enginuity.logger.definition.convertor.EngineSpeedConvertor</convertor>
</parameter>
<parameter id="Throttle Opening Angle" desc="Engine throttle opening angle.">
<address>
<byte>0x000015</byte>
</address>
<convertor>enginuity.logger.definition.convertor.ThrottleOpeningAngleConvertor</convertor>
</parameter>
<parameter id="Accelerator Opening Angle" desc="Accelerator pedal opening angle.">
<address>
<byte>0x000029</byte>
</address>
<convertor>enginuity.logger.definition.convertor.AcceleratorOpeningAngleConvertor</convertor>
</parameter>
<parameter id="EGT" desc="Exhaust gas temperature reading.">
<address>
<byte>0x000106</byte>
</address>
<convertor>enginuity.logger.definition.convertor.ExhaustGasTemperatureConvertor</convertor>
</parameter>
<!-- More to come!! -->
<!-- New format -->
<!--
<parameter id="Coolant Temperature" desc="Engine coolant temperature reading.">
<address>
<byte>0x000008</byte>
</address>
<conversion>
<metric units="C" expr=""/>
<imperial units="F" expr=""/>
<metric units="C" expr="x-40" format="0"/>
</conversion>
</parameter>
-->
<parameter id="Engine Speed" desc="Engine speed in Revolutions Per Minute.">
<address>
<byte>0x00000E</byte>
<byte>0x00000F</byte>
</address>
<conversion>
<metric units="rpm" expr="x/4" format="0"/>
</conversion>
</parameter>
<parameter id="Throttle Opening Angle" desc="Engine throttle opening angle.">
<address>
<byte>0x000015</byte>
</address>
<conversion>
<metric units="%" expr="x/2.55" format="0.0"/>
</conversion>
</parameter>
<parameter id="Accelerator Pedal Angle" desc="Accelerator pedal angle.">
<address>
<byte>0x000029</byte>
</address>
<conversion>
<metric units="%" expr="x/2.55" format="0.0"/>
</conversion>
</parameter>
<parameter id="AFR" desc="Engine air/fuel ratio reading.">
<address>
<byte>0x000046</byte>
</address>
<conversion>
<metric units="Lambda" expr="x/128" format="0.0"/>
</conversion>
</parameter>
<parameter id="EGT" desc="Exhaust gas temperature reading.">
<address>
<byte>0x000106</byte>
</address>
<conversion>
<metric units="C" expr="(x+40)*5" format="0"/>
</conversion>
</parameter>
<!-- More to come!! -->
</parameters>
</protocol>

View File

@ -1,9 +1,9 @@
package enginuity.logger;
import enginuity.Settings;
import enginuity.logger.definition.EcuParameter;
import enginuity.logger.io.serial.SerialPortDiscoverer;
import enginuity.logger.io.serial.SerialPortDiscovererImpl;
import enginuity.logger.definition.EcuParameter;
import enginuity.logger.manager.QueryManager;
import enginuity.logger.manager.QueryManagerImpl;
import enginuity.logger.manager.TransmissionManager;

View File

@ -1,7 +1,5 @@
package enginuity.logger.definition;
import enginuity.logger.definition.convertor.EcuParameterConvertor;
public interface EcuParameter {
String getName();

View File

@ -0,0 +1,11 @@
package enginuity.logger.definition;
public interface EcuParameterConvertor {
double convert(byte[] bytes);
String getUnits();
String format(double value);
}

View File

@ -0,0 +1,36 @@
package enginuity.logger.definition;
import static enginuity.util.ByteUtil.asInt;
import static enginuity.util.JEPUtil.evaluate;
import static enginuity.util.ParamChecker.checkNotNullOrEmpty;
import java.text.DecimalFormat;
public final class EcuParameterConvertorImpl implements EcuParameterConvertor {
private final String units;
private final String expression;
private final DecimalFormat format;
public EcuParameterConvertorImpl(String units, String expression, String format) {
checkNotNullOrEmpty(units, "units");
checkNotNullOrEmpty(expression, "expression");
checkNotNullOrEmpty(format, "format");
this.units = units;
this.expression = expression;
this.format = new DecimalFormat(format);
}
public double convert(byte[] bytes) {
double value = (double) asInt(bytes);
return evaluate(expression, value);
}
public String getUnits() {
return units;
}
public String format(double value) {
return format.format(value);
}
}

View File

@ -1,6 +1,5 @@
package enginuity.logger.definition;
import enginuity.logger.definition.convertor.EcuParameterConvertor;
import static enginuity.util.ParamChecker.checkNotNull;
import static enginuity.util.ParamChecker.checkNotNullOrEmpty;

View File

@ -5,10 +5,10 @@ import static enginuity.util.ParamChecker.checkNotNullOrEmpty;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
public final class EcuParameterLoaderImpl implements EcuParameterLoader {

View File

@ -1,4 +0,0 @@
package enginuity.logger.definition;
public interface LoggerDefinition {
}

View File

@ -1,7 +1,5 @@
package enginuity.logger.definition;
import enginuity.logger.definition.convertor.EcuParameterConvertor;
import enginuity.logger.exception.ConfigurationException;
import enginuity.util.ParamChecker;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
@ -15,9 +13,13 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
private static final String TAG_PROTOCOL = "protocol";
private static final String TAG_PARAMETER = "parameter";
private static final String TAG_CONVERTOR = "convertor";
private static final String TAG_METRIC = "metric";
private static final String TAG_BYTE = "byte";
private static final String ATTR_ID = "id";
private static final String ATTR_DESC = "desc";
private static final String ATTR_UNITS = "units";
private static final String ATTR_EXPRESSION = "expr";
private static final String ATTR_FORMAT = "format";
private List<EcuParameter> params;
private String paramName;
private String paramDesc;
@ -44,6 +46,9 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
paramName = attributes.getValue(ATTR_ID);
paramDesc = attributes.getValue(ATTR_DESC);
addressList = new ArrayList<String>();
} else if (TAG_METRIC.equals(qName)) {
convertor = new EcuParameterConvertorImpl(attributes.getValue(ATTR_UNITS), attributes.getValue(ATTR_EXPRESSION),
attributes.getValue(ATTR_FORMAT));
}
}
charBuffer = new StringBuilder();
@ -61,12 +66,6 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
} else if (parseProtocol) {
if (TAG_BYTE.equals(qName)) {
addressList.add(charBuffer.toString());
} else if (TAG_CONVERTOR.equals(qName)) {
try {
convertor = (EcuParameterConvertor) Class.forName(charBuffer.toString().trim()).newInstance();
} catch (Exception e) {
throw new ConfigurationException(e);
}
} else if (TAG_PARAMETER.equals(qName)) {
String[] addresses = new String[addressList.size()];
addressList.toArray(addresses);

View File

@ -1,4 +0,0 @@
package enginuity.logger.definition;
public final class LoggerDefinitionImpl implements LoggerDefinition {
}

View File

@ -7,7 +7,6 @@ import enginuity.logger.exception.UnsupportedPortTypeException;
import enginuity.logger.io.serial.protocol.ConnectionProperties;
import enginuity.logger.io.serial.protocol.Protocol;
import enginuity.logger.query.RegisteredQuery;
import enginuity.logger.io.serial.SerialConnection;
import static enginuity.util.HexUtil.asHex;
import static enginuity.util.ParamChecker.checkGreaterThanZero;
import static enginuity.util.ParamChecker.checkNotNull;

View File

@ -8,8 +8,6 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import enginuity.logger.io.serial.SerialPortDiscoverer;
public final class SerialPortDiscovererImpl implements SerialPortDiscoverer {
@SuppressWarnings({"unchecked"})

View File

@ -1,7 +1,7 @@
package enginuity.logger.io.serial.protocol;
import enginuity.logger.query.RegisteredQuery;
import enginuity.logger.exception.InvalidResponseException;
import enginuity.logger.query.RegisteredQuery;
import static enginuity.util.ByteUtil.asByte;
import static enginuity.util.ByteUtil.asInt;
import static enginuity.util.HexUtil.asHex;

View File

@ -1,10 +1,10 @@
package enginuity.logger.manager;
import enginuity.Settings;
import enginuity.logger.io.serial.SerialConnection;
import enginuity.logger.io.serial.SerialConnectionImpl;
import enginuity.logger.exception.NotConnectedException;
import enginuity.logger.exception.SerialCommunicationException;
import enginuity.logger.io.serial.SerialConnection;
import enginuity.logger.io.serial.SerialConnectionImpl;
import enginuity.logger.io.serial.protocol.Protocol;
import enginuity.logger.io.serial.protocol.ProtocolFactory;
import enginuity.logger.query.LoggerCallback;

View File

@ -1,38 +1,36 @@
package enginuity.maps;
import java.awt.Color;
import java.awt.Font;
import enginuity.util.JEPUtil;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;
import java.text.DecimalFormat;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import org.nfunk.jep.JEP;
public class DataCell extends JLabel implements MouseListener, Serializable {
private double binValue = 0;
private double originalValue = 0;
private Scale scale = new Scale();
private String displayValue = "";
private double realValue = 0;
private Color scaledColor = new Color(0,0,0);
private Color highlightColor = new Color(155,155,255);
private Color increaseBorder = Color.RED;
private Color decreaseBorder = Color.BLUE;
private Boolean selected = false;
private Boolean highlighted = false;
private Table table;
private int x = 0;
private int y = 0;
private double compareValue = 0;
private int compareType = Table.COMPARE_OFF;
private int compareDisplay = Table.COMPARE_ABSOLUTE;
private double binValue = 0;
private double originalValue = 0;
private Scale scale = new Scale();
private String displayValue = "";
private Color scaledColor = new Color(0, 0, 0);
private Color highlightColor = new Color(155, 155, 255);
private Color increaseBorder = Color.RED;
private Color decreaseBorder = Color.BLUE;
private Boolean selected = false;
private Boolean highlighted = false;
private Table table;
private int x = 0;
private int y = 0;
private double compareValue = 0;
private int compareType = Table.COMPARE_OFF;
private int compareDisplay = Table.COMPARE_ABSOLUTE;
public DataCell() { }
public DataCell() {
}
public DataCell(Scale scale) {
this.scale = scale;
@ -54,30 +52,31 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
} else {
if (getCompareDisplay() == Table.COMPARE_ABSOLUTE) {
displayValue = formatter.format(
calcDisplayValue(binValue, table.getScale().getExpression()) -
calcDisplayValue(compareValue, table.getScale().getExpression()));
calcDisplayValue(binValue, table.getScale().getExpression()) -
calcDisplayValue(compareValue, table.getScale().getExpression()));
} else if (getCompareDisplay() == Table.COMPARE_PERCENT) {
double difference = calcDisplayValue(binValue, table.getScale().getExpression()) -
calcDisplayValue(compareValue, table.getScale().getExpression());
if (difference == 0) displayValue = "0%";
else displayValue = (int)(difference / calcDisplayValue(binValue, table.getScale().getExpression()) * 100)+"%";
calcDisplayValue(compareValue, table.getScale().getExpression());
if (difference == 0) {
displayValue = "0%";
} else {
displayValue = (int) (difference / calcDisplayValue(binValue, table.getScale().getExpression()) * 100) + "%";
}
}
}
setText(displayValue);
}
public double calcDisplayValue(double input, String expression) {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", input);
parser.parseExpression(expression);
return parser.getValue();
return JEPUtil.evaluate(expression, input);
}
public void setColor(Color color) {
scaledColor = color;
if (!selected) super.setBackground(color);
if (!selected) {
super.setBackground(color);
}
}
public void setDisplayValue(String displayValue) {
@ -86,21 +85,21 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
}
public void setBinValue(double binValue) {
this.binValue = binValue;
this.binValue = binValue;
// make sure it's in range
if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT) {
if (binValue < 0) {
this.setBinValue(0);
// make sure it's in range
if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT) {
if (binValue < 0) {
this.setBinValue(0);
} else if (binValue > Math.pow(256, table.getStorageType()) - 1) {
this.setBinValue((int)(Math.pow(256, table.getStorageType()) - 1));
} else if (binValue > Math.pow(256, table.getStorageType()) - 1) {
this.setBinValue((int) (Math.pow(256, table.getStorageType()) - 1));
}
}
}
}
this.updateDisplayValue();
}
this.updateDisplayValue();
}
public double getBinValue() {
return binValue;
@ -132,7 +131,9 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
if (highlighted) {
this.setBackground(getHighlightColor());
} else {
if (!selected) this.setBackground(scaledColor);
if (!selected) {
this.setBackground(scaledColor);
}
}
}
}
@ -147,7 +148,9 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
public void mousePressed(MouseEvent e) {
if (!table.isStatic()) {
if (!e.isControlDown()) table.clearSelection();
if (!e.isControlDown()) {
table.clearSelection();
}
table.startHighlight(x, y);
}
requestFocus();
@ -159,19 +162,23 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
}
}
public void mouseClicked(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void increment(double increment) {
double oldValue = Double.parseDouble(getText());
if (table.getScale().getCoarseIncrement() < 0) increment = 0 - increment;
if (table.getScale().getCoarseIncrement() < 0) {
increment = 0 - increment;
}
setRealValue((calcDisplayValue(binValue,
scale.getExpression()) + increment) + "");
setRealValue(String.valueOf((calcDisplayValue(binValue, scale.getExpression()) + increment)));
// make sure table is incremented if change isnt great enough
int maxValue = (int)Math.pow(8, (double)table.getStorageType());
int maxValue = (int) Math.pow(8, (double) table.getStorageType());
if (table.getStorageType() != Table.STORAGE_TYPE_FLOAT &&
oldValue == Double.parseDouble(getText()) &&
@ -221,17 +228,14 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
public void setRealValue(String input) {
// create parser
if (!input.equalsIgnoreCase("x")) {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", Double.parseDouble(input));
parser.parseExpression(table.getScale().getByteExpression());
if (!"x".equalsIgnoreCase(input)) {
double result = JEPUtil.evaluate(table.getScale().getByteExpression(), Double.parseDouble(input));
if (table.getStorageType() == Table.STORAGE_TYPE_FLOAT) {
this.setBinValue(parser.getValue());
this.setBinValue(result);
} else {
this.setBinValue((int)Math.round(parser.getValue()));
this.setBinValue((int) Math.round(result));
}
}
@ -282,11 +286,8 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
}
public void setCompareRealValue(String input) {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", Double.parseDouble(input));
parser.parseExpression(table.getScale().getByteExpression());
this.setCompareValue((int)Math.round(parser.getValue()));
double result = JEPUtil.evaluate(table.getScale().getByteExpression(), Double.parseDouble(input));
this.setCompareValue((int) Math.round(result));
}
public void setCompareDisplay(int compareDisplay) {
@ -298,14 +299,13 @@ public class DataCell extends JLabel implements MouseListener, Serializable {
}
public void multiply(double factor) {
setRealValue(Double.parseDouble(getText()) * factor+"");
setRealValue(String.valueOf(Double.parseDouble(getText()) * factor));
}
/**
* Used by 3d graph renderer.
* @return
*/
public double getValue() {
return calcDisplayValue(binValue, table.getScale().getExpression());
}
public double getValue() {
return calcDisplayValue(binValue, table.getScale().getExpression());
}
}

View File

@ -3,8 +3,8 @@ package enginuity.maps;
import enginuity.Settings;
import enginuity.swing.TableFrame;
import static enginuity.util.ColorScaler.getScaledColor;
import enginuity.util.JEPUtil;
import enginuity.xml.RomAttributeParser;
import org.nfunk.jep.JEP;
import javax.swing.*;
import javax.swing.border.LineBorder;
@ -23,64 +23,64 @@ import java.util.Vector;
public abstract class Table extends JPanel implements Serializable {
public static final int ENDIAN_LITTLE= 1;
public static final int ENDIAN_BIG = 2;
public static final int ENDIAN_LITTLE = 1;
public static final int ENDIAN_BIG = 2;
public static final int TABLE_1D = 1;
public static final int TABLE_2D = 2;
public static final int TABLE_3D = 3;
public static final int TABLE_1D = 1;
public static final int TABLE_2D = 2;
public static final int TABLE_3D = 3;
public static final int TABLE_X_AXIS = 4;
public static final int TABLE_Y_AXIS = 5;
public static final int TABLE_SWITCH = 6;
public static final int COMPARE_OFF = 0;
public static final int COMPARE_OFF = 0;
public static final int COMPARE_ORIGINAL = 1;
public static final int COMPARE_TABLE = 2;
public static final int COMPARE_PERCENT = 0;
public static final int COMPARE_TABLE = 2;
public static final int COMPARE_PERCENT = 0;
public static final int COMPARE_ABSOLUTE = 1;
public static final int STORAGE_TYPE_FLOAT = 99;
protected static final Color UNCHANGED_VALUE_COLOR = new Color(160, 160, 160);
protected String name;
protected int type;
protected String category = "Other";
protected String description = "";
protected Vector<Scale> scales = new Vector<Scale>();
protected int scaleIndex = 0; // index of selected scale
protected String name;
protected int type;
protected String category = "Other";
protected String description = "";
protected Vector<Scale> scales = new Vector<Scale>();
protected int scaleIndex = 0; // index of selected scale
protected int storageAddress;
protected int storageType;
protected int endian;
protected boolean flip;
protected DataCell[] data = new DataCell[0];
protected boolean isStatic = false;
protected boolean beforeRam = false;
protected int ramOffset = 0;
protected BorderLayout borderLayout = new BorderLayout();
protected GridLayout centerLayout = new GridLayout(1,1,0,0);
protected JPanel centerPanel = new JPanel(centerLayout);
protected TableFrame frame;
protected int verticalOverhead = 103;
protected int horizontalOverhead = 2;
protected int cellHeight = 18;
protected int cellWidth = 42;
protected int minHeight = 100;
protected int minWidth = 425;
protected Rom container;
protected int highlightX;
protected int highlightY;
protected boolean highlight = false;
protected Table axisParent;
protected Color maxColor;
protected Color minColor;
protected boolean isAxis = false;
protected int compareType = 0;
protected int compareDisplay = 1;
protected int userLevel = 0;
protected Settings settings;
protected boolean locked = false;
protected int storageAddress;
protected int storageType;
protected int endian;
protected boolean flip;
protected DataCell[] data = new DataCell[0];
protected boolean isStatic = false;
protected boolean beforeRam = false;
protected int ramOffset = 0;
protected BorderLayout borderLayout = new BorderLayout();
protected GridLayout centerLayout = new GridLayout(1, 1, 0, 0);
protected JPanel centerPanel = new JPanel(centerLayout);
protected TableFrame frame;
protected int verticalOverhead = 103;
protected int horizontalOverhead = 2;
protected int cellHeight = 18;
protected int cellWidth = 42;
protected int minHeight = 100;
protected int minWidth = 425;
protected Rom container;
protected int highlightX;
protected int highlightY;
protected boolean highlight = false;
protected Table axisParent;
protected Color maxColor;
protected Color minColor;
protected boolean isAxis = false;
protected int compareType = 0;
protected int compareDisplay = 1;
protected int userLevel = 0;
protected Settings settings;
protected boolean locked = false;
public Table(Settings settings) {
this.setSettings(settings);
@ -225,7 +225,7 @@ public abstract class Table extends JPanel implements Serializable {
KeyStroke num7 = KeyStroke.getKeyStroke('7');
KeyStroke num8 = KeyStroke.getKeyStroke('8');
KeyStroke num9 = KeyStroke.getKeyStroke('9');
KeyStroke mulKey = KeyStroke.getKeyStroke('*');
KeyStroke mulKey = KeyStroke.getKeyStroke('*');
KeyStroke mulKeys = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK);
KeyStroke numPoint = KeyStroke.getKeyStroke('.');
KeyStroke copy = KeyStroke.getKeyStroke("control C");
@ -528,12 +528,11 @@ public abstract class Table extends JPanel implements Serializable {
Double.parseDouble(data[i].getText()) < low) {
// value exceeds limit
data[i].setColor(getSettings().getWarningColor());
data[i].setColor(getSettings().getWarningColor());
} else {
// limits not set, scale based on table values
double scale = 0;
double scale;
if (high - low == 0) {
// if all values are the same, color will be middle value
scale = .5;
@ -998,23 +997,18 @@ public abstract class Table extends JPanel implements Serializable {
public void validateScaling() {
if (type != Table.TABLE_SWITCH && !isStatic) {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", 5);
// make sure a scale is present
if (scales.isEmpty()) {
scales.add(new Scale());
}
parser.parseExpression(scales.get(scaleIndex).getExpression());
double toReal = parser.getValue(); // calculate real world value of "5"
parser.addVariable("x", toReal);
parser.parseExpression(scales.get(scaleIndex).getByteExpression());
double startValue = 5;
double toReal = JEPUtil.evaluate(scales.get(scaleIndex).getExpression(), startValue); // calculate real world value of "5"
double endValue = JEPUtil.evaluate(scales.get(scaleIndex).getByteExpression(), toReal);
// if real to byte doesn't equal 5, report conflict
if (Math.abs(parser.getValue() - 5) > .001) {
if (Math.abs(endValue - startValue) > .001) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 1));

View File

@ -0,0 +1,17 @@
package enginuity.util;
import org.nfunk.jep.JEP;
public final class JEPUtil {
private JEPUtil() {
}
public static double evaluate(String expression, double value) {
JEP parser = new JEP();
parser.initSymTab(); // clear the contents of the symbol table
parser.addVariable("x", value);
parser.parseExpression(expression);
return parser.getValue();
}
}