20
20
21
21
import com .t_oster .liblasercut .ProgressListener ;
22
22
import com .t_oster .liblasercut .platform .Util ;
23
+ import com .t_oster .liblasercut .*;
23
24
import java .io .IOException ;
24
25
import java .io .PrintStream ;
25
26
import java .util .Arrays ;
26
27
import java .util .LinkedList ;
27
28
import java .util .List ;
29
+ import java .util .ArrayDeque ;
28
30
29
31
/**
30
32
* This class implements a driver for Grbl based firmwares.
33
35
*/
34
36
public class Grbl extends GenericGcodeDriver
35
37
{
38
+ protected ArrayDeque <Integer > commandLenQueue ;
39
+
36
40
public Grbl ()
37
41
{
42
+ commandLenQueue = new ArrayDeque <Integer >();
38
43
//set some grbl-specific defaults
39
44
setLineend ("CR" );
40
45
setIdentificationLine ("Grbl" );
@@ -105,6 +110,24 @@ public void setAutoHome(boolean auto_home)
105
110
this .autoHome = auto_home ;
106
111
}
107
112
113
+ protected boolean simpleStreamMode = true ;
114
+
115
+ public boolean isSimpleStreamMode ()
116
+ {
117
+ return simpleStreamMode ;
118
+ }
119
+
120
+ public void setSimpleStreamMode (boolean mode ) throws IOException
121
+ {
122
+ if (mode != simpleStreamMode ) {
123
+ if (!simpleStreamMode )
124
+ waitForCommandsCompletion ();
125
+ else
126
+ commandLenQueue .clear ();
127
+
128
+ simpleStreamMode = mode ;
129
+ }
130
+ }
108
131
109
132
@ Override
110
133
public String getModelName ()
@@ -119,6 +142,53 @@ protected void sendLineWithoutWait(String text, Object... parameters) throws IOE
119
142
sendLine (text , parameters );
120
143
setWaitForOKafterEachLine (wasSetWaitingForOk );
121
144
}
145
+
146
+
147
+ protected static final int GRBL_BUF_LEN = 128 ;
148
+
149
+ protected Integer getBufferedCommandsLen ()
150
+ {
151
+ Integer len = 0 ;
152
+ for (Integer c : commandLenQueue )
153
+ len += c ;
154
+ return len ;
155
+ }
156
+
157
+ protected void sendLineCC (String text , Object ... parameters ) throws IOException
158
+ {
159
+ String outStr = String .format (FORMAT_LOCALE , text +LINEEND (), parameters );
160
+ int len = outStr .length ();
161
+
162
+ while (in .ready () || (getBufferedCommandsLen () + len > GRBL_BUF_LEN )) {
163
+ String line = waitForLine ();
164
+ if (!"ok" .equals (line ))
165
+ {
166
+ throw new IOException ("Lasercutter did not respond 'ok', but '" +line +"'instead." );
167
+ }
168
+ commandLenQueue .remove ();
169
+ System .out .println (String .format (FORMAT_LOCALE , "%d" + LINEEND (), getBufferedCommandsLen (), parameters ));
170
+ }
171
+
172
+ commandLenQueue .add (len );
173
+ out .print (outStr );
174
+ //TODO: Remove
175
+ System .out .println (String .format (FORMAT_LOCALE , "%d> %s" , getBufferedCommandsLen (), outStr ));
176
+ out .flush ();
177
+ }
178
+
179
+ protected void waitForCommandsCompletion () throws IOException
180
+ {
181
+ while (!commandLenQueue .isEmpty ()) {
182
+ String line = waitForLine ();
183
+ if (!"ok" .equals (line ))
184
+ {
185
+ throw new IOException ("Lasercutter did not respond 'ok', but '" +line +"'instead." );
186
+ }
187
+ commandLenQueue .remove ();
188
+ System .out .println (String .format (FORMAT_LOCALE , "%d" + LINEEND (), getBufferedCommandsLen ()));
189
+ }
190
+ }
191
+
122
192
123
193
/**
124
194
* Initializes Grbl, handling issuing of soft-reset and initial homing
@@ -187,14 +257,7 @@ protected void move(PrintStream out, double x, double y, double resolution) thro
187
257
}
188
258
}
189
259
190
- /**
191
- * Send a line of gcode to the cutter, stripping out any whitespace in the process
192
- * @param text
193
- * @param parameters
194
- * @throws IOException
195
- */
196
- @ Override
197
- protected void sendLine (String text , Object ... parameters ) throws IOException
260
+ protected void sendLineSimple (String text , Object ... parameters ) throws IOException
198
261
{
199
262
out .format (FORMAT_LOCALE , text .replace (" " , "" )+LINEEND (), parameters );
200
263
//TODO: Remove
@@ -210,6 +273,80 @@ protected void sendLine(String text, Object... parameters) throws IOException
210
273
}
211
274
}
212
275
276
+ /**
277
+ * Send a line of gcode to the cutter, stripping out any whitespace in the process
278
+ * @param text
279
+ * @param parameters
280
+ * @throws IOException
281
+ */
282
+ @ Override
283
+
284
+ protected void sendLine (String text , Object ... parameters ) throws IOException
285
+ {
286
+ if (isSimpleStreamMode ())
287
+ sendLineSimple (text , parameters );
288
+ else
289
+ sendLineCC (text , parameters );
290
+ }
291
+
292
+ @ Override
293
+ public void sendJob (LaserJob job , ProgressListener pl , List <String > warnings ) throws IllegalJobException , Exception {
294
+ pl .progressChanged (this , 0 );
295
+ this .currentPower = -1 ;
296
+ this .currentSpeed = -1 ;
297
+
298
+ pl .taskChanged (this , "checking job" );
299
+ checkJob (job );
300
+ this .jobName = job .getName ()+".gcode" ;
301
+ job .applyStartPoint ();
302
+ pl .taskChanged (this , "connecting..." );
303
+ connect (pl );
304
+ pl .taskChanged (this , "sending" );
305
+ try {
306
+ writeInitializationCode ();
307
+
308
+ boolean wasSimpleMode = isSimpleStreamMode ();
309
+ setSimpleStreamMode ( false );
310
+
311
+ pl .progressChanged (this , 20 );
312
+ int i = 0 ;
313
+ int max = job .getParts ().size ();
314
+ for (JobPart p : job .getParts ())
315
+ {
316
+ if (p instanceof Raster3dPart || p instanceof RasterPart )
317
+ {
318
+ p = convertRasterizableToVectorPart ((RasterizableJobPart ) p , p .getDPI (), getUseBidirectionalRastering ());
319
+ }
320
+ if (p instanceof VectorPart )
321
+ {
322
+ //TODO: in direct mode use progress listener to indicate progress
323
+ //of individual job
324
+ writeVectorGCode ((VectorPart ) p , p .getDPI ());
325
+ }
326
+ i ++;
327
+ pl .progressChanged (this , 20 + (int ) (i *(double ) 60 /max ));
328
+ }
329
+ setSimpleStreamMode (wasSimpleMode );
330
+ writeShutdownCode ();
331
+ disconnect (job .getName ()+".gcode" );
332
+ }
333
+ catch (IOException e ) {
334
+ pl .taskChanged (this , "disconnecting" );
335
+ disconnect (this .jobName );
336
+ throw e ;
337
+ }
338
+ pl .taskChanged (this , "sent." );
339
+ pl .progressChanged (this , 100 );
340
+ }
341
+
342
+ @ Override
343
+ protected void setKeysMissingFromDeserialization ()
344
+ {
345
+ super .setKeysMissingFromDeserialization ();
346
+ commandLenQueue = new ArrayDeque <Integer >();
347
+ simpleStreamMode = true ;
348
+ }
349
+
213
350
@ Override
214
351
public Grbl clone ()
215
352
{
0 commit comments