-
Notifications
You must be signed in to change notification settings - Fork 583
Expand file tree
/
Copy pathGCodes2.cpp
More file actions
4325 lines (3968 loc) · 108 KB
/
GCodes2.cpp
File metadata and controls
4325 lines (3968 loc) · 108 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GCodes2.cpp
*
* Created on: 3 Dec 2016
* Author: David
*
* This file contains the code to see what G, M or T command we have and start processing it.
*/
#include "GCodes.h"
#include "GCodeBuffer.h"
#include "GCodeQueue.h"
#include "Heating/Heat.h"
#include "Movement/Move.h"
#include "Network.h"
#include "Scanner.h"
#include "PrintMonitor.h"
#include "RepRap.h"
#include "Tools/Tool.h"
#include "FilamentMonitors/FilamentMonitor.h"
#include "General/IP4String.h"
#include "Movement/StepperDrivers/DriverMode.h"
#include "Version.h"
#if SUPPORT_IOBITS
# include "PortControl.h"
#endif
#if HAS_WIFI_NETWORKING
# include "FirmwareUpdater.h"
#endif
#if SUPPORT_12864_LCD
# include "Display/Display.h"
#endif
#if SUPPORT_DOTSTAR_LED
# include "Fans/DotStarLed.h"
#endif
#include <utility> // for std::swap
// If the code to act on is completed, this returns true, otherwise false.
// It is called repeatedly for a given code until it returns true for that code.
bool GCodes::ActOnCode(GCodeBuffer& gb, const StringRef& reply)
{
// Can we queue this code?
if (gb.CanQueueCodes() && codeQueue->ShouldQueueCode(gb))
{
// Don't queue any GCodes if there are segments not yet picked up by Move, because in the event that a segment corresponds to no movement,
// the move gets discarded, which throws out the count of scheduled moves and hence the synchronisation
if (segmentsLeft != 0)
{
return false;
}
if (codeQueue->QueueCode(gb))
{
HandleReply(gb, GCodeResult::ok, "");
return true;
}
return false; // we should queue this code but we can't, so wait until we can either execute it or queue it
}
switch (gb.GetCommandLetter())
{
case 'G':
if (gb.HasCommandNumber())
{
return HandleGcode(gb, reply);
}
break;
case 'M':
if (gb.HasCommandNumber())
{
return HandleMcode(gb, reply);
}
break;
case 'T':
return HandleTcode(gb, reply);
default:
break;
}
reply.printf("Bad command: %s", gb.Buffer());
HandleReply(gb, GCodeResult::error, reply.c_str());
return true;
}
bool GCodes::HandleGcode(GCodeBuffer& gb, const StringRef& reply)
{
GCodeResult result = GCodeResult::ok;
const int code = gb.GetCommandNumber();
if (simulationMode != 0 && code > 4 && code != 10 && code != 11 && code != 20 && code != 21 && (code < 53 || code > 59) && (code < 90 || code > 92))
{
return true; // we only simulate some gcodes
}
switch (code)
{
case 0: // Rapid move
case 1: // Ordinary move
if (segmentsLeft != 0) // do this check first to avoid locking movement unnecessarily
{
return false;
}
if (!LockMovement(gb))
{
return false;
}
{
const char* err = DoStraightMove(gb, code == 1);
if (err != nullptr)
{
AbortPrint(gb);
gb.SetState(GCodeState::waitingForSpecialMoveToComplete, err); // force the user position to be restored
}
}
break;
case 2: // Clockwise arc
case 3: // Anti clockwise arc
// We only support X and Y axes in these (and optionally Z for corkscrew moves), but you can map them to other axes in the tool definitions
if (segmentsLeft != 0) // do this check first to avoid locking movement unnecessarily
{
return false;
}
if (!LockMovement(gb))
{
return false;
}
{
const char* err = DoArcMove(gb, code == 2);
if (err != nullptr)
{
AbortPrint(gb);
gb.SetState(GCodeState::waitingForSpecialMoveToComplete, err); // force the user position to be restored
}
}
break;
case 4: // Dwell
result = DoDwell(gb);
break;
case 10: // Set/report offsets and temperatures, or retract
{
#if SUPPORT_WORKPLACE_COORDINATES
if (gb.Seen('L'))
{
const uint32_t ival = gb.GetUIValue();
switch (ival)
{
case 1:
result = SetOrReportOffsets(gb, reply); // same as G10 with offsets and no L parameter
break;
case 2:
result = GetSetWorkplaceCoordinates(gb, reply, false);
break;
case 20:
result = GetSetWorkplaceCoordinates(gb, reply, true);
break;
default:
result = GCodeResult::badOrMissingParameter;
break;
}
}
else
#endif
{
bool modifyingTool = gb.Seen('P') || gb.Seen('R') || gb.Seen('S');
for (size_t axis = 0; axis < numVisibleAxes; ++axis)
{
modifyingTool |= gb.Seen(axisLetters[axis]);
}
if (modifyingTool)
{
if (simulationMode != 0)
{
break;
}
result = SetOrReportOffsets(gb, reply);
}
else
{
result = RetractFilament(gb, true);
}
}
}
break;
case 11: // Un-retract
result = RetractFilament(gb, false);
break;
case 20: // Inches (which century are we living in, here?)
distanceScale = InchToMm;
break;
case 21: // mm
distanceScale = 1.0;
break;
case 28: // Home
result = DoHome(gb, reply);
break;
case 29: // Grid-based bed probing
if (!LockMovementAndWaitForStandstill(gb)) // do this first to make sure that a new grid isn't being defined
{
return false;
}
{
const int sparam = (gb.Seen('S')) ? gb.GetIValue() : 0;
switch(sparam)
{
case 0: // probe and save height map
result = ProbeGrid(gb, reply);
break;
case 1: // load height map file
result = LoadHeightMap(gb, reply);
break;
case 2: // clear height map
ClearBedMapping();
break;
default:
result = GCodeResult::badOrMissingParameter;
break;
}
}
break;
case 30: // Z probe/manually set at a position and set that as point P
if (!LockMovementAndWaitForStandstill(gb))
{
return false;
}
if ((reprap.GetMove().GetKinematics().AxesToHomeBeforeProbing() & ~axesHomed) != 0)
{
reply.copy("Insufficient axes homed for bed probing");
result = GCodeResult::error;
}
else
{
result = ExecuteG30(gb, reply);
}
break;
case 31: // Return the probe value, or set probe variables
result = SetPrintZProbe(gb, reply);
break;
case 32: // Probe Z at multiple positions and generate the bed transform
if (!LockMovementAndWaitForStandstill(gb))
{
return false;
}
// We need to unlock the movement system here in case there is no Z probe and we are doing manual probing.
// Otherwise, even though the bed probing code calls UnlockAll when doing a manual bed probe, the movement system
// remains locked because the current MachineState object already held the lock when the macro file was started,
// which means that no gcode source other than the one that executed G32 is allowed to jog the Z axis.
UnlockAll(gb);
DoFileMacro(gb, BED_EQUATION_G, true); // Try to execute bed.g
break;
#if SUPPORT_WORKPLACE_COORDINATES
case 53: // Temporarily use machine coordinates
gb.MachineState().useMachineCoordinates = true;
break;
case 54: // Switch to coordinate system 1
case 55: // Switch to coordinate system 2
case 56: // Switch to coordinate system 3
case 57: // Switch to coordinate system 4
case 58: // Switch to coordinate system 5
case 59: // Switch to coordinate system 6,7,8,9
if (!LockMovementAndWaitForStandstill(gb))
{
return false;
}
{
unsigned int cs = code - 54;
if (code == 59)
{
const int8_t fraction = gb.GetCommandFraction();
if (fraction >= 0)
{
cs += (unsigned int)fraction;
}
}
if (cs < NumCoordinateSystems)
{
currentCoordinateSystem = cs; // this is the zero-base coordinate system number
gb.MachineState().useMachineCoordinates = false;
gb.MachineState().useMachineCoordinatesSticky = false;
ToolOffsetInverseTransform(moveBuffer.coords, currentUserPosition); // update user coordinates
}
else
{
result = GCodeResult::notSupported;
}
}
break;
#endif
case 60: // Save position
result = SavePosition(gb, reply);
break;
case 90: // Absolute coordinates
gb.MachineState().axesRelative = false;
break;
case 91: // Relative coordinates
gb.MachineState().axesRelative = true; // Axis movements (i.e. X, Y and Z)
break;
case 92: // Set position
result = SetPositions(gb);
break;
default:
result = GCodeResult::notSupported;
}
return HandleResult(gb, result, reply, nullptr);
}
bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply)
{
GCodeResult result = GCodeResult::ok;
OutputBuffer *outBuf = nullptr;
const int code = gb.GetCommandNumber();
if ( simulationMode != 0
&& (code < 20 || code > 37)
&& code != 0 && code != 1 && code != 82 && code != 83 && code != 105 && code != 109 && code != 111 && code != 112 && code != 122
&& code != 200 && code != 204 && code != 207 && code != 408 && code != 999)
{
return true; // we don't simulate most M codes
}
switch (code)
{
case 0: // Stop
case 1: // Sleep
// Don't allow M0 or M1 to stop a print, unless the print is paused or the command comes from the file being printed itself.
if (reprap.GetPrintMonitor().IsPrinting() && &gb != fileGCode && !IsPaused())
{
reply.copy("Pause the print before attempting to cancel it");
result = GCodeResult::error;
}
else
{
if ( !LockMovementAndWaitForStandstill(gb) // wait until everything has stopped
|| !IsCodeQueueIdle() // must also wait until deferred command queue has caught up
)
{
return false;
}
const bool wasPaused = isPaused; // isPaused gets cleared by CancelPrint
const bool wasSimulating = IsSimulating(); // simulationMode may get cleared by CancelPrint
StopPrint((&gb == fileGCode) ? StopPrintReason::normalCompletion : StopPrintReason::userCancelled);
if (!wasSimulating) // don't run any macro files or turn heaters off etc. if we were simulating before we stopped the print
{
// If we are cancelling a paused print with M0 and we are homed and cancel.g exists then run it and do nothing else
if (wasPaused && code == 0 && AllAxesAreHomed() && DoFileMacro(gb, CANCEL_G, false))
{
break;
}
gb.SetState((code == 0) ? GCodeState::stopping : GCodeState::sleeping);
DoFileMacro(gb, (code == 0) ? STOP_G : SLEEP_G, false);
}
}
break;
case 3: // Spin spindle clockwise
{
switch (machineType)
{
case MachineType::cnc:
{
const uint32_t slot = gb.Seen('P') ? gb.GetUIValue() : 0;
if (slot >= MaxSpindles)
{
reply.copy("Invalid spindle index");
result = GCodeResult::error;
}
else
{
if (gb.Seen('S'))
{
const float rpm = gb.GetFValue();
platform.AccessSpindle(slot).SetRpm(rpm);
}
platform.AccessSpindle(slot).TurnOn();
}
}
break;
case MachineType::laser:
if (gb.Seen('S')) {
platform.SetLaserPwm(ConvertLaserPwm(gb.GetFValue()));
}
break;
default:
#if SUPPORT_ROLAND
if (gb.Seen('S') && reprap.GetRoland()->Active())
{
result = reprap.GetRoland()->ProcessSpindle(gb.GetFValue());
}
else
#endif
{
result = GCodeResult::notSupportedInCurrentMode;
}
break;
}
}
break;
case 4: // Spin spindle counter clockwise
{
if (machineType == MachineType::cnc)
{
const uint32_t slot = gb.Seen('P') ? gb.GetUIValue() : 0;
if (slot >= MaxSpindles)
{
reply.copy("Invalid spindle index");
result = GCodeResult::error;
}
else
{
if (gb.Seen('S'))
{
const float rpm = gb.GetFValue();
platform.AccessSpindle(slot).SetRpm(-rpm);
}
platform.AccessSpindle(slot).TurnOn();
}
}
else
{
result = GCodeResult::notSupportedInCurrentMode;
}
}
break;
case 5: // Spindle motor off
switch (machineType)
{
case MachineType::cnc:
if (gb.Seen('P'))
{
// Turn off specific spindle
const uint32_t slot = gb.GetUIValue();
if (slot >= MaxSpindles)
{
reply.copy("Invalid spindle index");
result = GCodeResult::error;
}
else
{
platform.AccessSpindle(slot).TurnOff();
}
}
else
{
// Turn off every spindle if no 'P' parameter is present
for (size_t i = 0; i < MaxSpindles; i++)
{
platform.AccessSpindle(i).TurnOff();
}
}
break;
case MachineType::laser:
platform.SetLaserPwm(0);
break;
default:
#if SUPPORT_ROLAND
if (reprap.GetRoland()->Active())
{
result = reprap.GetRoland()->ProcessSpindle(0.0);
}
else
#endif
{
result = GCodeResult::notSupportedInCurrentMode;
}
break;
}
break;
case 18: // Motors off
case 84:
if (!LockMovementAndWaitForStandstill(gb))
{
return false;
}
{
bool seen = false;
for (size_t axis = 0; axis < numTotalAxes; axis++)
{
if (gb.Seen(axisLetters[axis]))
{
SetAxisNotHomed(axis);
platform.DisableDrive(axis);
seen = true;
}
}
if (gb.Seen(extrudeLetter))
{
uint32_t eDrive[MaxExtruders];
size_t eCount = numExtruders;
gb.GetUnsignedArray(eDrive, eCount, false);
for (size_t i = 0; i < eCount; i++)
{
seen = true;
if (eDrive[i] >= numExtruders)
{
reply.printf("Invalid extruder number specified: %" PRIu32, eDrive[i]);
result = GCodeResult::error;
break;
}
platform.DisableDrive(numTotalAxes + eDrive[i]);
}
}
if (gb.Seen('S'))
{
seen = true;
const float idleTimeout = gb.GetFValue();
if (idleTimeout < 0.0)
{
reply.copy("Idle timeouts cannot be negative");
result = GCodeResult::error;
}
else
{
reprap.GetMove().SetIdleTimeout(idleTimeout);
}
}
if (!seen)
{
DisableDrives();
}
}
break;
case 20: // List files on SD card
if (!LockFileSystem(gb)) // don't allow more than one at a time to avoid contention on output buffers
{
return false;
}
{
const int sparam = (gb.Seen('S')) ? gb.GetIValue() : 0;
const unsigned int rparam = (gb.Seen('R')) ? gb.GetUIValue() : 0;
String<MaxFilenameLength> dir;
if (gb.Seen('P'))
{
gb.GetPossiblyQuotedString(dir.GetRef());
}
else
{
dir.copy(platform.GetGCodeDir());
}
if (sparam == 2)
{
outBuf = reprap.GetFilesResponse(dir.c_str(), rparam, true); // send the file list in JSON format
if (outBuf == nullptr)
{
return false;
}
outBuf->cat('\n');
}
else if (sparam == 3)
{
outBuf = reprap.GetFilelistResponse(dir.c_str(), rparam);
if (outBuf == nullptr)
{
return false;
}
outBuf->cat('\n');
}
else
{
if (!OutputBuffer::Allocate(outBuf))
{
return false; // cannot allocate an output buffer, try again later
}
// To mimic the behaviour of the official RepRapPro firmware:
// If we are emulating RepRap then we print "GCode files:\n" at the start, otherwise we don't.
// If we are emulating Marlin and the code came via the serial/USB interface, then we don't put quotes around the names and we separate them with newline;
// otherwise we put quotes around them and separate them with comma.
if (platform.Emulating() == Compatibility::me || platform.Emulating() == Compatibility::reprapFirmware)
{
outBuf->copy("GCode files:\n");
}
bool encapsulateList = ((&gb != serialGCode && &gb != telnetGCode) || !platform.EmulatingMarlin());
FileInfo fileInfo;
if (platform.GetMassStorage()->FindFirst(dir.c_str(), fileInfo))
{
// iterate through all entries and append each file name
do {
if (encapsulateList)
{
outBuf->catf("%c%s%c%c", FILE_LIST_BRACKET, fileInfo.fileName.c_str(), FILE_LIST_BRACKET, FILE_LIST_SEPARATOR);
}
else
{
outBuf->catf("%s\n", fileInfo.fileName.c_str());
}
} while (platform.GetMassStorage()->FindNext(fileInfo));
if (encapsulateList)
{
// remove the last separator
(*outBuf)[outBuf->Length() - 1] = 0;
}
}
else
{
outBuf->cat("NONE\n");
}
}
}
break;
case 21: // Initialise SD card
if (!LockFileSystem(gb)) // don't allow more than one at a time to avoid contention on output buffers
{
return false;
}
{
const size_t card = (gb.Seen('P')) ? gb.GetIValue() : 0;
result = platform.GetMassStorage()->Mount(card, reply, true);
}
break;
case 22: // Release SD card
if (!LockFileSystem(gb)) // don't allow more than one at a time to avoid contention on output buffers
{
return false;
}
{
const size_t card = (gb.Seen('P')) ? gb.GetIValue() : 0;
result = platform.GetMassStorage()->Unmount(card, reply);
}
break;
case 23: // Set file to print
case 32: // Select file and start SD print
// We now allow a file that is being printed to chain to another file. This is required for the resume-after-power-fail functionality.
if (fileGCode->OriginalMachineState().fileState.IsLive() && (&gb) != fileGCode)
{
reply.copy("Cannot set file to print, because a file is already being printed");
result = GCodeResult::error;
break;
}
if (code == 32 && !LockMovementAndWaitForStandstill(gb))
{
return false;
}
{
String<MaxFilenameLength> filename;
if (gb.GetUnprecedentedString(filename.GetRef()))
{
if (QueueFileToPrint(filename.c_str(), reply))
{
reprap.GetPrintMonitor().StartingPrint(filename.c_str());
if (platform.EmulatingMarlin() && (&gb == serialGCode || &gb == telnetGCode))
{
reply.copy("File opened\nFile selected");
}
else
{
// Command came from web interface or PanelDue, or not emulating Marlin, so send a nicer response
reply.printf("File %s selected for printing", filename.c_str());
}
if (code == 32)
{
StartPrinting(true);
}
}
else
{
result = GCodeResult::error;
}
}
else
{
reply.copy("Filename expected");
result = GCodeResult::error;
}
}
break;
case 24: // Print/resume-printing the selected file
if (IsPausing() || IsResuming())
{
// ignore the resume request
}
else
{
if (!LockMovementAndWaitForStandstill(gb))
{
return false;
}
if (IsPaused())
{
#if HAS_VOLTAGE_MONITOR
if (!platform.IsPowerOk())
{
reply.copy("Cannot resume while power voltage is low");
result = GCodeResult::error;
}
else
#endif
{
gb.SetState(GCodeState::resuming1);
if (AllAxesAreHomed())
{
DoFileMacro(gb, RESUME_G, true);
}
}
}
else if (!fileToPrint.IsLive())
{
reply.copy("Cannot print, because no file is selected!");
result = GCodeResult::error;
}
else
{
#if HAS_VOLTAGE_MONITOR
if (!platform.IsPowerOk())
{
reply.copy("Cannot start a print while power voltage is low");
result = GCodeResult::error;
}
else
#endif
{
bool fromStart = (fileOffsetToPrint == 0);
if (!fromStart)
{
// We executed M23 to set the file offset, which normally means that we are executing resurrect.g.
// We need to copy the absolute/relative and volumetric extrusion flags over
fileGCode->OriginalMachineState().drivesRelative = gb.MachineState().drivesRelative;
fileGCode->OriginalMachineState().feedRate = gb.MachineState().feedRate;
fileGCode->OriginalMachineState().volumetricExtrusion = gb.MachineState().volumetricExtrusion;
fileToPrint.Seek(fileOffsetToPrint);
moveFractionToSkip = moveFractionToStartAt;
}
StartPrinting(fromStart);
}
}
}
break;
case 226: // Synchronous pause, normally initiated from within the file being printed
if (!isPaused && !IsPausing())
{
if (gb.IsDoingFileMacro())
{
pausePending = true;
}
else
{
if (!LockMovementAndWaitForStandstill(gb)) // lock movement before calling DoPause, also wait for movement to complete
{
return false;
}
DoPause(gb, PauseReason::gcode, nullptr);
}
}
break;
case 600: // Filament change pause, synchronous
if (!isPaused && !IsPausing())
{
if (gb.IsDoingFileMacro())
{
filamentChangePausePending = true;
}
else
{
if (!LockMovementAndWaitForStandstill(gb)) // lock movement before calling DoPause, also wait for movement to complete
{
return false;
}
DoPause(gb, PauseReason::filamentChange, nullptr);
}
}
break;
case 25: // Pause the print
if (isPaused)
{
reply.copy("Printing is already paused!!");
result = GCodeResult::error;
}
else if (!reprap.GetPrintMonitor().IsPrinting())
{
reply.copy("Cannot pause print, because no file is being printed!");
result = GCodeResult::error;
}
else if (fileGCode->IsDoingFileMacro())
{
pausePending = true;
}
else
{
if (!LockMovement(gb)) // lock movement before calling DoPause
{
return false;
}
DoPause(gb, PauseReason::user, nullptr);
}
break;
case 26: // Set SD position
// This is used between executing M23 to set up the file to print, and M25 to print it
if (gb.Seen('S'))
{
fileOffsetToPrint = (FilePosition)gb.GetUIValue();
if (gb.Seen('P'))
{
moveFractionToStartAt = constrain<float>(gb.GetFValue(), 0.0, 1.0);
}
}
break;
case 27: // Report print status - Deprecated
if (reprap.GetPrintMonitor().IsPrinting())
{
// Pronterface keeps sending M27 commands if "Monitor status" is checked, and it specifically expects the following response syntax
FileData& fileBeingPrinted = fileGCode->OriginalMachineState().fileState;
reply.printf("SD printing byte %lu/%lu", fileBeingPrinted.GetPosition() - fileInput->BytesCached(), fileBeingPrinted.Length());
}
else
{
reply.copy("Not SD printing.");
}
break;
case 28: // Write to file
{
String<MaxFilenameLength> filename;
if (gb.GetUnprecedentedString(filename.GetRef()))
{
const bool ok = gb.OpenFileToWrite(platform.GetGCodeDir(), filename.c_str(), 0, false, 0);
if (ok)
{
reply.printf("Writing to file: %s", filename.c_str());
}
else
{
reply.printf("Can't open file %s for writing.", filename.c_str());
result = GCodeResult::error;
}
}
else
{
reply.copy("Filename expected");
result = GCodeResult::error;
}
}
break;
case 29: // End of file being written; should be intercepted before getting here
reply.copy("GCode end-of-file being interpreted.");
break;
case 30: // Delete file
{
String<MaxFilenameLength> filename;
if (gb.GetUnprecedentedString(filename.GetRef()))
{
platform.GetMassStorage()->Delete(platform.GetGCodeDir(), filename.c_str());
}
else
{
reply.copy("Filename expected");
result = GCodeResult::error;
}
}
break;
// For case 32, see case 23
case 36: // Return file information
if (!LockFileSystem(gb)) // getting file info takes several calls and isn't reentrant
{
return false;
}
{
String<MaxFilenameLength> filename;
const bool gotFilename = gb.GetUnprecedentedString(filename.GetRef());
const bool done = reprap.GetFileInfoResponse((gotFilename) ? filename.c_str() : nullptr, outBuf, false);
if (outBuf != nullptr)
{
outBuf->cat('\n');
}
result = (done) ? GCodeResult::ok : GCodeResult::notFinished;
}
break;
case 37: // Simulation mode on/off, or simulate a whole file
{
bool seen = false;
String<MaxFilenameLength> simFileName;
gb.TryGetPossiblyQuotedString('P', simFileName.GetRef(), seen);
if (seen)
{
const bool updateFile = !gb.Seen('F') || gb.GetUIValue() == 1;
result = SimulateFile(gb, reply, simFileName.GetRef(), updateFile);
}
else
{
uint32_t newSimulationMode;
gb.TryGetUIValue('S', newSimulationMode, seen);
if (seen)
{
result = ChangeSimulationMode(gb, reply, newSimulationMode);
}
else
{
reply.printf("Simulation mode: %s, move time: %.1f sec, other time: %.1f sec",
(simulationMode != 0) ? "on" : "off", (double)reprap.GetMove().GetSimulationTime(), (double)simulationTime);
}
}
}
break;
case 38: // Report SHA1 of file
if (!LockFileSystem(gb)) // getting file hash takes several calls and isn't reentrant
{
return false;
}
if (fileBeingHashed == nullptr)
{
// See if we can open the file and start hashing
String<MaxFilenameLength> filename;
if (gb.GetUnprecedentedString(filename.GetRef()))
{
if (StartHash(filename.c_str()))
{
// Hashing is now in progress...
result = GCodeResult::notFinished;
}
else
{
reply.printf("Cannot open file: %s", filename.c_str());
result = GCodeResult::error;
}
}
else
{
reply.copy("Filename expected");
result = GCodeResult::error;
}
}
else
{
// This can take some time. All the actual heavy lifting is in dedicated methods
result = AdvanceHash(reply);
}
break;