|
| 1 | +package it.sc2.iregon.aco.engine.plugin.plugins; |
| 2 | + |
| 3 | +import it.sc2.iregon.aco.config.Mapping; |
| 4 | +import it.sc2.iregon.aco.config.structure.Pin; |
| 5 | +import it.sc2.iregon.aco.engine.structure.Constant; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.regex.Matcher; |
| 11 | +import java.util.regex.Pattern; |
| 12 | + |
| 13 | +public class DigitalWritePlugin implements Plugin{ |
| 14 | + |
| 15 | + private String source; |
| 16 | + private Mapping pinMapping; |
| 17 | + |
| 18 | + ViewOption viewOption; |
| 19 | + |
| 20 | + // sketch constants |
| 21 | + private final List<Constant> constants; |
| 22 | + |
| 23 | + public DigitalWritePlugin() { |
| 24 | + this.viewOption = new ViewOption(true); |
| 25 | + constants = new ArrayList<>(); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getPluginName() { |
| 30 | + return "Optimize digitalWrite()"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getPluginDescription() { |
| 35 | + return "Replace digitalWrite function with relative port manipulation instruction"; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void load(String source, Mapping pinMapping) { |
| 40 | + this.source = source; |
| 41 | + this.pinMapping = pinMapping; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String run() { |
| 46 | + findAllConstants(); |
| 47 | + |
| 48 | + StringBuilder sb = new StringBuilder(); |
| 49 | + |
| 50 | + final String regex = "digitalWrite\\(([^,]+),\\s*([^\\)]+)\\)"; |
| 51 | + final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); |
| 52 | + final Matcher matcher = pattern.matcher(source); |
| 53 | + |
| 54 | + boolean result = matcher.find(); |
| 55 | + |
| 56 | + if (result) { |
| 57 | + do { |
| 58 | + String logicalPinName = getLogicalPinName(matcher.group(1)); |
| 59 | + Optional<Pin> pinToReplace = pinMapping.findPinByLogicalName(logicalPinName); |
| 60 | + if (pinToReplace.isPresent()) { |
| 61 | + String replacement = "PORT" + |
| 62 | + pinToReplace.get().getPort(); |
| 63 | + if(matcher.group(2).equals("HIGH")) |
| 64 | + replacement += " |= " + |
| 65 | + getPortSettingBits( |
| 66 | + pinToReplace.get().getPortIndex(), |
| 67 | + "0", |
| 68 | + "1"); |
| 69 | + else if(matcher.group(2).equals("LOW")) |
| 70 | + replacement += " &= " + |
| 71 | + getPortSettingBits( |
| 72 | + pinToReplace.get().getPortIndex(), |
| 73 | + "1", |
| 74 | + "0"); |
| 75 | + System.out.println("Replacement: " + replacement); |
| 76 | + matcher.appendReplacement(sb, replacement); |
| 77 | + } else { |
| 78 | + // TODO: manage pin not found |
| 79 | + } |
| 80 | + result = matcher.find(); |
| 81 | + } while (result); |
| 82 | + matcher.appendTail(sb); |
| 83 | + } |
| 84 | + return sb.toString(); |
| 85 | + } |
| 86 | + |
| 87 | + private String getLogicalPinName(String logicalIndex) { |
| 88 | + try { |
| 89 | + Integer.parseInt(logicalIndex); |
| 90 | + return logicalIndex; |
| 91 | + } catch (NumberFormatException e) { |
| 92 | + return constants.stream() |
| 93 | + .filter(constant -> constant.getName().equals(logicalIndex)) |
| 94 | + .findFirst().get().getValue(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void findAllConstants() { |
| 99 | + // Const variables |
| 100 | + final String constRegex = "const\\s+([^\\s]+)\\s+([^\\s]+)\\s+=\\s+([^;]+)"; |
| 101 | + final Pattern constPattern = Pattern.compile(constRegex, Pattern.MULTILINE); |
| 102 | + final Matcher constMatcher = constPattern.matcher(source); |
| 103 | + |
| 104 | + while (constMatcher.find()) { |
| 105 | + constants.add(new Constant(constMatcher.group(2), constMatcher.group(3), Constant.Type.CONST)); |
| 106 | + } |
| 107 | + |
| 108 | + // DEFINE |
| 109 | + final String defineRegex = "#define\\s*([^\\s]+)\\s*(.*)"; |
| 110 | + final Pattern definePattern = Pattern.compile(defineRegex, Pattern.MULTILINE); |
| 111 | + final Matcher defineMatcher = definePattern.matcher(source); |
| 112 | + |
| 113 | + while (defineMatcher.find()) { |
| 114 | + constants.add(new Constant(defineMatcher.group(1), defineMatcher.group(2), Constant.Type.DEFINE)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private String getPortSettingBits(String selectedBitIndex, String emptyValue, String fillValue) { |
| 119 | + int portIndexInt = Integer.parseInt(selectedBitIndex); |
| 120 | + StringBuilder retValue = new StringBuilder(); |
| 121 | + |
| 122 | + for (int i = 0; i < (8 - portIndexInt) - 1; i++) { |
| 123 | + retValue.append(emptyValue); |
| 124 | + } |
| 125 | + retValue.append(fillValue); |
| 126 | + for (int i = (8 - portIndexInt); i < 8; i++) { |
| 127 | + retValue.append(emptyValue); |
| 128 | + } |
| 129 | + return retValue.toString(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public ViewOption getViewOption() { |
| 134 | + return viewOption; |
| 135 | + } |
| 136 | +} |
0 commit comments