diff --git a/firmware/controllers/sensors/auto_generated_sensor.cpp b/firmware/controllers/sensors/auto_generated_sensor.cpp index e01f5ef9af..8c2abd4fba 100644 --- a/firmware/controllers/sensors/auto_generated_sensor.cpp +++ b/firmware/controllers/sensors/auto_generated_sensor.cpp @@ -1,97 +1,97 @@ #include "global.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 const char *getSensorType(SensorType value){ switch(value) { -case AcceleratorPedal: +case SensorType::AcceleratorPedal: return "AcceleratorPedal"; -case AcceleratorPedalPrimary: +case SensorType::AcceleratorPedalPrimary: return "AcceleratorPedalPrimary"; -case AcceleratorPedalSecondary: +case SensorType::AcceleratorPedalSecondary: return "AcceleratorPedalSecondary"; -case Aux1: +case SensorType::Aux1: return "Aux1"; -case Aux2: +case SensorType::Aux2: return "Aux2"; -case Aux3: +case SensorType::Aux3: return "Aux3"; -case Aux4: +case SensorType::Aux4: return "Aux4"; -case Aux5: +case SensorType::Aux5: return "Aux5"; -case Aux6: +case SensorType::Aux6: return "Aux6"; -case Aux7: +case SensorType::Aux7: return "Aux7"; -case Aux8: +case SensorType::Aux8: return "Aux8"; -case AuxTemp1: +case SensorType::AuxTemp1: return "AuxTemp1"; -case AuxTemp2: +case SensorType::AuxTemp2: return "AuxTemp2"; -case BarometricPressure: +case SensorType::BarometricPressure: return "BarometricPressure"; -case BatteryVoltage: +case SensorType::BatteryVoltage: return "BatteryVoltage"; -case Clt: +case SensorType::Clt: return "Clt"; -case DriverThrottleIntent: +case SensorType::DriverThrottleIntent: return "DriverThrottleIntent"; -case FuelEthanolPercent: +case SensorType::FuelEthanolPercent: return "FuelEthanolPercent"; -case FuelLevel: +case SensorType::FuelLevel: return "FuelLevel"; -case FuelPressureHigh: +case SensorType::FuelPressureHigh: return "FuelPressureHigh"; -case FuelPressureInjector: +case SensorType::FuelPressureInjector: return "FuelPressureInjector"; -case FuelPressureLow: +case SensorType::FuelPressureLow: return "FuelPressureLow"; -case Iat: +case SensorType::Iat: return "Iat"; -case IdlePosition: +case SensorType::IdlePosition: return "IdlePosition"; -case Invalid: +case SensorType::Invalid: return "Invalid"; -case Lambda1: +case SensorType::Lambda1: return "Lambda1"; -case Lambda2: +case SensorType::Lambda2: return "Lambda2"; -case Maf: +case SensorType::Maf: return "Maf"; -case Map: +case SensorType::Map: return "Map"; -case MapFast: +case SensorType::MapFast: return "MapFast"; -case MapSlow: +case SensorType::MapSlow: return "MapSlow"; -case OilPressure: +case SensorType::OilPressure: return "OilPressure"; -case PlaceholderLast: +case SensorType::PlaceholderLast: return "PlaceholderLast"; -case Rpm: +case SensorType::Rpm: return "Rpm"; -case Tps1: +case SensorType::Tps1: return "Tps1"; -case Tps1Primary: +case SensorType::Tps1Primary: return "Tps1Primary"; -case Tps1Secondary: +case SensorType::Tps1Secondary: return "Tps1Secondary"; -case Tps2: +case SensorType::Tps2: return "Tps2"; -case Tps2Primary: +case SensorType::Tps2Primary: return "Tps2Primary"; -case Tps2Secondary: +case SensorType::Tps2Secondary: return "Tps2Secondary"; -case TurbochargerSpeed: +case SensorType::TurbochargerSpeed: return "TurbochargerSpeed"; -case VehicleSpeed: +case SensorType::VehicleSpeed: return "VehicleSpeed"; -case WastegatePosition: +case SensorType::WastegatePosition: return "WastegatePosition"; } return NULL; diff --git a/firmware/controllers/sensors/sensor_type.h b/firmware/controllers/sensors/sensor_type.h index 17b0a72fa2..cef185e609 100644 --- a/firmware/controllers/sensors/sensor_type.h +++ b/firmware/controllers/sensors/sensor_type.h @@ -15,7 +15,7 @@ * 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 :( Clt, // 1 Iat, @@ -85,4 +85,4 @@ typedef enum __attribute__ ((__packed__)) { // Leave me at the end! PlaceholderLast, -} SensorType; +}; diff --git a/java_tools/enum2string.jar b/java_tools/enum2string.jar index ff79711da5..d3f6648aec 100644 Binary files a/java_tools/enum2string.jar and b/java_tools/enum2string.jar differ diff --git a/java_tools/enum_to_string/src/main/java/com/rusefi/EnumToString.java b/java_tools/enum_to_string/src/main/java/com/rusefi/EnumToString.java index a15adef6f3..f6d6ab9e9e 100644 --- a/java_tools/enum_to_string/src/main/java/com/rusefi/EnumToString.java +++ b/java_tools/enum_to_string/src/main/java/com/rusefi/EnumToString.java @@ -83,23 +83,29 @@ public class EnumToString { public EnumToString outputData(EnumsReader enumsReader) { SystemOut.println("Preparing output for " + enumsReader.getEnums().size() + " enums\n"); - for (Map.Entry> e : enumsReader.getEnums().entrySet()) { + for (Map.Entry e : enumsReader.getEnums().entrySet()) { 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"); } SystemOut.println("EnumToString: " + headerFileContent.length() + " bytes of content\n"); return this; } - private static String makeCode(String enumName, Collection values) { + private static String makeCode(String enumName, EnumsReader.EnumState enumState) { StringBuilder sb = new StringBuilder(); + Collection values = enumState.values.values(); sb.append(getMethodSignature(enumName) + "{\n"); sb.append("switch(value) {\n"); 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"); } diff --git a/java_tools/enum_to_string/src/main/java/com/rusefi/EnumsReader.java b/java_tools/enum_to_string/src/main/java/com/rusefi/EnumsReader.java index 4e8a97bb31..d862eae1b3 100644 --- a/java_tools/enum_to_string/src/main/java/com/rusefi/EnumsReader.java +++ b/java_tools/enum_to_string/src/main/java/com/rusefi/EnumsReader.java @@ -9,9 +9,10 @@ import java.io.*; import java.util.*; public class EnumsReader { + public static final String ENUMCLASS_PREFIX = "enumclass"; private final Map currentValues = new TreeMap<>(); - private final Map> enums = new TreeMap<>(); + private final Map enums = new TreeMap<>(); @NotNull static List getSortedByOrder(Map brain_pin_e) { @@ -20,7 +21,7 @@ public class EnumsReader { return new ArrayList<>(byOrdinal); } - public Map> getEnums() { + public Map getEnums() { return enums; } @@ -32,6 +33,8 @@ public class EnumsReader { boolean isInsideEnum = false; BufferedReader reader = new BufferedReader(in); String line; + String enumName = null; + boolean isEnumClass = false; boolean withAutoValue = false; @@ -40,18 +43,31 @@ public class EnumsReader { line = line.replaceAll("//.+", ""); if (line.startsWith("typedefenum{") || line.startsWith("typedefenum__attribute__")) { - SystemOut.println(" EnumsReader: Entering enum"); + SystemOut.println(" EnumsReader: Entering legacy enum"); currentValues.clear(); withAutoValue = false; 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(";")) { isInsideEnum = false; - line = line.substring(1, line.length() - 1); - SystemOut.println(" EnumsReader: Ending enum " + line + " found " + currentValues.size() + " values"); + if (enumName == null) + enumName = line.substring(1, line.length() - 1); + SystemOut.println(" EnumsReader: Ending enum " + enumName + " found " + currentValues.size() + " values"); if (withAutoValue) validateValues(currentValues); - enums.put(line, new TreeMap<>(currentValues)); + enums.put(enumName, new EnumState(currentValues, enumName, isEnumClass)); } else { if (isInsideEnum) { if (isKeyValueLine(line)) { @@ -91,4 +107,16 @@ public class EnumsReader { 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_])*))*,?"); } + + public static class EnumState { + public Map values; + public final String enumName; + public final boolean isEnumClass; + + public EnumState(Map currentValues, String enumName, boolean isEnumClass) { + values = new TreeMap<>(currentValues); + this.enumName = enumName; + this.isEnumClass = isEnumClass; + } + } } diff --git a/java_tools/enum_to_string/src/main/java/com/rusefi/ToJavaEnum.java b/java_tools/enum_to_string/src/main/java/com/rusefi/ToJavaEnum.java index 0d68593a32..ade58e73dc 100644 --- a/java_tools/enum_to_string/src/main/java/com/rusefi/ToJavaEnum.java +++ b/java_tools/enum_to_string/src/main/java/com/rusefi/ToJavaEnum.java @@ -25,7 +25,7 @@ public class ToJavaEnum { enumsReader.read(new FileReader(f)); } - for (Map.Entry> e : enumsReader.getEnums().entrySet()) { + for (Map.Entry e : enumsReader.getEnums().entrySet()) { String java = generate(e.getKey(), e.getValue()); String fullFileName = outputPath + File.separator + e.getKey() + ".java"; @@ -35,12 +35,12 @@ public class ToJavaEnum { } } - public static String generate(String key, Map values) { + public static String generate(String key, EnumsReader.EnumState enumState) { StringBuilder sb = new StringBuilder("package com.rusefi.enums;\n"); sb.append("//auto-generated by ToJavaEnum.java\n\n\n\n"); sb.append("public enum " + key + " {\n"); - List sorted = EnumsReader.getSortedByOrder(values); + List sorted = EnumsReader.getSortedByOrder(enumState.values); for (Value value : sorted) { sb.append("\t" + value.getName() + ",\n"); diff --git a/java_tools/enum_to_string/src/test/java/com/rusefi/EnumToStringTest.java b/java_tools/enum_to_string/src/test/java/com/rusefi/EnumToStringTest.java index 80edfe5236..9b890872c3 100644 --- a/java_tools/enum_to_string/src/test/java/com/rusefi/EnumToStringTest.java +++ b/java_tools/enum_to_string/src/test/java/com/rusefi/EnumToStringTest.java @@ -43,7 +43,7 @@ public class EnumToStringTest { EnumsReader enumsReader = new EnumsReader().read(reader); EnumToString enumToString = process(enumsReader); - List values = new ArrayList<>(enumsReader.getEnums().get("brain_pin_e").values()); + List values = new ArrayList<>(enumsReader.getEnums().get("brain_pin_e").values.values()); assertEquals(3, values.size()); Value first = values.get(0); assertEquals("GPIO_HEX", first.getName()); @@ -86,6 +86,26 @@ public class EnumToStringTest { "}\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 public void parseEnumWithoutExplicitValues() throws IOException { final StringReader reader = new StringReader( @@ -95,13 +115,13 @@ public class EnumToStringTest { "\tGPIO_HEX,\n" + "}brain_pin_e; // hello"); EnumsReader enumsReader = new EnumsReader().read(reader); - Map brain_pin_e = enumsReader.getEnums().get("brain_pin_e"); - assertEquals(2, brain_pin_e.get("GPIO_HEX").getIntValue()); + EnumsReader.EnumState brain_pin_e = enumsReader.getEnums().get("brain_pin_e"); + assertEquals(2, brain_pin_e.values.get("GPIO_HEX").getIntValue()); - List listByOrdinal = EnumsReader.getSortedByOrder(brain_pin_e); + List listByOrdinal = EnumsReader.getSortedByOrder(brain_pin_e.values); assertEquals(0, listByOrdinal.get(0).getIntValue()); - for (Map.Entry> e : enumsReader.getEnums().entrySet()) { + for (Map.Entry e : enumsReader.getEnums().entrySet()) { String a = new ToJavaEnum().generate(e.getKey(), e.getValue()); @@ -117,4 +137,45 @@ public class EnumToStringTest { "}\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()); + } }