Implement endian processing for ECU data converter defined in logger definition

This commit is contained in:
Dale Schultz 2015-01-21 22:56:14 -05:00
parent f3852c8a90
commit fc5a2de4b3
3 changed files with 71 additions and 25 deletions

View File

@ -1,6 +1,6 @@
/* /*
* RomRaider Open-Source Tuning, Logging and Reflashing * RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2014 RomRaider.com * Copyright (C) 2006-2015 RomRaider.com
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import com.romraider.Settings;
import com.romraider.logger.ecu.comms.query.EcuQuery; import com.romraider.logger.ecu.comms.query.EcuQuery;
import com.romraider.logger.ecu.comms.query.EcuQueryImpl; import com.romraider.logger.ecu.comms.query.EcuQueryImpl;
import com.romraider.logger.ecu.definition.EcuAddress; import com.romraider.logger.ecu.definition.EcuAddress;
@ -50,6 +51,7 @@ public class TableAxisQueryParameterSet {
* @param expression - the equation to convert byte data to a real number. * @param expression - the equation to convert byte data to a real number.
* @param units - the value's unit of measure. * @param units - the value's unit of measure.
* @param size - the length of the Table's axis. * @param size - the length of the Table's axis.
* @param endian - the data endian.
* @return a List of ECU Query items. * @return a List of ECU Query items.
*/ */
public static final List<EcuQuery> build( public static final List<EcuQuery> build(
@ -82,7 +84,8 @@ public class TableAxisQueryParameterSet {
new EcuDataConvertor[] { new EcuDataConvertor[] {
new EcuParameterConvertorImpl( new EcuParameterConvertorImpl(
units, expression, "0.000", -1, storageType, units, expression, "0.000", -1, storageType,
new HashMap<String, String>(), getDefault() Settings.ENDIAN_BIG, new HashMap<String, String>(),
getDefault()
) )
} }
); );

View File

@ -1,6 +1,6 @@
/* /*
* RomRaider Open-Source Tuning, Logging and Reflashing * RomRaider Open-Source Tuning, Logging and Reflashing
* Copyright (C) 2006-2014 RomRaider.com * Copyright (C) 2006-2015 RomRaider.com
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -20,36 +20,41 @@
package com.romraider.logger.ecu.definition; package com.romraider.logger.ecu.definition;
import static com.romraider.logger.ecu.definition.xml.ConverterMaxMinDefaults.getDefault; import static com.romraider.logger.ecu.definition.xml.ConverterMaxMinDefaults.getDefault;
import static com.romraider.util.ByteUtil.asFloat;
import static com.romraider.util.ByteUtil.asSignedInt;
import static com.romraider.util.ByteUtil.asUnsignedInt; import static com.romraider.util.ByteUtil.asUnsignedInt;
import static com.romraider.util.JEPUtil.evaluate; import static com.romraider.util.JEPUtil.evaluate;
import static com.romraider.util.ParamChecker.checkNotNull; import static com.romraider.util.ParamChecker.checkNotNull;
import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; import static com.romraider.util.ParamChecker.checkNotNullOrEmpty;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.romraider.Settings;
import com.romraider.logger.ecu.ui.handler.dash.GaugeMinMax; import com.romraider.logger.ecu.ui.handler.dash.GaugeMinMax;
public final class EcuParameterConvertorImpl implements EcuDataConvertor { public final class EcuParameterConvertorImpl implements EcuDataConvertor {
private final String FLOAT = "float"; private static final String FLOAT = "float";
private final String INT = "int"; private static final String UINT = "uint";
private final String units; private final String units;
private final String expression; private final String expression;
private final DecimalFormat format; private final DecimalFormat format;
private final int bit; private final int bit;
private final String dataType; private final String dataType;
private final int endian;
private final Map<String, String> replaceMap; private final Map<String, String> replaceMap;
private final GaugeMinMax gaugeMinMax; private final GaugeMinMax gaugeMinMax;
public EcuParameterConvertorImpl() { public EcuParameterConvertorImpl() {
this("Raw data", "x", "0", -1, "uint", new HashMap<String, String>(), getDefault()); this("Raw data", "x", "0", -1, "uint", Settings.ENDIAN_BIG,
new HashMap<String, String>(), getDefault());
} }
public EcuParameterConvertorImpl(String units, String expression, String format, int bit, String dataType, Map<String, String> replaceMap, public EcuParameterConvertorImpl(String units, String expression,
GaugeMinMax gaugeMinMax) { String format, int bit, String dataType, int endian,
Map<String, String> replaceMap, GaugeMinMax gaugeMinMax) {
checkNotNullOrEmpty(units, "units"); checkNotNullOrEmpty(units, "units");
checkNotNullOrEmpty(expression, "expression"); checkNotNullOrEmpty(expression, "expression");
checkNotNullOrEmpty(format, "format"); checkNotNullOrEmpty(format, "format");
@ -59,27 +64,53 @@ public final class EcuParameterConvertorImpl implements EcuDataConvertor {
this.format = new DecimalFormat(format); this.format = new DecimalFormat(format);
this.bit = bit; this.bit = bit;
this.dataType = (dataType == null ? "uint8" : dataType); this.dataType = (dataType == null ? "uint8" : dataType);
this.endian = endian;
this.replaceMap = replaceMap; this.replaceMap = replaceMap;
this.gaugeMinMax = gaugeMinMax; this.gaugeMinMax = gaugeMinMax;
} }
public double convert(byte[] bytes) { public double convert(byte[] bytes) {
final ByteBuffer bb = ByteBuffer.wrap(bytes);
if (endian == Settings.ENDIAN_LITTLE) {
bb.order(ByteOrder.LITTLE_ENDIAN);
}
double result = 0;
if (bit >= 0 && bit <= 31) { if (bit >= 0 && bit <= 31) {
return (asUnsignedInt(bytes) & (1 << bit)) != 0 ? 1 : 0; return (asUnsignedInt(bytes) & (1 << bit)) != 0 ? 1 : 0;
} else {
double value = 0;
if (dataType.equalsIgnoreCase(FLOAT)) {
value = (double) asFloat(bytes, 0 , bytes.length);
}
else if (dataType.toLowerCase().startsWith(INT)) {
value = (double) asSignedInt(bytes);
}
else {
value = (double) asUnsignedInt(bytes);
}
double result = evaluate(expression, value);
return Double.isNaN(result) || Double.isInfinite(result) ? 0.0 : result;
} }
else if (dataType.equalsIgnoreCase(FLOAT)) {
result = evaluate(expression, bb.getFloat());
}
else {
long value = 0;
switch (bb.capacity()) {
case 1:
value = bb.get();
break;
case 2:
value = bb.getShort();
break;
case 4:
value = bb.getInt();
break;
}
if (dataType.toLowerCase().startsWith(UINT)) {
switch (bb.capacity()) {
case 1:
value = value & 0xff;
break;
case 2:
value = value & 0xffff;
break;
case 4:
value = value & 0xffffffffL;
break;
}
}
result = evaluate(expression, value);
}
return Double.isNaN(result) || Double.isInfinite(result) ? 0.0 : result;
} }
public String getUnits() { public String getUnits() {

View File

@ -39,6 +39,7 @@ import java.util.Set;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import com.romraider.Settings;
import com.romraider.io.connection.ConnectionProperties; import com.romraider.io.connection.ConnectionProperties;
import com.romraider.io.connection.ConnectionPropertiesImpl; import com.romraider.io.connection.ConnectionPropertiesImpl;
import com.romraider.logger.ecu.comms.query.EcuInit; import com.romraider.logger.ecu.comms.query.EcuInit;
@ -92,6 +93,7 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
private static final String ATTR_BIT = "bit"; private static final String ATTR_BIT = "bit";
private static final String ATTR_PARAMETER = "parameter"; private static final String ATTR_PARAMETER = "parameter";
private static final String ATTR_STORAGETYPE = "storagetype"; private static final String ATTR_STORAGETYPE = "storagetype";
private static final String ATTR_ENDIAN = "endian";
private static final String ATTR_BAUD = "baud"; private static final String ATTR_BAUD = "baud";
private static final String ATTR_DATABITS = "databits"; private static final String ATTR_DATABITS = "databits";
private static final String ATTR_STOPBITS = "stopbits"; private static final String ATTR_STOPBITS = "stopbits";
@ -144,6 +146,7 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
private String conversionExpression; private String conversionExpression;
private String conversionFormat; private String conversionFormat;
private String conversionStorageType; private String conversionStorageType;
private int conversionEndian;
private GaugeMinMax conversionGauge; private GaugeMinMax conversionGauge;
private String target; private String target;
private String version; private String version;
@ -229,6 +232,13 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
conversionExpression = attributes.getValue(ATTR_EXPRESSION); conversionExpression = attributes.getValue(ATTR_EXPRESSION);
conversionFormat = attributes.getValue(ATTR_FORMAT); conversionFormat = attributes.getValue(ATTR_FORMAT);
conversionStorageType = attributes.getValue(ATTR_STORAGETYPE); conversionStorageType = attributes.getValue(ATTR_STORAGETYPE);
String endian = attributes.getValue(ATTR_ENDIAN);
if (endian != null) {
conversionEndian = endian.equalsIgnoreCase("little") ? Settings.ENDIAN_LITTLE : Settings.ENDIAN_BIG;
}
else {
conversionEndian = Settings.ENDIAN_BIG;
}
double gaugeMin = getConversionMin(attributes, conversionUnits); double gaugeMin = getConversionMin(attributes, conversionUnits);
double gaugeMax = getConversionMax(attributes, conversionUnits); double gaugeMax = getConversionMax(attributes, conversionUnits);
double gaugeStep = getConversionStep(attributes, conversionUnits); double gaugeStep = getConversionStep(attributes, conversionUnits);
@ -329,8 +339,10 @@ public final class LoggerDefinitionHandler extends DefaultHandler {
derivedConvertorList.add(new EcuDerivedParameterConvertorImpl(conversionUnits, derivedConvertorList.add(new EcuDerivedParameterConvertorImpl(conversionUnits,
conversionExpression, conversionFormat, replaceMap, conversionGauge)); conversionExpression, conversionFormat, replaceMap, conversionGauge));
} else { } else {
convertorList.add(new EcuParameterConvertorImpl(conversionUnits, conversionExpression, conversionFormat, address.getBit(), convertorList.add(new EcuParameterConvertorImpl(
conversionStorageType, replaceMap, conversionGauge)); conversionUnits, conversionExpression, conversionFormat,
address.getBit(), conversionStorageType, conversionEndian,
replaceMap, conversionGauge));
} }
} else if (TAG_ECUPARAM.equals(qName)) { } else if (TAG_ECUPARAM.equals(qName)) {
if (ecuInit != null && ecuAddressMap.containsKey(ecuInit.getEcuId())) { if (ecuInit != null && ecuAddressMap.containsKey(ecuInit.getEcuId())) {