|
25 | 25 | import java.util.Arrays;
|
26 | 26 | import java.util.LinkedList;
|
27 | 27 | import java.util.List;
|
| 28 | +import java.util.ArrayDeque; |
28 | 29 |
|
29 | 30 | /**
|
30 | 31 | * This class implements a driver for Grbl based firmwares.
|
|
33 | 34 | */
|
34 | 35 | public class Grbl extends GenericGcodeDriver
|
35 | 36 | {
|
| 37 | + protected ArrayDeque<Integer> commandLenQueue; |
| 38 | + |
36 | 39 | public Grbl()
|
37 | 40 | {
|
| 41 | + commandLenQueue = new ArrayDeque<Integer>(); |
38 | 42 | //set some grbl-specific defaults
|
39 | 43 | setLineend("CR");
|
40 | 44 | setIdentificationLine("Grbl");
|
@@ -105,20 +109,109 @@ public void setAutoHome(boolean auto_home)
|
105 | 109 | this.autoHome = auto_home;
|
106 | 110 | }
|
107 | 111 |
|
| 112 | + protected boolean simpleStreamMode = true; |
| 113 | + |
| 114 | + public boolean isSimpleStreamMode() |
| 115 | + { |
| 116 | + return simpleStreamMode; |
| 117 | + } |
| 118 | + |
| 119 | + public void setSimpleStreamMode(boolean mode) throws IOException |
| 120 | + { |
| 121 | + if (mode != simpleStreamMode) { |
| 122 | + if (!simpleStreamMode) |
| 123 | + waitForCommandsCompletion(); |
| 124 | + else |
| 125 | + commandLenQueue.clear(); |
| 126 | + |
| 127 | + simpleStreamMode = mode; |
| 128 | + } |
| 129 | + } |
108 | 130 |
|
109 | 131 | @Override
|
110 | 132 | public String getModelName()
|
111 | 133 | {
|
112 | 134 | return "Grbl Gcode Driver";
|
113 | 135 | }
|
114 |
| - |
| 136 | + |
115 | 137 | protected void sendLineWithoutWait(String text, Object... parameters) throws IOException
|
116 | 138 | {
|
117 | 139 | boolean wasSetWaitingForOk = isWaitForOKafterEachLine();
|
118 | 140 | setWaitForOKafterEachLine(false);
|
119 | 141 | sendLine(text, parameters);
|
120 | 142 | setWaitForOKafterEachLine(wasSetWaitingForOk);
|
121 | 143 | }
|
| 144 | + |
| 145 | + @Override |
| 146 | + protected String waitForLine() throws IOException |
| 147 | + { |
| 148 | + String line = ""; |
| 149 | + while ("".equals(line)) |
| 150 | + {//skip empty lines |
| 151 | + line = in.readLine(); |
| 152 | + } |
| 153 | + |
| 154 | + //TODO: remove |
| 155 | + if (isSimpleStreamMode() || !"ok".equals(line)) |
| 156 | + System.out.println("< "+line); |
| 157 | + else { |
| 158 | + int len = commandLenQueue.peek(); |
| 159 | + // Debug: print counted chars still remains in the buffer AFTER receiving this 'ok' response |
| 160 | + System.out.println(String.format(FORMAT_LOCALE, "%d< %s", getBufferedCommandsLen() - len, line)); |
| 161 | + } |
| 162 | + |
| 163 | + return line; |
| 164 | + } |
| 165 | + |
| 166 | + protected static final int GRBL_BUF_LEN = 128; |
| 167 | + |
| 168 | + protected Integer getBufferedCommandsLen() |
| 169 | + { |
| 170 | + Integer len = 0; |
| 171 | + for (Integer c : commandLenQueue) |
| 172 | + len += c; |
| 173 | + return len; |
| 174 | + } |
| 175 | + |
| 176 | + protected void sendLineSimple(String text, Object... parameters) throws IOException |
| 177 | + { |
| 178 | + super.sendLine(text, parameters); |
| 179 | + } |
| 180 | + |
| 181 | + protected void sendLineCC(String text, Object... parameters) throws IOException |
| 182 | + { |
| 183 | + String outStr = String.format(FORMAT_LOCALE, text+LINEEND(), parameters); |
| 184 | + int len = outStr.length(); |
| 185 | + |
| 186 | + // Read all received responses from grbl or wait for needed free space in the serial buffer |
| 187 | + while (in.ready() || (getBufferedCommandsLen() + len > GRBL_BUF_LEN)) { |
| 188 | + String line = waitForLine(); |
| 189 | + if (!"ok".equals(line)) |
| 190 | + { |
| 191 | + throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead."); |
| 192 | + } |
| 193 | + commandLenQueue.remove(); |
| 194 | + } |
| 195 | + |
| 196 | + commandLenQueue.add(len); |
| 197 | + out.print(outStr); |
| 198 | + //TODO: Remove |
| 199 | + System.out.println(String.format(FORMAT_LOCALE, "%d> %s", getBufferedCommandsLen(), outStr)); |
| 200 | + out.flush(); |
| 201 | + } |
| 202 | + |
| 203 | + protected void waitForCommandsCompletion() throws IOException |
| 204 | + { |
| 205 | + while (!commandLenQueue.isEmpty()) { |
| 206 | + String line = waitForLine(); |
| 207 | + if (!"ok".equals(line)) |
| 208 | + { |
| 209 | + throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead."); |
| 210 | + } |
| 211 | + commandLenQueue.remove(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
122 | 215 |
|
123 | 216 | /**
|
124 | 217 | * Initializes Grbl, handling issuing of soft-reset and initial homing
|
@@ -186,28 +279,39 @@ protected void move(PrintStream out, double x, double y, double resolution) thro
|
186 | 279 | sendLine("G0 X%f Y%f", x, y);
|
187 | 280 | }
|
188 | 281 | }
|
189 |
| - |
| 282 | + |
190 | 283 | /**
|
191 | 284 | * Send a line of gcode to the cutter, stripping out any whitespace in the process
|
192 | 285 | * @param text
|
193 | 286 | * @param parameters
|
194 | 287 | * @throws IOException
|
195 | 288 | */
|
196 | 289 | @Override
|
| 290 | + |
197 | 291 | protected void sendLine(String text, Object... parameters) throws IOException
|
198 | 292 | {
|
199 |
| - out.format(FORMAT_LOCALE, text.replace(" ", "")+LINEEND(), parameters); |
200 |
| - //TODO: Remove |
201 |
| - System.out.println(String.format(FORMAT_LOCALE, "> "+text+LINEEND(), parameters)); |
202 |
| - out.flush(); |
203 |
| - if (isWaitForOKafterEachLine()) |
204 |
| - { |
205 |
| - String line = waitForLine(); |
206 |
| - if (!"ok".equals(line)) |
207 |
| - { |
208 |
| - throw new IOException("Lasercutter did not respond 'ok', but '"+line+"'instead."); |
209 |
| - } |
210 |
| - } |
| 293 | + if (isSimpleStreamMode()) |
| 294 | + sendLineSimple(text, parameters); |
| 295 | + else |
| 296 | + sendLineCC(text, parameters); |
| 297 | + } |
| 298 | + |
| 299 | + @Override |
| 300 | + protected void sendJobPrepare() throws IOException { |
| 301 | + setSimpleStreamMode(false); |
| 302 | + } |
| 303 | + |
| 304 | + @Override |
| 305 | + protected void sendJobFinish() throws IOException { |
| 306 | + setSimpleStreamMode(true); |
| 307 | + } |
| 308 | + |
| 309 | + @Override |
| 310 | + protected void setKeysMissingFromDeserialization() |
| 311 | + { |
| 312 | + super.setKeysMissingFromDeserialization(); |
| 313 | + commandLenQueue = new ArrayDeque<Integer>(); |
| 314 | + simpleStreamMode = true; |
211 | 315 | }
|
212 | 316 |
|
213 | 317 | @Override
|
|
0 commit comments