diff --git a/src/enginuity/newmaps/ecudata/DataCell.java b/src/enginuity/newmaps/ecudata/DataCell.java index c0017718..adf2672f 100644 --- a/src/enginuity/newmaps/ecudata/DataCell.java +++ b/src/enginuity/newmaps/ecudata/DataCell.java @@ -21,13 +21,40 @@ package enginuity.newmaps.ecudata; -public class DataCell { +import enginuity.util.ByteUtil; + +public class DataCell extends Number { byte[] bytes; - String displayValue; + int endian; + int storageType; - public DataCell(byte[] bytes, int storageType) { + public DataCell(byte[] bytes, int storageType, int endian) { this.bytes = bytes; + this.storageType = storageType; + this.endian = endian; + } + + public int intValue() { + return ByteUtil.asSignedInt(bytes, endian); + } + + public long longValue() { + // TODO: this.. + return 0; + } + + public float floatValue() { + return ByteUtil.asFloat(bytes, endian); + } + + public double doubleValue() { + // TODO: this.. + return 0; + } + + public byte[] byteValues() { + return bytes; } } diff --git a/src/enginuity/newmaps/ecudata/Table3DData.java b/src/enginuity/newmaps/ecudata/Table3DData.java index b433d55b..eafaf26d 100644 --- a/src/enginuity/newmaps/ecudata/Table3DData.java +++ b/src/enginuity/newmaps/ecudata/Table3DData.java @@ -23,10 +23,11 @@ package enginuity.newmaps.ecudata; import enginuity.newmaps.ecumetadata.Table3DMetadata; import enginuity.newmaps.exception.DataPopulationException; +import enginuity.util.ByteUtil; public class Table3DData extends TableData { - private float[][] values; + private DataCell[][] values; private AxisData xAxis; private AxisData yAxis; @@ -35,11 +36,11 @@ public class Table3DData extends TableData { populate(data); } - public float[][] getValues() { + public DataCell[][] getValues() { return values; } - public float getValueAt(int x, int y) throws IndexOutOfBoundsException { + public DataCell getValueAt(int x, int y) throws IndexOutOfBoundsException { return values[x][y]; } @@ -51,7 +52,13 @@ public class Table3DData extends TableData { } public boolean populate(byte[] data) { - Table3DMetadata meta = (Table3DMetadata)metadata; + + // populate axes first + xAxis.populate(data); + yAxis.populate(data); + + // Now populate the table itself + int dataSize = ByteUtil.getLengthInBytes(metadata.getScale().getStorageType()); return true; } diff --git a/src/enginuity/newmaps/util/ECUDataUtil.java b/src/enginuity/newmaps/util/ECUDataUtil.java index 6166c722..566c4a32 100644 --- a/src/enginuity/newmaps/util/ECUDataUtil.java +++ b/src/enginuity/newmaps/util/ECUDataUtil.java @@ -40,42 +40,6 @@ public final class ECUDataUtil { } - public static float binaryToReal(byte[] data, int dataType, int endian, String to_real) { - - float output = 0; - - // Check endian - if (endian == Scale.ENDIAN_LITTLE) data = ByteUtil.reverseEndian(data); - - // Calculate ECU values - if (dataType == Scale.STORAGE_TYPE_FLOAT) output = ByteUtil.asFloat(data); - else if (dataType == Scale.STORAGE_TYPE_INT8) output = ByteUtil.asSignedInt(data); - else if (dataType == Scale.STORAGE_TYPE_INT16) output = ByteUtil.asSignedInt(data); - else if (dataType == Scale.STORAGE_TYPE_INT32) output = ByteUtil.asSignedInt(data); - else if (dataType == Scale.STORAGE_TYPE_UINT8) output = ByteUtil.asUnsignedInt(data); - else if (dataType == Scale.STORAGE_TYPE_UINT16) output = ByteUtil.asUnsignedInt(data); - else if (dataType == Scale.STORAGE_TYPE_UINT32) output = ByteUtil.asUnsignedInt(data); - - return (float)JEPUtil.evaluate(to_real, (double)output); - } - - - public static byte[] realToBinary(float realValue, int dataType, int endian, String to_byte) { - - realValue = (float)JEPUtil.evaluate(to_byte, (double)realValue); - - if (dataType == Scale.STORAGE_TYPE_FLOAT) return ByteUtil.asBytes(realValue); - else if (dataType == Scale.STORAGE_TYPE_INT8) return ByteUtil.asSignedBytes((int)realValue); - else if (dataType == Scale.STORAGE_TYPE_INT16) return ByteUtil.asSignedBytes((int)realValue); - else if (dataType == Scale.STORAGE_TYPE_INT32) return ByteUtil.asSignedBytes((int)realValue); - else if (dataType == Scale.STORAGE_TYPE_UINT8) return ByteUtil.asUnsignedBytes((int)realValue); - else if (dataType == Scale.STORAGE_TYPE_UINT16) return ByteUtil.asUnsignedBytes((int)realValue); - else if (dataType == Scale.STORAGE_TYPE_UINT32) return ByteUtil.asUnsignedBytes((int)realValue); - - return null; - } - - public static byte[] getTableAsBytes() { // TODO: build byte arrays from table data return null; diff --git a/src/enginuity/util/ByteUtil.java b/src/enginuity/util/ByteUtil.java index 1d270344..79f575a9 100644 --- a/src/enginuity/util/ByteUtil.java +++ b/src/enginuity/util/ByteUtil.java @@ -31,7 +31,10 @@ public final class ByteUtil { private ByteUtil() { } - public static int asUnsignedInt(byte[] bytes) { + public static int asUnsignedInt(byte[] bytes, int endian) { + + if (endian == Scale.ENDIAN_LITTLE) bytes = reverseEndian(bytes); + int i = 0; for (int j = 0; j < bytes.length; j++) { if (j > 0) { @@ -42,7 +45,14 @@ public final class ByteUtil { return i; } - public static int asSignedInt(byte[] bytes) { + public static int asUnsignedInt(byte[] bytes) { + return asUnsignedInt(bytes, Scale.ENDIAN_BIG); + } + + public static int asSignedInt(byte[] bytes, int endian) { + + if (endian == Scale.ENDIAN_LITTLE) bytes = reverseEndian(bytes); + int i = 0; for (int j = 0; j < bytes.length; j++) { if (j > 0) { @@ -53,6 +63,10 @@ public final class ByteUtil { return i; } + public static int asSignedInt(byte[] bytes) { + return asSignedInt(bytes, Scale.ENDIAN_BIG); + } + public static int getLengthInBytes(int type) { if (type == Scale.STORAGE_TYPE_CHAR) return 1; @@ -76,12 +90,14 @@ public final class ByteUtil { return Byte.valueOf(b).intValue(); } - public static byte[] asUnsignedBytes(int i) { + public static byte[] asUnsignedBytes(int i, int endian) { byte[] b = new byte[4]; for (int j = 0; j < 4; j++) { int offset = (b.length - 1 - j) << 3; b[j] = (byte) ((i >>> offset) & 0xFF); } + + if (endian == Scale.ENDIAN_LITTLE) b = reverseEndian(b); return b; } @@ -93,17 +109,19 @@ public final class ByteUtil { return newData; } - public static byte[] asSignedBytes(int i) { + public static byte[] asSignedBytes(int i, int endian) { byte[] b = new byte[4]; for (int j = 0; j < 4; j++) { int offset = (b.length - 1 - j) << 3; b[j] = (byte) (i >>> offset); } + if (endian == Scale.ENDIAN_LITTLE) b = reverseEndian(b); return b; } - public static float asFloat(byte[] bytes) { - return Float.intBitsToFloat(asUnsignedInt(bytes)); + public static float asFloat(byte[] bytes, int endian) { + byte[] currentCell = { bytes[0],bytes[1],bytes[2],bytes[3] }; + return Float.intBitsToFloat(asUnsignedInt(currentCell, endian)); } public static byte[] asBytes(float f) {