Skip to content
This repository was archived by the owner on May 8, 2024. It is now read-only.

Commit ceeca18

Browse files
authored
Merge pull request #1 from sc2-mkr/plugins
Add digitalWrite() optimization plugin
2 parents 1bc29a8 + 8438270 commit ceeca18

File tree

2 files changed

+137
-71
lines changed

2 files changed

+137
-71
lines changed

src/main/java/it/sc2/iregon/aco/engine/AcoEngine.java

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class AcoEngine implements Engine {
3333
private final List<Constant> constants;
3434

3535
public AcoEngine() {
36-
options = new HashMap();
36+
options = new HashMap<>();
3737

3838
// TODO: add a menu for mapper selection
3939
mappingFactory = new MappingFactory();
@@ -63,8 +63,6 @@ public void setChip(String chip) {
6363

6464
@Override
6565
public void optimize() {
66-
// findAllConstants();
67-
6866
this.options.forEach((pluginName, isActive) -> {
6967
System.out.println(pluginName + " " + isActive);
7068
if(isActive) {
@@ -75,16 +73,6 @@ public void optimize() {
7573
}
7674
}
7775
});
78-
79-
// System.out.println("#### SOURCE ####\n" + source);
80-
// if (options.get("pin-mode")) optimizePinMode();
81-
//
82-
// if (options.get("setup-and-loop")) { // Execute for last (the most destructive operation)
83-
// setSetupIndexes();
84-
// setLoopIndexes();
85-
// removeSetupAndLoop();
86-
// }
87-
8876
}
8977

9078
private void findAllConstants() {
@@ -107,64 +95,6 @@ private void findAllConstants() {
10795
}
10896
}
10997

110-
// private void optimizePinMode() {
111-
// StringBuilder sb = new StringBuilder();
112-
//
113-
// final String regex = "pinMode\\(([^,]*),\\s*([^)]*)\\)";
114-
// final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
115-
// final Matcher matcher = pattern.matcher(source);
116-
//
117-
// boolean result = matcher.find();
118-
//
119-
// if (result) {
120-
// do {
121-
// String logicalPinName = getLogicalPinName(matcher.group(1));
122-
// Optional<Pin> pinToReplace = portMapping.findPinByLogicalPin(logicalPinName);
123-
// if (pinToReplace.isPresent()) {
124-
// String replacement = "";
125-
// if (matcher.group(2).equals("OUTPUT"))
126-
// replacement = "DDR" +
127-
// pinToReplace.get().getPort() +
128-
// " |= (1<<DD" +
129-
// pinToReplace.get().getPort() +
130-
// pinToReplace.get().getPortIndex() +
131-
// ")";
132-
// System.out.println("Replacement: " + replacement);
133-
// matcher.appendReplacement(sb, replacement);
134-
// } else {
135-
// // TODO: manage pin not found
136-
// }
137-
// result = matcher.find();
138-
// } while (result);
139-
// matcher.appendTail(sb);
140-
// }
141-
// source = sb.toString();
142-
// }
143-
144-
// private String getLogicalPinName(String logicalIndex) {
145-
// try {
146-
// Integer.parseInt(logicalIndex);
147-
// return logicalIndex;
148-
// } catch (NumberFormatException e) {
149-
// return constants.stream()
150-
// .filter(constant -> constant.getName().equals(logicalIndex))
151-
// .findFirst().get().getValue();
152-
// }
153-
// }
154-
155-
// private String getDirectionBits(String portIndex) {
156-
// int portIndexInt = Integer.parseInt(portIndex);
157-
// StringBuilder retValue = new StringBuilder();
158-
//
159-
// for (int i = 0; i < (8 - portIndexInt) - 1; i++) {
160-
// retValue.append("0");
161-
// }
162-
// retValue.append("1");
163-
// for (int i = (8 - portIndexInt); i < 8; i++) {
164-
// retValue.append("0");
165-
// }
166-
// return retValue.toString();
167-
// }
16898

16999
// private void removeSetupAndLoop() {
170100
// String setup = source.substring(startSetupIndex, endSetupIndex);
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)