diff --git a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/CurveData.java b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/CurveData.java index 71f15f6109..63d304eb2e 100644 --- a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/CurveData.java +++ b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/CurveData.java @@ -43,7 +43,7 @@ public class CurveData implements HoHo { return null; ArrayIniField field = (ArrayIniField) iniField; int curveSize = field.getRows(); - BufferedReader r = TS2C.readAndScroll(msqFileName, curveName + "\""); + BufferedReader r = TS2C.readAndScroll(msqFileName, curveName + "\"", TS2C.fileFactory); float[] curveValues = new float[curveSize]; readAxle(curveValues, r); diff --git a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TS2C.java b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TS2C.java index 7323561dba..c5463126c4 100644 --- a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TS2C.java +++ b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TS2C.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import java.io.*; import java.util.Date; +import java.util.function.Function; /** * tuner studio project to C data structure converter command line utility @@ -14,6 +15,18 @@ import java.util.Date; */ @SuppressWarnings("StringConcatenationInsideStringBufferAppend") public class TS2C { + public static final Function fileFactory = new Function() { + @Override + public Reader apply(String fileName) { + try { + return new FileReader(fileName); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + }; + + public static String FINGER_PRINT = "/* Generated by " + TS2C.class.getSimpleName() + " on " + new Date() + "*/\n"; /** @@ -121,19 +134,22 @@ public class TS2C { /** * @param fileName text file to open * @param magicStringKey magic string content to scroll to + * @param factory * @return Reader after the magicStringKey line */ - static BufferedReader readAndScroll(String fileName, String magicStringKey) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(fileName)); + static BufferedReader readAndScroll(String fileName, String magicStringKey, Function factory) throws IOException { + + Reader reader = factory.apply(fileName); + BufferedReader br = new BufferedReader(reader); System.out.println("Reading from " + fileName + ", scrolling to " + magicStringKey); String line; - while ((line = reader.readLine()) != null) { + while ((line = br.readLine()) != null) { if (line.contains(magicStringKey)) { System.out.println("Found " + line); break; } } - return reader; + return br; } private static String writeTableLine(CurveData loadBins, CurveData rpmBins, ValueSource valueSource, int loadIndex) { diff --git a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TableData.java b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TableData.java index 57573d487b..e51b49eaab 100644 --- a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TableData.java +++ b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TableData.java @@ -6,7 +6,9 @@ import org.jetbrains.annotations.NotNull; import java.io.BufferedReader; import java.io.IOException; +import java.io.Reader; import java.util.Arrays; +import java.util.function.Function; public class TableData { @NotNull @@ -16,22 +18,27 @@ public class TableData { if (field.getRows() != field.getCols()) throw new UnsupportedOperationException("Not square table not supported yet"); // todo: replace with loadCount & rpmCount - int size = field.getRows(); + int rows = field.getRows(); - float[][] table = new float[size][]; - for (int i = 0; i < size; i++) { - table[i] = new float[size]; + return readTable(msqFileName, tableName, rows, TS2C.fileFactory, field.getCols()); + } + + @NotNull + public static float[][] readTable(String msqFileName, String tableName, int rows, Function factory, int columns) throws IOException { + float[][] table = new float[rows][]; + for (int rowIndex = 0; rowIndex < rows; rowIndex++) { + table[rowIndex] = new float[columns]; } - BufferedReader reader = TS2C.readAndScroll(msqFileName, tableName); - readTable(table, reader, size); + BufferedReader reader = TS2C.readAndScroll(msqFileName, tableName, factory); + readTable(table, reader, rows, columns); return table; } - private static void readTable(float[][] table, BufferedReader r, int size) throws IOException { - int index = 0; + private static void readTable(float[][] table, BufferedReader r, int rows, int columns) throws IOException { + int rowIndex = 0; - while (index < size) { + while (rowIndex < rows) { String line = r.readLine(); if (line == null) throw new IOException("End of file?"); @@ -40,19 +47,19 @@ public class TableData { continue; String[] values = line.split("\\s"); - if (values.length != size) - throw new IllegalStateException("Expected " + size + " but got " + Arrays.toString(values) + ". Unexpected line: " + line); + if (values.length != columns) + throw new IllegalStateException("Expected " + columns + " but got " + values.length + " content = " + Arrays.toString(values) + ". Unexpected line: " + line); - for (int i = 0; i < size; i++) { - String str = values[i]; + for (int column = 0; column < columns; column++) { + String str = values[column]; try { - table[index][i] = Float.parseFloat(str); + table[rowIndex][column] = Float.parseFloat(str); } catch (NumberFormatException e) { throw new IllegalStateException("While reading " + str, e); } } - System.out.println("Got line " + index + ": " + Arrays.toString(table[index])); - index++; + System.out.println("Got line " + rowIndex + ": " + Arrays.toString(table[rowIndex])); + rowIndex++; } } } diff --git a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TuneCanTool.java b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TuneCanTool.java index cb310e7ddf..df08841baf 100644 --- a/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TuneCanTool.java +++ b/java_tools/tune-tools/src/main/java/com/rusefi/tools/tune/TuneCanTool.java @@ -49,7 +49,11 @@ public class TuneCanTool { RootHolder.ROOT = "../firmware/"; - processREOtune(1507, Fields.engine_type_e_HELLEN_154_HYUNDAI_COUPE_BK2, "BK2"); + writeDiffBetweenLocalTuneFileAndDefaultTune("x", TuneCanTool.DEFAULT_TUNE, + "C:\\stuff\\i\\canam-2022-short\\canam-progress-nov-26.msq", "x"); + + +// processREOtune(1507, Fields.engine_type_e_HELLEN_154_HYUNDAI_COUPE_BK2, "BK2"); // processREOtune(1502, Fields.engine_type_e_HYUNDAI_PB, "PB"); // processREOtune(1490, Fields.engine_type_e_MRE_M111, "m111-alex"); // handle("Mitsubicha", 1258); @@ -76,7 +80,7 @@ public class TuneCanTool { writeDiffBetweenLocalTuneFileAndDefaultTune(vehicleName, currentTuneFileName, localFileName, url); } - private static void writeDiffBetweenLocalTuneFileAndDefaultTune(String vehicleName, String currentTuneFileName, String localFileName, String cannedComment) throws JAXBException, IOException { + private static void writeDiffBetweenLocalTuneFileAndDefaultTune(String vehicleName, String defaultTuneFileName, String localFileName, String cannedComment) throws JAXBException, IOException { String reportsOutputFolder = "tune_reports"; new File(reportsOutputFolder).mkdir(); @@ -84,8 +88,8 @@ public class TuneCanTool { StringBuilder methods = new StringBuilder(); - Msq currentTune = Msq.readTune(currentTuneFileName); - StringBuilder sb = TuneCanTool.getTunePatch(currentTune, custom, ini, currentTuneFileName, methods); + Msq defaultTune = Msq.readTune(defaultTuneFileName); + StringBuilder sb = TuneCanTool.getTunePatch(defaultTune, custom, ini, defaultTuneFileName, methods); String fileNameMethods = reportsOutputFolder + "/" + vehicleName + "_methods.md"; try (FileWriter methodsWriter = new FileWriter(fileNameMethods)) { @@ -177,8 +181,8 @@ public class TuneCanTool { if (cf.isArray()) { if (cf.getArraySizes().length == 2) { - //float[][] tableData = TableData.readTable(currentTuneFileName, name, ini); - //System.out.printf(" " + name); + float[][] tableData = TableData.readTable(currentTuneFileName, name, ini); + System.out.printf(" " + name); continue; } diff --git a/java_tools/tune-tools/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java b/java_tools/tune-tools/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java index 9aa49f471f..5c1e18efc4 100644 --- a/java_tools/tune-tools/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java +++ b/java_tools/tune-tools/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java @@ -7,7 +7,6 @@ import com.rusefi.tools.tune.TuneTools; import com.rusefi.tune.xml.Msq; import com.rusefi.tune.xml.Page; import org.junit.Assert; -import org.junit.function.ThrowingRunnable; import org.junit.jupiter.api.Test; import static org.junit.Assert.*; diff --git a/java_tools/tune-tools/src/test/java/com/rusefi/tune/TableDataTest.java b/java_tools/tune-tools/src/test/java/com/rusefi/tune/TableDataTest.java new file mode 100644 index 0000000000..8480ece82b --- /dev/null +++ b/java_tools/tune-tools/src/test/java/com/rusefi/tune/TableDataTest.java @@ -0,0 +1,32 @@ +package com.rusefi.tune; + +import com.rusefi.tools.tune.TableData; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +import static org.junit.Assert.assertEquals; + +public class TableDataTest { + @Test + public void read() throws IOException { + + String s = "\n" + + " 0.0 0.0 0.0 333 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 444 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 \n" + + " \n"; + + Reader r = new StringReader(s); + float[][] data = TableData.readTable("x", "scriptTable4", 8, s1 -> r, 10); + + assertEquals(333, (int)data[0][3]); + } +}