export board-specific macro for stm32 pins based on board yaml #3298

This commit is contained in:
rusefillc 2023-08-27 21:48:01 -04:00
parent b0298e58bc
commit ca178224b6
5 changed files with 80 additions and 2 deletions

View File

@ -13,4 +13,6 @@ public interface BoardInputs {
List<String> getInputFiles();
Writer getWriter() throws IOException;
List<String> getBoardMeta(String boardMetaFileName);
}

View File

@ -3,6 +3,9 @@ package com.rusefi.pinout;
import com.devexperts.logging.Logging;
import java.io.*;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -59,4 +62,13 @@ public class FileSystemBoardInputsImpl implements BoardInputs {
public Writer getWriter() throws IOException {
return new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp");
}
@Override
public List<String> getBoardMeta(String boardMetaFileName) {
try {
return Files.readAllLines(Paths.get(URI.create(boardMetaFileName)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -133,6 +133,9 @@ public class PinoutLogic {
SystemOut.println("Null yaml for " + yamlFile);
return;
}
Map</*meta name*/String, /*native name*/String> map = processMetaHeader(yamlData);
List<Map<String, Object>> data = (List<Map<String, Object>>) yamlData.get("pins");
if (data == null) {
SystemOut.println("Null yaml for " + yamlFile);
@ -175,6 +178,36 @@ public class PinoutLogic {
}
}
private Map<String, String> processMetaHeader(Map<String, Object> yamlData) {
String metaHeader = (String) yamlData.get("meta");
if (metaHeader==null)
return Collections.emptyMap();
return getStringStringMap(boardInputs.getBoardMeta(metaHeader));
}
@NotNull
public static Map<String, String> getStringStringMap(List<String> lines) {
Map</*meta name*/String, /*native name*/String> map = new HashMap<>();
for (String line : lines) {
line = line.trim();
if (ToolUtil.startsWithToken(line, VariableRegistry.DEFINE)) {
line = line.substring(VariableRegistry.DEFINE.length() + 1);
int index = line.indexOf(' ');
if (index == -1)
continue;
String name = line.substring(0, index);
String value = line.substring(index).trim();
map.put(name, value);
}
}
return map;
}
private void addPinToList(String id, String pinTsName, String pinClass) {
String existingTsName = tsNameById.get(id);
if (existingTsName != null && !existingTsName.equals(pinTsName))

View File

@ -6,15 +6,34 @@ import com.rusefi.newparse.DefinitionsState;
import org.junit.Test;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class PinoutLogicIntegratedTest {
private static final List<String> META_CONTENT = Arrays.asList("#define H144_LS_1 Gpio::G7\n",
"#define H144_LS_2 Gpio::G8\n",
"// IN_O2S AIN13 A13 PA0\n",
"#define H144_IN_O2S EFI_ADC_0\n",
"// IN_O2S2 AIN12 PA1\n",
"#define H144_IN_O2S2 EFI_ADC_1");
@Test
public void testMetaParsing() {
Map</*meta name*/String, /*native name*/String> map = PinoutLogic.getStringStringMap(META_CONTENT);
assertEquals(4, map.size());
assertEquals("EFI_ADC_0", map.get("H144_IN_O2S"));
}
@Test
public void testWholeThing() throws IOException {
runPinoutTest("pins:\n" +
runPinoutTest("meta: meta.h\n" +
"pins:\n" +
" - pin: 1\n" +
" id: [E11, E11]\n" +
" class: [event_inputs, switch_inputs]\n" +
@ -40,6 +59,11 @@ public class PinoutLogicIntegratedTest {
@Test
public void testTemplate() throws IOException {
runPinoutTest("pins:\n" +
" - pin: 2\n" +
" id: G8\n" +
" class: outputs\n" +
" function: Digital trigger/switch input for instance Hall type CAM\n" +
" ts_name: ___ - Digital 1\n" +
" - pin: 1\n" +
" id: [E11, E11]\n" +
" class: [event_inputs, switch_inputs]\n" +
@ -55,6 +79,7 @@ public class PinoutLogicIntegratedTest {
"const char * getBoardSpecificPinName(brain_pin_e brainPin) {\n" +
"\tswitch(brainPin) {\n" +
"\t\tcase Gpio::E11: return \"1 - Digital 2\";\n" +
"\t\tcase Gpio::G8: return \"2 - Digital 1\";\n" +
"\t\tdefault: return nullptr;\n" +
"\t}\n" +
"\treturn nullptr;\n" +
@ -92,6 +117,11 @@ public class PinoutLogicIntegratedTest {
public Writer getWriter() {
return testWriter;
}
@Override
public List<String> getBoardMeta(String boardMetaFileName) {
return META_CONTENT;
}
};
ReaderStateImpl state = new ReaderStateImpl();
@ -99,6 +129,7 @@ public class PinoutLogicIntegratedTest {
state.getEnumsReader().read(new StringReader("enum class Gpio : uint16_t {\n" +
"Unassigned = 0,\n" +
"Invalid = 0x01,\n" +
"G8 = 5,\n" +
"E11 = 0x0B,\n" +
"};"));

View File

@ -44,7 +44,7 @@ public class ToolUtil {
return line.length() == 0 || line.startsWith("!") || line.startsWith("//");
}
static boolean startsWithToken(String line, String token) {
public static boolean startsWithToken(String line, String token) {
return line.startsWith(token + " ") || line.startsWith(token + "\t");
}