diff --git a/firmware/controllers/algo/live_data_ids.h b/firmware/controllers/algo/live_data_ids.h new file mode 100644 index 0000000000..c55461a201 --- /dev/null +++ b/firmware/controllers/algo/live_data_ids.h @@ -0,0 +1,22 @@ +/** + * @file live_data_ids.h + * + * Live Documentation Structure + * these indexes are used by custom protocol command TS_GET_STRUCT + */ + +#pragma once + +typedef enum { +LDS_SPEED_DENSITY, +LDS_ENGINE, +LDS_FUEL_TRIM, +LDS_TPS_TPS_ENRICHMENT, +LDS_TRIGGER_CENTRAL, +LDS_ETB_PID, +LDS_IDLE_PID, +LDS_ALTERNATOR_PID, +LDS_CJ125_PID, +LDS_TRIGGER_STATE, +LDS_AC_CONTROL +} live_data_e; \ No newline at end of file diff --git a/firmware/controllers/algo/rusefi_enums.h b/firmware/controllers/algo/rusefi_enums.h index 5e9c70d337..d9d1ba9d36 100644 --- a/firmware/controllers/algo/rusefi_enums.h +++ b/firmware/controllers/algo/rusefi_enums.h @@ -12,6 +12,7 @@ #include "efifeatures.h" #include "obd_error_codes.h" +#include "live_data_ids.h" #include "rusefi_generated.h" // we do not want to start the search for header from current folder so we use brackets here // https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename diff --git a/firmware/gen_enum_to_string.sh b/firmware/gen_enum_to_string.sh index 0b8921d420..a53adf55de 100755 --- a/firmware/gen_enum_to_string.sh +++ b/firmware/gen_enum_to_string.sh @@ -4,6 +4,8 @@ echo "This batch files reads rusefi_enums.h and produces auto_generated_enums.* rm gen_enum_to_string.log +java -DSystemOut.name=gen_java_enum -cp ../java_tools/enum2string.jar com.rusefi.ToJavaEnum -enumInputFile controllers/algo/live_data_ids.h -outputPath ../java_console/ui/src/main/java/com/rusefi/enums + java -DSystemOut.name=gen_enum_to_string \ -jar ../java_tools/enum2string.jar \ -outputPath controllers/algo \ diff --git a/java_console/ui/src/main/java/com/rusefi/enums/live_data_e.java b/java_console/ui/src/main/java/com/rusefi/enums/live_data_e.java new file mode 100644 index 0000000000..fbc378b883 --- /dev/null +++ b/java_console/ui/src/main/java/com/rusefi/enums/live_data_e.java @@ -0,0 +1,18 @@ +package com.rusefi.enums; +//auto-generated by ToJavaEnum.java + + + +public enum live_data_e { + LDS_SPEED_DENSITY, + LDS_ENGINE, + LDS_FUEL_TRIM, + LDS_TPS_TPS_ENRICHMENT, + LDS_TRIGGER_CENTRAL, + LDS_ETB_PID, + LDS_IDLE_PID, + LDS_ALTERNATOR_PID, + LDS_CJ125_PID, + LDS_TRIGGER_STATE, + LDS_AC_CONTROL, +} diff --git a/java_tools/enum2string.jar b/java_tools/enum2string.jar index cd84b188df..ff79711da5 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 958c1cf505..a15adef6f3 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 @@ -26,37 +26,17 @@ public class EnumToString { private final static StringBuilder headerFileContent = new StringBuilder(); - private final static String KEY_INPUT_PATH = "-enumInputPath"; public final static String KEY_ENUM_INPUT_FILE = "-enumInputFile"; - private final static String KEY_OUTPUT = "-outputPath"; - private final static String KEY_OUTPUT_FILE = "-generatedFile"; - private static String fileSuffix = "enums"; public static void main(String[] args) throws IOException { - if (args.length < 4) { - SystemOut.println("Please specify at least\n\n" + - KEY_ENUM_INPUT_FILE + "XXX\n" + - KEY_OUTPUT + "XXX\n" - ); - return; - } + InvokeReader invokeReader = new InvokeReader(args).invoke(); + String outputPath = invokeReader.getOutputPath(); - String inputPath = "."; - String outputPath = null; EnumsReader enumsReader = new EnumsReader(); EnumToString state = new EnumToString(); - for (int i = 0; i < args.length - 1; i += 2) { - String key = args[i]; - if (key.equals(KEY_INPUT_PATH)) { - inputPath = Objects.requireNonNull(args[i + 1], KEY_INPUT_PATH); - } else if (key.equals(KEY_ENUM_INPUT_FILE)) { - String headerInputFile = args[i + 1]; - state.consumeFile(enumsReader, inputPath, headerInputFile); - } else if (key.equals(KEY_OUTPUT_FILE)) { - fileSuffix = args[i + 1]; - } else if (key.equals(KEY_OUTPUT)) { - outputPath = args[i + 1]; - } + + for (String inputFile : invokeReader.getInputFiles()) { + state.consumeFile(enumsReader, invokeReader.getInputPath(), inputFile); } headerFileContent.append("#pragma once\n"); @@ -75,7 +55,7 @@ public class EnumToString { new File(outputPath).mkdirs(); state.writeCppAndHeaderFiles(outputPath + File.separator + "auto_generated_" + - fileSuffix); + InvokeReader.fileSuffix); SystemOut.close(); } @@ -89,15 +69,14 @@ public class EnumToString { bw.close(); } - private void consumeFile(EnumsReader enumsReader, String inputPath, String headerInputFileName) throws IOException { + public void consumeFile(EnumsReader enumsReader, String inputPath, String headerInputFileName) throws IOException { Objects.requireNonNull(inputPath, "inputPath"); File f = new File(inputPath + File.separator + headerInputFileName); SystemOut.println("Reading enums from " + headerInputFileName); - String simpleFileName = f.getName(); - bothFilesHeader.insert(0, "// " + LazyFile.LAZY_FILE_TAG + " from " + simpleFileName + " "); + bothFilesHeader.insert(0, "// " + LazyFile.LAZY_FILE_TAG + " from " + f.getName() + " "); - includesSection.append("#include \"" + simpleFileName + "\"\n"); + includesSection.append("#include \"" + f.getName() + "\"\n"); enumsReader.read(new FileReader(f)); } @@ -142,4 +121,5 @@ public class EnumToString { public String getCppFileContent() { return cppFileContent.toString(); } + } 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 dff1541bab..4e8a97bb31 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 @@ -3,15 +3,23 @@ package com.rusefi; import com.rusefi.enum_reader.Value; import com.rusefi.util.SystemOut; +import org.jetbrains.annotations.NotNull; + import java.io.*; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; public class EnumsReader { private final Map currentValues = new TreeMap<>(); private final Map> enums = new TreeMap<>(); + @NotNull + static List getSortedByOrder(Map brain_pin_e) { + Set byOrdinal = new TreeSet<>(Comparator.comparingInt(Value::getIntValue)); + byOrdinal.addAll(brain_pin_e.values()); + return new ArrayList<>(byOrdinal); + } + public Map> getEnums() { return enums; } @@ -24,6 +32,9 @@ public class EnumsReader { boolean isInsideEnum = false; BufferedReader reader = new BufferedReader(in); String line; + + boolean withAutoValue = false; + while ((line = reader.readLine()) != null) { line = removeSpaces(line); @@ -31,11 +42,15 @@ public class EnumsReader { if (line.startsWith("typedefenum{") || line.startsWith("typedefenum__attribute__")) { SystemOut.println(" EnumsReader: Entering enum"); currentValues.clear(); + withAutoValue = false; isInsideEnum = true; } 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"); + SystemOut.println(" EnumsReader: Ending enum " + line + " found " + currentValues.size() + " values"); + if (withAutoValue) + validateValues(currentValues); + enums.put(line, new TreeMap<>(currentValues)); } else { if (isInsideEnum) { @@ -46,6 +61,9 @@ public class EnumsReader { if (index != -1) { value = line.substring(index + 1); line = line.substring(0, index); + } else { + value = Integer.toString(currentValues.size()); + withAutoValue = true; } SystemOut.println(" EnumsReader: Line " + line); currentValues.put(line, new Value(line, value)); @@ -58,6 +76,14 @@ public class EnumsReader { return this; } + private void validateValues(Map currentValues) { + for (Map.Entry entry : currentValues.entrySet()) { + int v = entry.getValue().getIntValue(); + if (v < 0 || v >= currentValues.size()) + throw new IllegalStateException("Unexpected " + entry); + } + } + private static String removeSpaces(String line) { return line.replaceAll("\\s+", ""); } diff --git a/java_tools/enum_to_string/src/main/java/com/rusefi/InvokeReader.java b/java_tools/enum_to_string/src/main/java/com/rusefi/InvokeReader.java new file mode 100644 index 0000000000..632d6b1d64 --- /dev/null +++ b/java_tools/enum_to_string/src/main/java/com/rusefi/InvokeReader.java @@ -0,0 +1,62 @@ +package com.rusefi; + +import com.rusefi.util.SystemOut; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +class InvokeReader { + private final static String KEY_INPUT_PATH = "-enumInputPath"; + private final static String KEY_OUTPUT = "-outputPath"; + private final static String KEY_OUTPUT_FILE = "-generatedFile"; + public static String fileSuffix = "enums"; + + private String[] args; + private String outputPath; + private List inputFiles = new ArrayList<>(); + private String inputPath = "."; + + public InvokeReader(String... args) { + if (args.length < 4) { + SystemOut.println("Please specify at least\n\n" + + EnumToString.KEY_ENUM_INPUT_FILE + "XXX\n" + + KEY_OUTPUT + "XXX\n" + ); + System.exit(-1); + } + + this.args = args; + } + + public String getInputPath() { + return inputPath; + } + + public String getOutputPath() { + return outputPath; + } + + public List getInputFiles() { + return inputFiles; + } + + public InvokeReader invoke() throws IOException { + outputPath = null; + for (int i = 0; i < args.length - 1; i += 2) { + String key = args[i]; + if (key.equals(KEY_INPUT_PATH)) { + inputPath = Objects.requireNonNull(args[i + 1], KEY_INPUT_PATH); + } else if (key.equals(EnumToString.KEY_ENUM_INPUT_FILE)) { + String headerInputFile = args[i + 1]; + inputFiles.add(headerInputFile); + } else if (key.equals(KEY_OUTPUT_FILE)) { + fileSuffix = args[i + 1]; + } else if (key.equals(KEY_OUTPUT)) { + outputPath = args[i + 1]; + } + } + return this; + } +} 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 new file mode 100644 index 0000000000..0d68593a32 --- /dev/null +++ b/java_tools/enum_to_string/src/main/java/com/rusefi/ToJavaEnum.java @@ -0,0 +1,52 @@ +package com.rusefi; + +import com.rusefi.enum_reader.Value; +import com.rusefi.util.SystemOut; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class ToJavaEnum { + public static void main(String[] args) throws IOException { + InvokeReader invokeReader = new InvokeReader(args).invoke(); + String outputPath = invokeReader.getOutputPath(); + + EnumsReader enumsReader = new EnumsReader(); + + for (String inputFile : invokeReader.getInputFiles()) { + File f = new File(invokeReader.getInputPath() + File.separator + inputFile); + SystemOut.println("Reading enums from " + f); + + enumsReader.read(new FileReader(f)); + } + + for (Map.Entry> e : enumsReader.getEnums().entrySet()) { + String java = generate(e.getKey(), e.getValue()); + + String fullFileName = outputPath + File.separator + e.getKey() + ".java"; + BufferedWriter br = new BufferedWriter(new FileWriter(fullFileName)); + br.write(java); + br.close(); + } + } + + public static String generate(String key, Map values) { + 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); + + for (Value value : sorted) { + sb.append("\t" + value.getName() + ",\n"); + } + + sb.append("}\n"); + return sb.toString(); + } +} 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 11328b4650..80edfe5236 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 @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; +import java.util.Map; import static com.rusefi.EnumsReader.isKeyValueLine; import static org.junit.Assert.assertEquals; @@ -94,5 +95,26 @@ 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()); + + List listByOrdinal = EnumsReader.getSortedByOrder(brain_pin_e); + assertEquals(0, listByOrdinal.get(0).getIntValue()); + + for (Map.Entry> e : enumsReader.getEnums().entrySet()) { + + String a = new ToJavaEnum().generate(e.getKey(), e.getValue()); + + assertEquals("package com.rusefi.enums;\n" + + "//auto-generated by ToJavaEnum.java\n" + + "\n" + + "\n" + + "\n" + + "public enum brain_pin_e {\n" + + "\tGPIO_UNASSIGNED,\n" + + "\tGPIO_INVALID,\n" + + "\tGPIO_HEX,\n" + + "}\n", a); + } } }