Annotations in C++ code to produce formulas in rusEfi console #807
This commit is contained in:
parent
5805dfc0ff
commit
d9c3eba470
|
@ -37,6 +37,7 @@
|
|||
// we use this 'DISPLAY' macro if value is not used by C++ in current context
|
||||
#define DISPLAY(x)
|
||||
|
||||
#define DISPLAY_TAG(x)
|
||||
#define DISPLAY_TEXT(x)
|
||||
#define DISPLAY_SENSOR(x) {}
|
||||
#define DISPLAY_IF(x) x
|
||||
|
|
|
@ -79,6 +79,8 @@ float getTpsRateOfChange(void) {
|
|||
* */
|
||||
percent_t getTpsValue(int adc DECLARE_ENGINE_PARAMETER_SUFFIX) {
|
||||
|
||||
DISPLAY_TAG(TPS_SECTION);
|
||||
|
||||
DISPLAY_TEXT(Analog_MCU_reads);
|
||||
engine->engineState.currentTpsAdc = adc;
|
||||
#if !EFI_UNIT_TEST
|
||||
|
@ -197,7 +199,10 @@ percent_t getPedalPosition(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
|
|||
if (mockPedalPosition != MOCK_UNDEFINED) {
|
||||
return mockPedalPosition;
|
||||
}
|
||||
float voltage = getVoltageDivided("pPS", engineConfiguration->throttlePedalPositionAdcChannel);
|
||||
DISPLAY_TAG(PEGAL_SECTION);
|
||||
DISPLAY_TEXT(Analog_MCU_reads);
|
||||
|
||||
float voltage = getVoltageDivided("pPS", CONFIG(DISPLAY_CONFIG(throttlePedalPositionAdcChannel)));
|
||||
float result = interpolateMsg("pedal", engineConfiguration->throttlePedalUpVoltage, 0, engineConfiguration->throttlePedalWOTVoltage, 100, voltage);
|
||||
|
||||
// this would put the value into the 0-100 range
|
||||
|
|
|
@ -3,7 +3,11 @@ package com.rusefi.ldmp.generated;
|
|||
import com.rusefi.ldmp.*;
|
||||
|
||||
public class TpsMeta {
|
||||
public static final Request[] CONTENT = new Request[]{
|
||||
public static final Request[] PEGAL_SECTION = new Request[]{
|
||||
new TextRequest("Analog_MCU_reads"),
|
||||
new ConfigRequest("throttlePedalPositionAdcChannel"),
|
||||
};
|
||||
public static final Request[] TPS_SECTION = new Request[]{
|
||||
new TextRequest("Analog_MCU_reads"),
|
||||
new FieldRequest("tpsVoltageMCU"),
|
||||
new TextRequest("Volts"),
|
||||
|
|
|
@ -172,7 +172,7 @@ public class LiveDocPanel {
|
|||
ThermistorState.VALUES, ThermistorsMeta.CONTENT));
|
||||
|
||||
liveDocs.add(getPanel("Throttle Position Sensor", "", Fields.LDS_ENGINE_STATE_INDEX,
|
||||
EngineState.VALUES, TpsMeta.CONTENT));
|
||||
EngineState.VALUES, TpsMeta.TPS_SECTION));
|
||||
|
||||
liveDocs.add(getPanel("tCharge", "", Fields.LDS_ENGINE_STATE_INDEX,
|
||||
EngineState.VALUES, SpeedDensityMeta.CONTENT));
|
||||
|
|
Binary file not shown.
|
@ -14,6 +14,7 @@ import java.util.stream.Stream;
|
|||
import static com.rusefi.ConfigDefinition.EOL;
|
||||
|
||||
public class LiveDocsMetaParser {
|
||||
private static final String DISPLAY_TAG = "DISPLAY_TAG";
|
||||
private static final String DISPLAY_CONFIG = "DISPLAY_CONFIG";
|
||||
private static final String DISPLAY_FIELD = "DISPLAY_FIELD";
|
||||
private static final String DISPLAY_TEXT = "DISPLAY_TEXT";
|
||||
|
@ -34,49 +35,62 @@ public class LiveDocsMetaParser {
|
|||
throw new IllegalArgumentException("Two arguments expected but " + Arrays.toString(args));
|
||||
String fileName = args[0];
|
||||
String destinationPath = args[1];
|
||||
SystemOut.println(fileName);
|
||||
if (!new File(fileName).exists())
|
||||
throw new IllegalStateException("Not found " + fileName);
|
||||
String content = readLineByLine(fileName);
|
||||
List<Request> r = parse(content);
|
||||
SystemOut.println(r);
|
||||
MetaInfo metaInfo = getMetaFromFile(fileName);
|
||||
SystemOut.println(metaInfo);
|
||||
|
||||
String className = getClassName(fileName);
|
||||
String javaCode = generateJavaCode(r, className);
|
||||
String javaCode = generateJavaCode(metaInfo, className);
|
||||
FileWriter fw = new FileWriter(destinationPath + "java_console/ui/src/com/rusefi/ldmp/generated/" + className + ".java");
|
||||
fw.write(javaCode);
|
||||
fw.close();
|
||||
}
|
||||
|
||||
public static List<Request> parse(String string) {
|
||||
private static MetaInfo getMetaFromFile(String fileName) throws IOException {
|
||||
SystemOut.println(fileName);
|
||||
if (!new File(fileName).exists())
|
||||
throw new IllegalStateException("Not found " + fileName);
|
||||
String content = readLineByLine(fileName);
|
||||
return parse(content);
|
||||
}
|
||||
|
||||
public static MetaInfo parse(String string) {
|
||||
Stack<List<Request>> stack = new Stack<>();
|
||||
|
||||
List<Request> result = new ArrayList<>();
|
||||
|
||||
MetaInfo meta = new MetaInfo();
|
||||
|
||||
List<Request> result = meta.start("CONTENT");
|
||||
string = string.replaceAll("[()>.]", " ");
|
||||
SystemOut.println(string);
|
||||
Scanner s = new Scanner(string);
|
||||
while (s.hasNext()) {
|
||||
String token = s.next();
|
||||
//SystemOut.println(token);
|
||||
if (DISPLAY_CONFIG.equals(token)) {
|
||||
if (DISPLAY_CONFIG.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
String config = s.next();
|
||||
SystemOut.println("REQ CONF " + config);
|
||||
result.add(new ConfigRequest(config));
|
||||
}
|
||||
} else if (DISPLAY_TEXT.equals(token)) {
|
||||
} else if (DISPLAY_TEXT.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
String config = s.next();
|
||||
SystemOut.println("REQ TEXT " + config);
|
||||
result.add(new TextRequest(config));
|
||||
}
|
||||
} else if (DISPLAY_FIELD.equals(token)) {
|
||||
} else if (DISPLAY_TAG.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
String tag = s.next();
|
||||
SystemOut.println("REQ TAG " + tag);
|
||||
result = meta.start(tag);
|
||||
}
|
||||
} else if (DISPLAY_FIELD.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
String config = s.next();
|
||||
SystemOut.println("REQ FIELD " + config);
|
||||
result.add(new FieldRequest(config));
|
||||
}
|
||||
} else if (DISPLAY_IF.equals(token)) {
|
||||
} else if (DISPLAY_IF.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
stack.push(result);
|
||||
|
||||
|
@ -90,7 +104,7 @@ public class LiveDocsMetaParser {
|
|||
|
||||
result = ifRequest.trueBlock;
|
||||
}
|
||||
} else if (DISPLAY_ELSE.equals(token)) {
|
||||
} else if (DISPLAY_ELSE.equalsIgnoreCase(token)) {
|
||||
if (stack.isEmpty())
|
||||
throw new IllegalStateException("No IF statement on stack");
|
||||
List<Request> onStack = stack.peek();
|
||||
|
@ -102,12 +116,12 @@ public class LiveDocsMetaParser {
|
|||
IfRequest ifRequest = (IfRequest) request;
|
||||
|
||||
result = ifRequest.falseBlock;
|
||||
} else if (DISPLAY_ENDIF.equals(token)) {
|
||||
} else if (DISPLAY_ENDIF.equalsIgnoreCase(token)) {
|
||||
if (stack.isEmpty())
|
||||
throw new IllegalStateException("No IF statement on stack");
|
||||
result = stack.pop();
|
||||
|
||||
} else if (DISPLAY_SENSOR.equals(token)) {
|
||||
} else if (DISPLAY_SENSOR.equalsIgnoreCase(token)) {
|
||||
if (s.hasNext()) {
|
||||
String config = s.next();
|
||||
SystemOut.println("REQ SENSOR " + config);
|
||||
|
@ -118,19 +132,28 @@ public class LiveDocsMetaParser {
|
|||
|
||||
if (!stack.isEmpty())
|
||||
throw new IllegalStateException("Unfinished");
|
||||
return result;
|
||||
return meta;
|
||||
}
|
||||
|
||||
public static String generateJavaCode(List<Request> r, String className) {
|
||||
public static String generateJavaCode(MetaInfo r, String className) {
|
||||
StringBuilder java = new StringBuilder("package com.rusefi.ldmp.generated;" + EOL + EOL +
|
||||
"import com.rusefi.ldmp.*;" + EOL + EOL +
|
||||
"public class " + className + " {" + EOL +
|
||||
"\tpublic static final Request[] CONTENT = new Request[]{" + EOL);
|
||||
"public class " + className + " {" + EOL
|
||||
);
|
||||
|
||||
java.append(Request.printList(r));
|
||||
java.append("\t};" + EOL +
|
||||
"}");
|
||||
for (Map.Entry<String, List<Request>> e : r.map.entrySet()) {
|
||||
List<Request> list = e.getValue();
|
||||
if (list.isEmpty())
|
||||
continue;
|
||||
|
||||
java.append("\tpublic static final Request[] " +
|
||||
e.getKey() +
|
||||
" = new Request[]{" + EOL);
|
||||
java.append(Request.printList(list));
|
||||
java.append("\t};" + EOL);
|
||||
}
|
||||
|
||||
java.append("}");
|
||||
return java.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.rusefi.ldmp;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class MetaInfo {
|
||||
public Map<String, List<Request>> map = new TreeMap<>();
|
||||
|
||||
|
||||
public List<Request> start(String content) {
|
||||
map.putIfAbsent(content, new ArrayList<>());
|
||||
return map.get(content);
|
||||
}
|
||||
|
||||
public List<Request> first() {
|
||||
return map.values().iterator().next();
|
||||
}
|
||||
}
|
|
@ -16,34 +16,34 @@ public class LiveDocsMetaParserTest {
|
|||
|
||||
@Test
|
||||
public void parseConfigElements() {
|
||||
List<Request> r = LiveDocsMetaParser.parse("\t\t// TCHARGE_MODE_RPM_TPS\n" +
|
||||
MetaInfo r = LiveDocsMetaParser.parse("\t\t// TCHARGE_MODE_RPM_TPS\n" +
|
||||
"\t\tfloat minRpmKcurrentTPS = interpolateMsg(\"minRpm\", tpMin, DISPLAY_CONFIG(tChargeMinRpmMinTps), tpMax,\n" +
|
||||
"\t\t\t\t/***display*/CONFIG(tChargeMinRpmMaxTps), tps);\n" +
|
||||
"\t\tfloat maxRpmKcurrentTPS = interpolateMsg(\"maxRpm\", tpMin, DISPLAY_CONFIG(tChargeMaxRpmMinTps), tpMax,\n" +
|
||||
"\t\t\t\tDISPLAY_CONFIG(tChargeMaxRpmMaxTps), tps);\n" +
|
||||
"\n" +
|
||||
"\t\tengine->engineState.Tcharge_coff = interpolateMsg(\"Kcurr\", rpmMin, minRpmKcurrentTPS, rpmMax, maxRpmKcurrentTPS, rpm);\n");
|
||||
assertEquals(3, r.size());
|
||||
assertEquals(new ConfigRequest("tChargeMinRpmMinTps"), r.get(0));
|
||||
assertEquals(3, r.first().size());
|
||||
assertEquals(new ConfigRequest("tChargeMinRpmMinTps"), r.first().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseField() {
|
||||
List<Request> r = LiveDocsMetaParser.parse("\tDISPLAY_TEXT(Analog_MCU_reads);\n" +
|
||||
MetaInfo r = LiveDocsMetaParser.parse("\tDISPLAY_TEXT(Analog_MCU_reads);\n" +
|
||||
"\tengine->engineState.DISPLAY_FIELD(currentTpsAdc) = adc;\n" +
|
||||
"\tDISPLAY_TEXT(ADC_which_equals);\n");
|
||||
assertEquals(3, r.size());
|
||||
assertEquals(3, r.first().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseDisplayConfig() {
|
||||
List<Request> r = LiveDocsMetaParser.parse("\t\t// DISPLAY_TEXT(interpolate(\")\n" +
|
||||
MetaInfo r = LiveDocsMetaParser.parse("\t\t// DISPLAY_TEXT(interpolate(\")\n" +
|
||||
"+\t\tDISPLAY_SENSOR(RPM) DISPLAY_TEXT(TCHARGE_MODE_RPM_TPS)\n" +
|
||||
"\t\tfloat minRpmKcurrentTPS = interpolateMsg(\"minRpm\", tpMin, DISPLAY_CONFIG(tChargeMinRpmMinTps), tpMax,\n");
|
||||
assertEquals(4, r.size());
|
||||
assertEquals(4, r.first().size());
|
||||
// implementation has eaten the bracket :(
|
||||
assertEquals(new TextRequest("interpolate"), r.get(0));
|
||||
assertEquals(new SensorRequest("RPM"), r.get(1));
|
||||
assertEquals(new TextRequest("interpolate"), r.first().get(0));
|
||||
assertEquals(new SensorRequest("RPM"), r.first().get(1));
|
||||
|
||||
String javaCode = LiveDocsMetaParser.generateJavaCode(r, "xx");
|
||||
assertEquals("package com.rusefi.ldmp.generated;\n" +
|
||||
|
@ -63,21 +63,24 @@ public class LiveDocsMetaParserTest {
|
|||
|
||||
@Test
|
||||
public void testField() {
|
||||
List<Request> r = LiveDocsMetaParser.parse("tm->DISPLAY_FIELD(voltageMCU) = getVoltage(\"term\", config->adcChannel);");
|
||||
assertEquals(1, r.size());
|
||||
assertEquals(new FieldRequest("voltageMCU"), r.get(0));
|
||||
MetaInfo r = LiveDocsMetaParser.parse("tm->DISPLAY_FIELD(voltageMCU) = getVoltage(\"term\", config->adcChannel);\n" +
|
||||
"DISPLAY_tag(tag) DISPLAY_FIELD(voltageMCU2)");
|
||||
assertEquals(1, r.first().size());
|
||||
assertEquals(new FieldRequest("voltageMCU"), r.first().get(0));
|
||||
|
||||
assertEquals(2, r.map.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseIf() {
|
||||
List<Request> r = LiveDocsMetaParser.parse("\tDISPLAY_IF(cond)\t// DISPLAY_TEXT(\"interpolate(\")\n" +
|
||||
MetaInfo r = LiveDocsMetaParser.parse("\tDisPLAY_IF(cond)\t// DISPLAY_TEXT(\"interpolate(\")\n" +
|
||||
"+\t\tDISPLAY_SENSOR(RPM)" +
|
||||
"/* DISPLAY_ELSE */ DISPLAY_TEXT(\"TCHARGE_MODE_RPM_TPS\")\n" +
|
||||
"/* DISPLAY_ElsE */ DISPLAY_TEXT(\"TCHARGE_MODE_RPM_TPS\")\n" +
|
||||
"\t\tfloat minRpmKcurrentTPS = interpolateMsg(\"minRpm\", tpMin, DISPLAY_CONFIG(tChargeMinRpmMinTps), tpMax,\n" +
|
||||
"/* DISPLAY_ENDIF */");
|
||||
|
||||
assertEquals(1, r.size());
|
||||
IfRequest ifRequest = (IfRequest) r.get(0);
|
||||
assertEquals(1, r.first().size());
|
||||
IfRequest ifRequest = (IfRequest) r.first().get(0);
|
||||
List<Request> trueBlock = ifRequest.trueBlock;
|
||||
assertEquals(2, trueBlock.size());
|
||||
assertEquals(new SensorRequest("RPM"), trueBlock.get(1));
|
||||
|
|
Loading…
Reference in New Issue