-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathGCodeCommand.java
More file actions
175 lines (141 loc) · 4.45 KB
/
GCodeCommand.java
File metadata and controls
175 lines (141 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package replicatorg.app.gcode;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GCodeCommand {
// These are the letter codes that we understand
static protected char[] codes = {
'A', 'B', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'P', 'Q', 'R', 'S', 'T', 'X', 'Y', 'Z' };
// pattern matchers.
static Pattern parenPattern = Pattern.compile("\\((.*)\\)");
static Pattern semiPattern = Pattern.compile(";(.*)");
static Pattern deleteBlockPattern = Pattern.compile("^(\\.*)");
// The actual GCode command string
private String command;
// Parsed out comment
private String comment = new String();
private class gCodeParameter {
final public char code;
final public Double value;
gCodeParameter(char code, Double value) {
this.code = code;
this.value = value;
}
}
// The set of parameters in this GCode
private List<gCodeParameter> parameters;
public GCodeCommand(String command) {
// Copy over the command
this.command = new String(command);
// Initialize the present and value tables
this.parameters = new ArrayList<gCodeParameter>();
// Parse (and strip) any comments out into a comment string
parseComments();
// Parse any codes out into the code tables
parseCodes();
}
// Find any comments, store them, then remove them from the command
private void parseComments() {
Matcher parenMatcher = parenPattern.matcher(command);
Matcher semiMatcher = semiPattern.matcher(command);
// Note that we only support one style of comments, and only one comment per row.
if (parenMatcher.find())
comment = parenMatcher.group(1);
if (semiMatcher.find())
comment = semiMatcher.group(1);
// clean it up.
comment = comment.trim();
comment = comment.replace('|', '\n');
// Finally, remove the comments from the command string
command = parenMatcher.replaceAll("");
semiMatcher = semiPattern.matcher(command);
command = semiMatcher.replaceAll("");
}
// Find any codes, and store them
private void parseCodes() {
for (char code : codes) {
Pattern myPattern = Pattern.compile(code + "([0-9.+-]+)");
Matcher myMatcher = myPattern.matcher(command);
if (command.indexOf(code) >= 0) {
double value = 0;
if (myMatcher.find()) {
String match = myMatcher.group(1);
value = Double.parseDouble(match);
}
parameters.add(new gCodeParameter(code, value));
}
}
}
public String getCommand() {
// TODO: Note that this is the command minus any comments.
return new String(command);
}
public String getComment() {
return new String(comment);
}
public boolean hasCode(char searchCode) {
for (gCodeParameter parameter : parameters) {
if (parameter.code == searchCode) {
return true;
}
}
return false;
}
public double getCodeValue(char searchCode) {
return getCodeValue(searchCode, -1);
}
public double getCodeValue(char searchCode, double fallback) {
for (gCodeParameter parameter : parameters) {
if (parameter.code == searchCode) {
return parameter.value;
}
}
return fallback;
}
public int getCodeValueInt(char searchCode, int fallback) {
return (int) getCodeValue(searchCode, fallback);
}
/*
* Note: unlike getCodeValue(char) above, this has no magic default
* return value. If used on a nonexistent code, it throws to indicate
* a programming error (in RepG, not the GCode).
*/
public int getCodeValueInt(char searchCode) {
for (gCodeParameter parameter : parameters) {
if (parameter.code == searchCode) {
return parameter.value.intValue();
}
}
throw new IllegalStateException(
"getCodeValueInt called for nonexistent code " + searchCode
+ "; caller should have checked hasCode first!");
}
// public Double removeCode(Character searchCode) {
// for (Iterator<gCodeParameter> i = parameters.iterator(); i.hasNext();)
// {
// gCodeParameter gcp = i.next();
// if(gcp.code == searchCode)
// {
// i.remove();
// return gcp.value;
// }
// }
// return null;
// }
//
// public void addCode(Character code, Double value)
// {
// parameters.add(new gCodeParameter(code, value));
// command += " ";
// command = command.concat(code.toString()).concat(value.toString());
// }
//
// public void addCode(Character code, Integer value)
// {
// parameters.add(new gCodeParameter(code, ((Number)value).doubleValue()));
// command += " ";
// command = command.concat(code.toString()).concat(value.toString());
// }
}