refactoring & progress

This commit is contained in:
rusefi 2019-06-16 23:05:06 -04:00
parent 47daf9512e
commit 2b43c3f4f9
7 changed files with 56 additions and 42 deletions

View File

@ -1,7 +1,12 @@
package com.rusefi.config;
import com.opensr5.ConfigurationImage;
import com.rusefi.config.generated.Fields;
import com.rusefi.core.Pair;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.util.Objects;
import static com.rusefi.config.FieldType.*;
@ -125,6 +130,24 @@ public class Field {
'}';
}
@NotNull
public Number getValue(ConfigurationImage ci) {
Objects.requireNonNull(ci);
Number value;
ByteBuffer wrapped = getByteBuffer(ci);
if (getType() == INT) {
value = wrapped.getInt();
} else {
value = wrapped.getFloat();
}
return value;
}
@NotNull
public ByteBuffer getByteBuffer(ConfigurationImage ci) {
return ci.getByteBuffer(getOffset(), 4);
}
public static Field create(String name, int offset, FieldType type, int bitOffset) {
Field field = new Field(name, offset, type, bitOffset);
return field;

View File

@ -1,19 +1,24 @@
package com.rusefi.config;
public enum FieldType {
INT,
INT(4),
/**
* signed 16 bit type
*/
INT16,
INT8,
BIT,
FLOAT;
INT16(2),
INT8(1),
BIT(/*bits are stored in 4 byte packs */4),
FLOAT(4);
public static final String INT_TYPE_STRING = "int";
public static final String FLOAT_TYPE_STRING = "float";
public static final String BYTE_TYPE_STRING = "byte";
public static final String SHORT_TYPE_STRING = "short";
private final int storageSize;
FieldType(int storageSize) {
this.storageSize = storageSize;
}
private String getTypeForCommand() {
switch (this) {
@ -36,4 +41,8 @@ public enum FieldType {
public String getStoreCommand() {
return "set_" + getTypeForCommand();
}
public int getStorageSize() {
return storageSize;
}
}

View File

@ -1,5 +1,10 @@
package com.opensr5;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* (c) Andrey Belomutskiy
* 3/6/2015
@ -21,6 +26,14 @@ public class ConfigurationImage {
this.content = content;
}
@NotNull
public ByteBuffer getByteBuffer(int offset, int size) {
byte data[] = getRange(offset, size);
ByteBuffer wrapped = ByteBuffer.wrap(data);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
return wrapped;
}
public int getSize() {
return content.length;
}

View File

@ -195,7 +195,7 @@ public class FormulasPane {
double displacement = ConfigField.getFloatValue(ci, Fields.DISPLACEMENT);
int cylinderCount = ConfigField.getIntValue(ci, Fields.CYLINDERSCOUNT);
String cylinderDisplacement = oneDecimal(displacement / cylinderCount);
String injectorFlow = oneDecimal((float) ConfigField.getValue(ci, Fields.INJECTOR_FLOW));
String injectorFlow = oneDecimal((float) Fields.INJECTOR_FLOW.getValue(ci));
String tCharge = "$Tcharge=f(CLT=" + oneDecimal(Sensor.CLT) + "C,IAT=" + IAT
+ "C,TPS=" + tpsStr + "\\%, RPM = " + RPM + ")=" + T_CHARGE + "C$";

View File

@ -18,7 +18,6 @@ import com.rusefi.config.generated.Fields;
import com.rusefi.core.ISensorCentral;
import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral;
import com.rusefi.ui.config.BaseConfigField;
import com.rusefi.ui.storage.Node;
import javax.swing.*;
@ -336,7 +335,7 @@ public class FuelTunePane {
return;
}
for (int i = 0; i < array.length; i++)
array[i] = BaseConfigField.getByteBuffer(bp.getController(), offset + 4 * i).getFloat();
array[i] = bp.getController().getByteBuffer(offset + 4 * i, 4).getFloat();
System.out.println("FuelTunePane: Loaded " + Arrays.toString(array));
}

View File

@ -12,7 +12,6 @@ import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public abstract class BaseConfigField {
protected final JLabel status = new JLabel("P");
@ -77,19 +76,7 @@ public abstract class BaseConfigField {
@NotNull
protected ByteBuffer getByteBuffer(ConfigurationImage ci) {
return getByteBuffer(ci, field);
return field.getByteBuffer(ci);
}
@NotNull
public static ByteBuffer getByteBuffer(ConfigurationImage ci, Field field) {
return getByteBuffer(ci, field.getOffset());
}
@NotNull
public static ByteBuffer getByteBuffer(ConfigurationImage ci, int offset) {
byte data[] = ci.getRange(offset, 4);
ByteBuffer wrapped = ByteBuffer.wrap(data);
wrapped.order(ByteOrder.LITTLE_ENDIAN);
return wrapped;
}
}

View File

@ -2,19 +2,15 @@ package com.rusefi.ui.config;
import com.opensr5.ConfigurationImage;
import com.rusefi.config.Field;
import com.rusefi.config.FieldType;
import com.rusefi.core.MessagesCentral;
import com.rusefi.core.Pair;
import com.rusefi.ui.util.JTextFieldWithWidth;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.nio.ByteBuffer;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Objects;
public class ConfigField extends BaseConfigField {
private final JTextField view = new JTextFieldWithWidth(200);
@ -56,25 +52,12 @@ public class ConfigField extends BaseConfigField {
@Override
protected void loadValue(ConfigurationImage ci) {
Number value = getValue(ci, field);
Number value = field.getValue(ci);
setValue(value);
}
@NotNull
public static Number getValue(ConfigurationImage ci, Field field) {
Objects.requireNonNull(ci);
Number value;
ByteBuffer wrapped = getByteBuffer(ci, field);
if (field.getType() == FieldType.INT) {
value = wrapped.getInt();
} else {
value = wrapped.getFloat();
}
return value;
}
public static double getFloatValue(ConfigurationImage ci, Field field) {
float value = getValue(ci, field).floatValue();
float value = field.getValue(ci).floatValue();
// this hack is trying to restore lost precision. It's a lame hack
String str = df.format(value);
try {
@ -85,6 +68,6 @@ public class ConfigField extends BaseConfigField {
}
public static int getIntValue(ConfigurationImage ci, Field field) {
return getValue(ci, field).intValue();
return field.getValue(ci).intValue();
}
}