enum reader progress

This commit is contained in:
rusefillc 2021-10-20 21:42:09 -04:00
parent d0f033cd9b
commit ffc4e97ba5
7 changed files with 159 additions and 64 deletions

View File

@ -1,97 +1,97 @@
#include "global.h" #include "global.h"
#include "sensor_type.h" #include "sensor_type.h"
// was generated automatically by rusEFI tool from sensor_type.h // by enum2string.jar tool on Tue Oct 19 23:51:48 UTC 2021 // was generated automatically by rusEFI tool from sensor_type.h // by enum2string.jar tool on Wed Oct 20 21:25:07 EDT 2021
// see also gen_config_and_enums.bat // see also gen_config_and_enums.bat
const char *getSensorType(SensorType value){ const char *getSensorType(SensorType value){
switch(value) { switch(value) {
case AcceleratorPedal: case SensorType::AcceleratorPedal:
return "AcceleratorPedal"; return "AcceleratorPedal";
case AcceleratorPedalPrimary: case SensorType::AcceleratorPedalPrimary:
return "AcceleratorPedalPrimary"; return "AcceleratorPedalPrimary";
case AcceleratorPedalSecondary: case SensorType::AcceleratorPedalSecondary:
return "AcceleratorPedalSecondary"; return "AcceleratorPedalSecondary";
case Aux1: case SensorType::Aux1:
return "Aux1"; return "Aux1";
case Aux2: case SensorType::Aux2:
return "Aux2"; return "Aux2";
case Aux3: case SensorType::Aux3:
return "Aux3"; return "Aux3";
case Aux4: case SensorType::Aux4:
return "Aux4"; return "Aux4";
case Aux5: case SensorType::Aux5:
return "Aux5"; return "Aux5";
case Aux6: case SensorType::Aux6:
return "Aux6"; return "Aux6";
case Aux7: case SensorType::Aux7:
return "Aux7"; return "Aux7";
case Aux8: case SensorType::Aux8:
return "Aux8"; return "Aux8";
case AuxTemp1: case SensorType::AuxTemp1:
return "AuxTemp1"; return "AuxTemp1";
case AuxTemp2: case SensorType::AuxTemp2:
return "AuxTemp2"; return "AuxTemp2";
case BarometricPressure: case SensorType::BarometricPressure:
return "BarometricPressure"; return "BarometricPressure";
case BatteryVoltage: case SensorType::BatteryVoltage:
return "BatteryVoltage"; return "BatteryVoltage";
case Clt: case SensorType::Clt:
return "Clt"; return "Clt";
case DriverThrottleIntent: case SensorType::DriverThrottleIntent:
return "DriverThrottleIntent"; return "DriverThrottleIntent";
case FuelEthanolPercent: case SensorType::FuelEthanolPercent:
return "FuelEthanolPercent"; return "FuelEthanolPercent";
case FuelLevel: case SensorType::FuelLevel:
return "FuelLevel"; return "FuelLevel";
case FuelPressureHigh: case SensorType::FuelPressureHigh:
return "FuelPressureHigh"; return "FuelPressureHigh";
case FuelPressureInjector: case SensorType::FuelPressureInjector:
return "FuelPressureInjector"; return "FuelPressureInjector";
case FuelPressureLow: case SensorType::FuelPressureLow:
return "FuelPressureLow"; return "FuelPressureLow";
case Iat: case SensorType::Iat:
return "Iat"; return "Iat";
case IdlePosition: case SensorType::IdlePosition:
return "IdlePosition"; return "IdlePosition";
case Invalid: case SensorType::Invalid:
return "Invalid"; return "Invalid";
case Lambda1: case SensorType::Lambda1:
return "Lambda1"; return "Lambda1";
case Lambda2: case SensorType::Lambda2:
return "Lambda2"; return "Lambda2";
case Maf: case SensorType::Maf:
return "Maf"; return "Maf";
case Map: case SensorType::Map:
return "Map"; return "Map";
case MapFast: case SensorType::MapFast:
return "MapFast"; return "MapFast";
case MapSlow: case SensorType::MapSlow:
return "MapSlow"; return "MapSlow";
case OilPressure: case SensorType::OilPressure:
return "OilPressure"; return "OilPressure";
case PlaceholderLast: case SensorType::PlaceholderLast:
return "PlaceholderLast"; return "PlaceholderLast";
case Rpm: case SensorType::Rpm:
return "Rpm"; return "Rpm";
case Tps1: case SensorType::Tps1:
return "Tps1"; return "Tps1";
case Tps1Primary: case SensorType::Tps1Primary:
return "Tps1Primary"; return "Tps1Primary";
case Tps1Secondary: case SensorType::Tps1Secondary:
return "Tps1Secondary"; return "Tps1Secondary";
case Tps2: case SensorType::Tps2:
return "Tps2"; return "Tps2";
case Tps2Primary: case SensorType::Tps2Primary:
return "Tps2Primary"; return "Tps2Primary";
case Tps2Secondary: case SensorType::Tps2Secondary:
return "Tps2Secondary"; return "Tps2Secondary";
case TurbochargerSpeed: case SensorType::TurbochargerSpeed:
return "TurbochargerSpeed"; return "TurbochargerSpeed";
case VehicleSpeed: case SensorType::VehicleSpeed:
return "VehicleSpeed"; return "VehicleSpeed";
case WastegatePosition: case SensorType::WastegatePosition:
return "WastegatePosition"; return "WastegatePosition";
} }
return NULL; return NULL;

View File

@ -15,7 +15,7 @@
* SEE sensor.h ON HOW TO ADD NEW SENSOR TYPES * SEE sensor.h ON HOW TO ADD NEW SENSOR TYPES
************************************** **************************************
*/ */
typedef enum __attribute__ ((__packed__)) { enum class SensorType : unsigned char {
Invalid, // we need indeces for Lua consumers. At the moment we still do not expose constants into Lua :( Invalid, // we need indeces for Lua consumers. At the moment we still do not expose constants into Lua :(
Clt, // 1 Clt, // 1
Iat, Iat,
@ -85,4 +85,4 @@ typedef enum __attribute__ ((__packed__)) {
// Leave me at the end! // Leave me at the end!
PlaceholderLast, PlaceholderLast,
} SensorType; };

Binary file not shown.

View File

@ -83,23 +83,29 @@ public class EnumToString {
public EnumToString outputData(EnumsReader enumsReader) { public EnumToString outputData(EnumsReader enumsReader) {
SystemOut.println("Preparing output for " + enumsReader.getEnums().size() + " enums\n"); SystemOut.println("Preparing output for " + enumsReader.getEnums().size() + " enums\n");
for (Map.Entry<String, Map<String, Value>> e : enumsReader.getEnums().entrySet()) { for (Map.Entry<String, EnumsReader.EnumState> e : enumsReader.getEnums().entrySet()) {
String enumName = e.getKey(); String enumName = e.getKey();
cppFileContent.append(makeCode(enumName, e.getValue().values())); EnumsReader.EnumState enumState = e.getValue();
cppFileContent.append(makeCode(enumName, enumState));
headerFileContent.append(getMethodSignature(enumName) + ";\n"); headerFileContent.append(getMethodSignature(enumName) + ";\n");
} }
SystemOut.println("EnumToString: " + headerFileContent.length() + " bytes of content\n"); SystemOut.println("EnumToString: " + headerFileContent.length() + " bytes of content\n");
return this; return this;
} }
private static String makeCode(String enumName, Collection<Value> values) { private static String makeCode(String enumName, EnumsReader.EnumState enumState) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Collection<Value> values = enumState.values.values();
sb.append(getMethodSignature(enumName) + "{\n"); sb.append(getMethodSignature(enumName) + "{\n");
sb.append("switch(value) {\n"); sb.append("switch(value) {\n");
for (Value e : values) { for (Value e : values) {
sb.append("case " + e.getName() + ":\n"); sb.append("case ");
if (enumState.isEnumClass) {
sb.append(enumState.enumName).append("::");
}
sb.append(e.getName() + ":\n");
sb.append(" return \"" + e.getName() + "\";\n"); sb.append(" return \"" + e.getName() + "\";\n");
} }

View File

@ -9,9 +9,10 @@ import java.io.*;
import java.util.*; import java.util.*;
public class EnumsReader { public class EnumsReader {
public static final String ENUMCLASS_PREFIX = "enumclass";
private final Map<String, Value> currentValues = new TreeMap<>(); private final Map<String, Value> currentValues = new TreeMap<>();
private final Map<String, Map<String, Value>> enums = new TreeMap<>(); private final Map<String, EnumState> enums = new TreeMap<>();
@NotNull @NotNull
static List<Value> getSortedByOrder(Map<String, Value> brain_pin_e) { static List<Value> getSortedByOrder(Map<String, Value> brain_pin_e) {
@ -20,7 +21,7 @@ public class EnumsReader {
return new ArrayList<>(byOrdinal); return new ArrayList<>(byOrdinal);
} }
public Map<String /*enum name*/, Map<String/*enum member*/, Value>> getEnums() { public Map<String /*enum name*/, EnumState> getEnums() {
return enums; return enums;
} }
@ -32,6 +33,8 @@ public class EnumsReader {
boolean isInsideEnum = false; boolean isInsideEnum = false;
BufferedReader reader = new BufferedReader(in); BufferedReader reader = new BufferedReader(in);
String line; String line;
String enumName = null;
boolean isEnumClass = false;
boolean withAutoValue = false; boolean withAutoValue = false;
@ -40,18 +43,31 @@ public class EnumsReader {
line = line.replaceAll("//.+", ""); line = line.replaceAll("//.+", "");
if (line.startsWith("typedefenum{") || line.startsWith("typedefenum__attribute__")) { if (line.startsWith("typedefenum{") || line.startsWith("typedefenum__attribute__")) {
SystemOut.println(" EnumsReader: Entering enum"); SystemOut.println(" EnumsReader: Entering legacy enum");
currentValues.clear(); currentValues.clear();
withAutoValue = false; withAutoValue = false;
isInsideEnum = true; isInsideEnum = true;
enumName = null;
isEnumClass = false;
} else if (line.startsWith(ENUMCLASS_PREFIX)) {
SystemOut.println(" EnumsReader: Entering fancy enum class");
currentValues.clear();
withAutoValue = false;
isInsideEnum = true;
isEnumClass = true;
int colonIndex = line.indexOf(":");
if (colonIndex == -1)
throw new IllegalStateException("color and Type not located in " + line);
enumName = line.substring(ENUMCLASS_PREFIX.length(), colonIndex);
} else if (line.startsWith("}") && line.endsWith(";")) { } else if (line.startsWith("}") && line.endsWith(";")) {
isInsideEnum = false; isInsideEnum = false;
line = line.substring(1, line.length() - 1); if (enumName == null)
SystemOut.println(" EnumsReader: Ending enum " + line + " found " + currentValues.size() + " values"); enumName = line.substring(1, line.length() - 1);
SystemOut.println(" EnumsReader: Ending enum " + enumName + " found " + currentValues.size() + " values");
if (withAutoValue) if (withAutoValue)
validateValues(currentValues); validateValues(currentValues);
enums.put(line, new TreeMap<>(currentValues)); enums.put(enumName, new EnumState(currentValues, enumName, isEnumClass));
} else { } else {
if (isInsideEnum) { if (isInsideEnum) {
if (isKeyValueLine(line)) { if (isKeyValueLine(line)) {
@ -91,4 +107,16 @@ public class EnumsReader {
static boolean isKeyValueLine(String line) { static boolean isKeyValueLine(String line) {
return removeSpaces(line).matches("[a-zA-Z_$][a-zA-Z\\d_$]*(=(0x[0-9a-fA-F]+|(-)?[0-9]+|([-a-zA-Z\\d_])*))*,?"); return removeSpaces(line).matches("[a-zA-Z_$][a-zA-Z\\d_$]*(=(0x[0-9a-fA-F]+|(-)?[0-9]+|([-a-zA-Z\\d_])*))*,?");
} }
public static class EnumState {
public Map<String, Value> values;
public final String enumName;
public final boolean isEnumClass;
public EnumState(Map<String, Value> currentValues, String enumName, boolean isEnumClass) {
values = new TreeMap<>(currentValues);
this.enumName = enumName;
this.isEnumClass = isEnumClass;
}
}
} }

View File

@ -25,7 +25,7 @@ public class ToJavaEnum {
enumsReader.read(new FileReader(f)); enumsReader.read(new FileReader(f));
} }
for (Map.Entry<String /*enum name*/, Map<String/*enum member*/, Value>> e : enumsReader.getEnums().entrySet()) { for (Map.Entry<String /*enum name*/, EnumsReader.EnumState> e : enumsReader.getEnums().entrySet()) {
String java = generate(e.getKey(), e.getValue()); String java = generate(e.getKey(), e.getValue());
String fullFileName = outputPath + File.separator + e.getKey() + ".java"; String fullFileName = outputPath + File.separator + e.getKey() + ".java";
@ -35,12 +35,12 @@ public class ToJavaEnum {
} }
} }
public static String generate(String key, Map<String, Value> values) { public static String generate(String key, EnumsReader.EnumState enumState) {
StringBuilder sb = new StringBuilder("package com.rusefi.enums;\n"); StringBuilder sb = new StringBuilder("package com.rusefi.enums;\n");
sb.append("//auto-generated by ToJavaEnum.java\n\n\n\n"); sb.append("//auto-generated by ToJavaEnum.java\n\n\n\n");
sb.append("public enum " + key + " {\n"); sb.append("public enum " + key + " {\n");
List<Value> sorted = EnumsReader.getSortedByOrder(values); List<Value> sorted = EnumsReader.getSortedByOrder(enumState.values);
for (Value value : sorted) { for (Value value : sorted) {
sb.append("\t" + value.getName() + ",\n"); sb.append("\t" + value.getName() + ",\n");

View File

@ -43,7 +43,7 @@ public class EnumToStringTest {
EnumsReader enumsReader = new EnumsReader().read(reader); EnumsReader enumsReader = new EnumsReader().read(reader);
EnumToString enumToString = process(enumsReader); EnumToString enumToString = process(enumsReader);
List<Value> values = new ArrayList<>(enumsReader.getEnums().get("brain_pin_e").values()); List<Value> values = new ArrayList<>(enumsReader.getEnums().get("brain_pin_e").values.values());
assertEquals(3, values.size()); assertEquals(3, values.size());
Value first = values.get(0); Value first = values.get(0);
assertEquals("GPIO_HEX", first.getName()); assertEquals("GPIO_HEX", first.getName());
@ -86,6 +86,26 @@ public class EnumToStringTest {
"}\n", enumToString.getCppFileContent()); "}\n", enumToString.getCppFileContent());
} }
@Test
public void parsePackedFancyEnum() throws IOException {
final StringReader reader = new StringReader(
"enum class myEnum : uint8_t {\n" +
"\tGPIO_UNASSIGNED = 0,\n" +
"\tGPIO_INVALID = 1,\n" +
"} brain_pin_e ;");
EnumsReader enumsReader = new EnumsReader().read(reader);
EnumToString enumToString = process(enumsReader);
assertEquals("const char *getMyEnum(myEnum value){\n" +
"switch(value) {\n" +
"case myEnum::GPIO_INVALID:\n" +
" return \"GPIO_INVALID\";\n" +
"case myEnum::GPIO_UNASSIGNED:\n" +
" return \"GPIO_UNASSIGNED\";\n" +
" }\n" +
" return NULL;\n" +
"}\n", enumToString.getCppFileContent());
}
@Test @Test
public void parseEnumWithoutExplicitValues() throws IOException { public void parseEnumWithoutExplicitValues() throws IOException {
final StringReader reader = new StringReader( final StringReader reader = new StringReader(
@ -95,13 +115,13 @@ public class EnumToStringTest {
"\tGPIO_HEX,\n" + "\tGPIO_HEX,\n" +
"}brain_pin_e; // hello"); "}brain_pin_e; // hello");
EnumsReader enumsReader = new EnumsReader().read(reader); EnumsReader enumsReader = new EnumsReader().read(reader);
Map<String, Value> brain_pin_e = enumsReader.getEnums().get("brain_pin_e"); EnumsReader.EnumState brain_pin_e = enumsReader.getEnums().get("brain_pin_e");
assertEquals(2, brain_pin_e.get("GPIO_HEX").getIntValue()); assertEquals(2, brain_pin_e.values.get("GPIO_HEX").getIntValue());
List<Value> listByOrdinal = EnumsReader.getSortedByOrder(brain_pin_e); List<Value> listByOrdinal = EnumsReader.getSortedByOrder(brain_pin_e.values);
assertEquals(0, listByOrdinal.get(0).getIntValue()); assertEquals(0, listByOrdinal.get(0).getIntValue());
for (Map.Entry<String /*enum name*/, Map<String/*enum member*/, Value>> e : enumsReader.getEnums().entrySet()) { for (Map.Entry<String /*enum name*/, EnumsReader.EnumState> e : enumsReader.getEnums().entrySet()) {
String a = new ToJavaEnum().generate(e.getKey(), e.getValue()); String a = new ToJavaEnum().generate(e.getKey(), e.getValue());
@ -117,4 +137,45 @@ public class EnumToStringTest {
"}\n", a); "}\n", a);
} }
} }
@Test
public void testTwoEnums() throws IOException {
final StringReader reader = new StringReader(
"typedef enum {\n" +
" AUTOTUNER_OFF = 0,\n" +
" STEADY_STATE_AT_BASELINE = 1,\n" +
"} PidAutoTune_AutoTunerState;\n" +
"\n" +
"typedef enum {\n" +
"\tINIT = 0,\n" +
"\tTPS_THRESHOLD = 1,\n" +
"\t/**\n" +
"\t * Live Docs reads 4 byte value so we want 4 byte enum\n" +
"\t */\n" +
"\tForce_4bytes_size_idle_state_e = ENUM_32_BITS,\n" +
"} idle_state_e;\n");
EnumsReader enumsReader = new EnumsReader().read(reader);
EnumToString enumToString = process(enumsReader);
assertEquals("const char *getPidAutoTune_AutoTunerState(PidAutoTune_AutoTunerState value){\n" +
"switch(value) {\n" +
"case AUTOTUNER_OFF:\n" +
" return \"AUTOTUNER_OFF\";\n" +
"case STEADY_STATE_AT_BASELINE:\n" +
" return \"STEADY_STATE_AT_BASELINE\";\n" +
" }\n" +
" return NULL;\n" +
"}\n" +
"const char *getIdle_state_e(idle_state_e value){\n" +
"switch(value) {\n" +
"case Force_4bytes_size_idle_state_e:\n" +
" return \"Force_4bytes_size_idle_state_e\";\n" +
"case INIT:\n" +
" return \"INIT\";\n" +
"case TPS_THRESHOLD:\n" +
" return \"TPS_THRESHOLD\";\n" +
" }\n" +
" return NULL;\n" +
"}\n", enumToString.getCppFileContent());
}
} }