forked from MarlinFirmware/Marlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanycubic_i3mega_lcd.cpp
1034 lines (894 loc) · 36.6 KB
/
anycubic_i3mega_lcd.cpp
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
/**
* anycubic_i3mega_lcd.cpp --- Support for Anycubic i3 Mega TFT
* Created by Christian Hopp on 09.12.17.
* Improved by David Ramiro
* Converted to ExtUI by John BouAntoun 21 June 2020
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../../inc/MarlinConfigPre.h"
#if ENABLED(ANYCUBIC_LCD_I3MEGA)
//#define ANYCUBIC_LCD_DEBUG
#include "anycubic_i3mega_lcd.h"
#include "../ui_api.h"
#include "../../../libs/numtostr.h"
#include "../../../module/stepper.h" // for disable_all_steppers
#include "../../../module/motion.h" // for quickstop_stepper, A20 read printing speed, feedrate_percentage
#include "../../../MarlinCore.h" // for disable_steppers
#include "../../../inc/MarlinConfig.h"
// command sending macro's with debugging capability
#define SEND_PGM(x) send_P(PSTR(x))
#define SENDLINE_PGM(x) sendLine_P(PSTR(x))
#define SEND_PGM_VAL(x,y) (send_P(PSTR(x)), sendLine(i16tostr3rj(y)))
#define SEND(x) send(x)
#define SENDLINE(x) sendLine(x)
#if ENABLED(ANYCUBIC_LCD_DEBUG)
#define SENDLINE_DBG_PGM(x,y) do{ sendLine_P(PSTR(x)); SERIAL_ECHOLNPGM(y); }while(0)
#define SENDLINE_DBG_PGM_VAL(x,y,z) do{ sendLine_P(PSTR(x)); SERIAL_ECHOLNPGM(y, z); }while(0)
#else
#define SENDLINE_DBG_PGM(x,y) sendLine_P(PSTR(x))
#define SENDLINE_DBG_PGM_VAL(x,y,z) sendLine_P(PSTR(x))
#endif
// Append ".gcode" to filename, if requested. Used for some DGUS-clone displays with built-in filter.
// Filenames are limited to 26 characters, so the actual name for the FILENAME can be 20 characters at most.
// If a longer string is desired without "extension, use the ALTNAME macro to provide a (longer) alternative.
#define SPECIAL_MENU_FILENAME(A) A TERN_(ANYCUBIC_LCD_GCODE_EXT, ".gcode")
#define SPECIAL_MENU_ALTNAME(A, B) TERN(ANYCUBIC_LCD_GCODE_EXT, A ".gcode", B)
AnycubicTFTClass AnycubicTFT;
char AnycubicTFTClass::TFTcmdbuffer[TFTBUFSIZE][TFT_MAX_CMD_SIZE];
int AnycubicTFTClass::TFTbuflen = 0,
AnycubicTFTClass::TFTbufindr = 0,
AnycubicTFTClass::TFTbufindw = 0;
char AnycubicTFTClass::serial3_char;
int AnycubicTFTClass::serial3_count = 0;
char* AnycubicTFTClass::TFTstrchr_pointer;
uint8_t AnycubicTFTClass::SpecialMenu = false;
AnycubicMediaPrintState AnycubicTFTClass::mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
AnycubicMediaPauseState AnycubicTFTClass::mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
char AnycubicTFTClass::SelectedDirectory[30];
char AnycubicTFTClass::SelectedFile[FILENAME_LENGTH];
// Serial helpers
static void sendNewLine() { LCD_SERIAL.write('\r'); LCD_SERIAL.write('\n'); }
static void send(const char *str) { LCD_SERIAL.print(str); }
static void send_P(PGM_P str) {
while (const char c = pgm_read_byte(str++))
LCD_SERIAL.write(c);
}
static void sendLine(const char *str) { send(str); sendNewLine(); }
static void sendLine_P(PGM_P str) { send_P(str); sendNewLine(); }
using namespace ExtUI;
AnycubicTFTClass::AnycubicTFTClass() {}
void AnycubicTFTClass::OnSetup() {
#ifndef LCD_BAUDRATE
#define LCD_BAUDRATE 115200
#endif
LCD_SERIAL.begin(LCD_BAUDRATE);
SENDLINE_DBG_PGM("J17", "TFT Serial Debug: Main board reset... J17"); // J17 Main board reset
delay_ms(10);
// Init the state of the key pins running on the TFT
#if BOTH(SDSUPPORT, HAS_SD_DETECT)
SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
SET_INPUT_PULLUP(FIL_RUNOUT1_PIN);
#endif
mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
// DoSDCardStateCheck();
SENDLINE_DBG_PGM("J12", "TFT Serial Debug: Ready... J12"); // J12 Ready
delay_ms(10);
DoFilamentRunoutCheck();
SelectedFile[0] = 0;
#if ENABLED(STARTUP_CHIME)
injectCommands(F("M300 P250 S554\nM300 P250 S554\nM300 P250 S740\nM300 P250 S554\nM300 P250 S740\nM300 P250 S554\nM300 P500 S831"));
#endif
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: Finished startup");
#endif
}
void AnycubicTFTClass::OnCommandScan() {
static millis_t nextStopCheck = 0; // used to slow the stopped print check down to reasonable times
const millis_t ms = millis();
if (ELAPSED(ms, nextStopCheck)) {
nextStopCheck = ms + 1000UL;
if (mediaPrintingState == AMPRINTSTATE_STOP_REQUESTED && IsNozzleHomed()) {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: Finished stopping print, releasing motors ...");
#endif
mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
injectCommands(F("M84\nM27")); // disable stepper motors and force report of SD status
delay_ms(200);
// tell printer to release resources of print to indicate it is done
SENDLINE_DBG_PGM("J14", "TFT Serial Debug: SD Print Stopped... J14");
}
}
if (TFTbuflen < (TFTBUFSIZE - 1))
GetCommandFromTFT();
if (TFTbuflen) {
TFTbuflen = (TFTbuflen - 1);
TFTbufindr = (TFTbufindr + 1) % TFTBUFSIZE;
}
}
void AnycubicTFTClass::OnKillTFT() {
SENDLINE_DBG_PGM("J11", "TFT Serial Debug: Kill command... J11");
}
void AnycubicTFTClass::OnSDCardStateChange(bool isInserted) {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: OnSDCardStateChange event triggered...", isInserted);
#endif
DoSDCardStateCheck();
}
void AnycubicTFTClass::OnSDCardError() {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: OnSDCardError event triggered...");
#endif
SENDLINE_DBG_PGM("J21", "TFT Serial Debug: On SD Card Error ... J21");
}
void AnycubicTFTClass::OnFilamentRunout() {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: FilamentRunout triggered...");
#endif
DoFilamentRunoutCheck();
}
void AnycubicTFTClass::OnUserConfirmRequired(const char * const msg) {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: OnUserConfirmRequired triggered... ", msg);
#endif
#if ENABLED(SDSUPPORT)
/**
* Need to handle the process of following states
* "Nozzle Parked"
* "Load Filament"
* "Filament Purging..."
* "HeaterTimeout"
* "Reheat finished."
*
* NOTE: The only way to handle these states is strcmp_P with the msg unfortunately (very expensive)
*/
if (strcmp_P(msg, PSTR("Nozzle Parked")) == 0) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_PARKED;
// enable continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD print paused done... J18");
}
else if (strcmp_P(msg, PSTR("Load Filament")) == 0) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_FILAMENT_OUT;
// enable continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm Filament is out... J18");
SENDLINE_DBG_PGM("J23", "TFT Serial Debug: UserConfirm Blocking filament prompt... J23");
}
else if (strcmp_P(msg, PSTR("Filament Purging...")) == 0) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_PARKING;
// TODO: JBA I don't think J05 just disables the continue button, i think it injects a rogue M25. So taking this out
// disable continue button
// SENDLINE_DBG_PGM("J05", "TFT Serial Debug: UserConfirm SD Filament Purging... J05"); // J05 printing pause
// enable continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm Filament is purging... J18");
}
else if (strcmp_P(msg, PSTR("HeaterTimeout")) == 0) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_HEATER_TIMEOUT;
// enable continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD Heater timeout... J18");
}
else if (strcmp_P(msg, PSTR("Reheat finished.")) == 0) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_REHEAT_FINISHED;
// enable continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: UserConfirm SD Reheat done... J18");
}
#endif
}
float AnycubicTFTClass::CodeValue() {
return (strtod(&TFTcmdbuffer[TFTbufindr][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindr] + 1], nullptr));
}
bool AnycubicTFTClass::CodeSeen(char code) {
TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindr], code);
return !!TFTstrchr_pointer; // Return True if a character was found
}
bool AnycubicTFTClass::IsNozzleHomed() {
const float xPosition = getAxisPosition_mm((axis_t) X);
const float yPosition = getAxisPosition_mm((axis_t) Y);
return WITHIN(xPosition, X_MIN_POS - 0.1, X_MIN_POS + 0.1) &&
WITHIN(yPosition, Y_MIN_POS - 0.1, Y_MIN_POS + 0.1);
}
void AnycubicTFTClass::HandleSpecialMenu() {
/**
* NOTE: that the file selection command actual lowercases the entire selected file/foldername, so charracter comparisons need to be lowercase.
*/
if (SelectedDirectory[0] == '<') {
switch (SelectedDirectory[1]) {
case 'e': // "<exit>"
SpecialMenu = false;
return;
break;
#if ENABLED(PROBE_MANUALLY)
case '0':
switch (SelectedDirectory[2]) {
case '1': // "<01ZUp0.1>"
SERIAL_ECHOLNPGM("Special Menu: Z Up 0.1");
injectCommands(F("G91\nG1 Z+0.1\nG90"));
break;
case '2': // "<02ZUp0.02>"
SERIAL_ECHOLNPGM("Special Menu: Z Up 0.02");
injectCommands(F("G91\nG1 Z+0.02\nG90"));
break;
case '3': // "<03ZDn0.02>"
SERIAL_ECHOLNPGM("Special Menu: Z Down 0.02");
injectCommands(F("G91\nG1 Z-0.02\nG90"));
break;
case '4': // "<04ZDn0.1>"
SERIAL_ECHOLNPGM("Special Menu: Z Down 0.1");
injectCommands(F("G91\nG1 Z-0.1\nG90"));
break;
case '5': // "<05PrehtBed>"
SERIAL_ECHOLNPGM("Special Menu: Preheat Bed");
injectCommands(F("M140 S65"));
break;
case '6': // "<06SMeshLvl>"
SERIAL_ECHOLNPGM("Special Menu: Start Mesh Leveling");
injectCommands(F("G29S1"));
break;
case '7': // "<07MeshNPnt>"
SERIAL_ECHOLNPGM("Special Menu: Next Mesh Point");
injectCommands(F("G29S2"));
break;
case '8': // "<08HtEndPID>"
SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotend PID");
// need to dwell for half a second to give the fan a chance to start before the pid tuning starts
injectCommands(F("M106 S204\nG4 P500\nM303 E0 S215 C15 U1"));
break;
case '9': // "<09HtBedPID>"
SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotbed Pid");
injectCommands(F("M303 E-1 S65 C6 U1"));
break;
default:
break;
}
break;
case '1':
switch (SelectedDirectory[2]) {
case '0': // "<10FWDeflts>"
SERIAL_ECHOLNPGM("Special Menu: Load FW Defaults");
injectCommands(F("M502\nM300 P105 S1661\nM300 P210 S1108"));
break;
case '1': // "<11SvEEPROM>"
SERIAL_ECHOLNPGM("Special Menu: Save EEPROM");
injectCommands(F("M500\nM300 P105 S1108\nM300 P210 S1661"));
break;
default:
break;
}
break;
#else // if ENABLED(PROBE_MANUALLY)
case '0':
switch (SelectedDirectory[2]) {
case '1': // "<01PrehtBed>"
SERIAL_ECHOLNPGM("Special Menu: Preheat Bed");
injectCommands(F("M140 S65"));
break;
case '2': // "<02ABL>"
SERIAL_ECHOLNPGM("Special Menu: Auto Bed Leveling");
injectCommands(F("G29N"));
break;
case '3': // "<03HtendPID>"
SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotend PID");
// need to dwell for half a second to give the fan a chance to start before the pid tuning starts
injectCommands(F("M106 S204\nG4 P500\nM303 E0 S215 C15 U1"));
break;
case '4': // "<04HtbedPID>"
SERIAL_ECHOLNPGM("Special Menu: Auto Tune Hotbed Pid");
injectCommands(F("M303 E-1 S65 C6 U1"));
break;
case '5': // "<05FWDeflts>"
SERIAL_ECHOLNPGM("Special Menu: Load FW Defaults");
injectCommands(F("M502\nM300 P105 S1661\nM300 P210 S1108"));
break;
case '6': // "<06SvEEPROM>"
SERIAL_ECHOLNPGM("Special Menu: Save EEPROM");
injectCommands(F("M500\nM300 P105 S1108\nM300 P210 S1661"));
break;
case '7': // <07SendM108>
SERIAL_ECHOLNPGM("Special Menu: Send User Confirmation");
injectCommands(F("M108"));
break;
default:
break;
}
break;
#endif // PROBE_MANUALLY
default:
break;
}
#if ENABLED(ANYCUBIC_LCD_DEBUG)
}
else {
SERIAL_ECHOPGM("TFT Serial Debug: Attempted to HandleSpecialMenu on non-special menu... ");
SERIAL_ECHOLN(SelectedDirectory);
#endif
}
}
void AnycubicTFTClass::RenderCurrentFileList() {
#if ENABLED(SDSUPPORT)
uint16_t selectedNumber = 0;
SelectedDirectory[0] = 0;
SelectedFile[0] = 0;
FileList currentFileList;
SENDLINE_PGM("FN "); // Filelist start
if (!isMediaInserted() && !SpecialMenu) {
SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to render Current File List... J02");
SENDLINE_PGM("<SPECI~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Special Menu>"));
}
else {
if (CodeSeen('S'))
selectedNumber = CodeValue();
if (SpecialMenu)
RenderSpecialMenu(selectedNumber);
else if (selectedNumber <= currentFileList.count())
RenderCurrentFolder(selectedNumber);
}
SENDLINE_PGM("END"); // Filelist stop
#endif // SDSUPPORT
}
void AnycubicTFTClass::RenderSpecialMenu(uint16_t selectedNumber) {
switch (selectedNumber) {
#if ENABLED(PROBE_MANUALLY)
case 0: // First Page
SENDLINE_PGM("<01ZUP~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Z Up 0.1>"));
SENDLINE_PGM("<02ZUP~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Z Up 0.02>"));
SENDLINE_PGM("<03ZDO~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Z Down 0.02>"));
SENDLINE_PGM("<04ZDO~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Z Down 0.1>"));
break;
case 4: // Second Page
SENDLINE_PGM("<05PRE~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Preheat Bed>"));
SENDLINE_PGM("<06MES~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_ALTNAME("<Mesh Leveling>", "<Start Mesh Leveling>"));
SENDLINE_PGM("<07NEX~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Next Mesh Point>"));
SENDLINE_PGM("<08PID~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<PID Tune Hotend>"));
break;
case 8: // Third Page
SENDLINE_PGM("<09PID~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<PID Tune Hotbed>"));
SENDLINE_PGM("<10FWD~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Load FW Defaults>"));
SENDLINE_PGM("<11SAV~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Save EEPROM>"));
SENDLINE_PGM("<EXIT_~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Exit>"));
break;
#else
case 0: // First Page
SENDLINE_PGM("<01PRE~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Preheat Bed>"));
SENDLINE_PGM("<02ABL~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Auto Bed Leveling>"));
SENDLINE_PGM("<03PID~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_ALTNAME("<PID Tune Hotend>", "<Auto Tune Hotend PID>"));
SENDLINE_PGM("<04PID~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_ALTNAME("<PID Tune Hotbed>", "<Auto Tune Hotbed PID>"));
break;
case 4: // Second Page
SENDLINE_PGM("<05FWD~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Load FW Defaults>"));
SENDLINE_PGM("<06SAV~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Save EEPROM>"));
SENDLINE_PGM("<06SEN~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_ALTNAME("<User Confirmation>", "<Send User Confirmation>"));
SENDLINE_PGM("<EXIT_~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Exit>"));
break;
#endif // PROBE_MANUALLY
default:
break;
}
}
void AnycubicTFTClass::RenderCurrentFolder(uint16_t selectedNumber) {
FileList currentFileList;
uint16_t cnt = selectedNumber;
uint16_t max_files;
uint16_t dir_files = currentFileList.count();
if ((dir_files - selectedNumber) < 4)
max_files = dir_files;
else
max_files = selectedNumber + 3;
for (cnt = selectedNumber; cnt <= max_files; cnt++) {
if (cnt == 0) { // Special Entry
if (currentFileList.isAtRootDir()) {
SENDLINE_PGM("<SPECI~1.GCO");
SENDLINE_PGM(SPECIAL_MENU_FILENAME("<Special Menu>"));
}
else {
SENDLINE_PGM("/..");
SENDLINE_PGM("/..");
}
}
else {
currentFileList.seek(cnt - 1, false);
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLN(currentFileList.filename());
#endif
if (currentFileList.isDir()) {
SEND_PGM("/");
SENDLINE(currentFileList.shortFilename());
SEND_PGM("/");
SENDLINE(currentFileList.filename());
}
else {
SENDLINE(currentFileList.shortFilename());
SENDLINE(currentFileList.filename());
}
}
}
}
void AnycubicTFTClass::OnPrintTimerStarted() {
#if ENABLED(SDSUPPORT)
if (mediaPrintingState == AMPRINTSTATE_PRINTING)
SENDLINE_DBG_PGM("J04", "TFT Serial Debug: Starting SD Print... J04"); // J04 Starting Print
#endif
}
void AnycubicTFTClass::OnPrintTimerPaused() {
#if ENABLED(SDSUPPORT)
if (isPrintingFromMedia()) {
mediaPrintingState = AMPRINTSTATE_PAUSED;
mediaPauseState = AMPAUSESTATE_PARKING;
}
#endif
}
void AnycubicTFTClass::OnPrintTimerStopped() {
#if ENABLED(SDSUPPORT)
if (mediaPrintingState == AMPRINTSTATE_PRINTING) {
mediaPrintingState = AMPRINTSTATE_NOT_PRINTING;
mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
SENDLINE_DBG_PGM("J14", "TFT Serial Debug: SD Print Completed... J14");
}
// otherwise it was stopped by the printer so don't send print completed signal to TFT
#endif
}
#define ROUND(val) int((val)+0.5f)
void AnycubicTFTClass::GetCommandFromTFT() {
char *starpos = nullptr;
while (LCD_SERIAL.available() > 0 && TFTbuflen < TFTBUFSIZE) {
serial3_char = LCD_SERIAL.read();
if (serial3_char == '\n' ||
serial3_char == '\r' ||
serial3_char == ':' ||
serial3_count >= (TFT_MAX_CMD_SIZE - 1)
) {
if (!serial3_count) return; // if empty line
TFTcmdbuffer[TFTbufindw][serial3_count] = 0; // terminate string
if ((strchr(TFTcmdbuffer[TFTbufindw], 'A') != nullptr)) {
int16_t a_command;
TFTstrchr_pointer = strchr(TFTcmdbuffer[TFTbufindw], 'A');
a_command = ((int)((strtod(&TFTcmdbuffer[TFTbufindw][TFTstrchr_pointer - TFTcmdbuffer[TFTbufindw] + 1], nullptr))));
#if ENABLED(ANYCUBIC_LCD_DEBUG)
if ((a_command > 7) && (a_command != 20)) // No debugging of status polls, please!
SERIAL_ECHOLNPGM("TFT Serial Command: ", TFTcmdbuffer[TFTbufindw]);
#endif
switch (a_command) {
case 0: { // A0 GET HOTEND TEMP
const celsius_float_t hotendActualTemp = getActualTemp_celsius(E0);
SEND_PGM_VAL("A0V ", ROUND(hotendActualTemp));
}
break;
case 1: { // A1 GET HOTEND TARGET TEMP
const celsius_float_t hotendTargetTemp = getTargetTemp_celsius(E0);
SEND_PGM_VAL("A1V ", ROUND(hotendTargetTemp));
}
break;
case 2: { // A2 GET HOTBED TEMP
const celsius_float_t heatedBedActualTemp = getActualTemp_celsius(BED);
SEND_PGM_VAL("A2V ", ROUND(heatedBedActualTemp));
}
break;
case 3: { // A3 GET HOTBED TARGET TEMP
const celsius_float_t heatedBedTargetTemp = getTargetTemp_celsius(BED);
SEND_PGM_VAL("A3V ", ROUND(heatedBedTargetTemp));
} break;
case 4: { // A4 GET FAN SPEED
SEND_PGM_VAL("A4V ", int(getActualFan_percent(FAN0)));
} break;
case 5: { // A5 GET CURRENT COORDINATE
const float xPosition = getAxisPosition_mm(X),
yPosition = getAxisPosition_mm(Y),
zPosition = getAxisPosition_mm(Z);
SEND_PGM("A5V X: "); LCD_SERIAL.print(xPosition);
SEND_PGM( " Y: "); LCD_SERIAL.print(yPosition);
SEND_PGM( " Z: "); LCD_SERIAL.print(zPosition);
SENDLINE_PGM("");
} break;
case 6: // A6 GET SD CARD PRINTING STATUS
#if ENABLED(SDSUPPORT)
if (isPrintingFromMedia()) {
SEND_PGM("A6V ");
if (isMediaInserted())
SENDLINE(ui8tostr3rj(getProgress_percent()));
else
SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to return printing status... J02");
}
else
SENDLINE_PGM("A6V ---");
#endif
break;
case 7: { // A7 GET PRINTING TIME
const uint32_t elapsedSeconds = getProgress_seconds_elapsed();
SEND_PGM("A7V ");
if (elapsedSeconds != 0) { // print time
const uint32_t elapsedMinutes = elapsedSeconds / 60;
SEND(ui8tostr2(elapsedMinutes / 60));
SEND_PGM(" H ");
SEND(ui8tostr2(elapsedMinutes % 60));
SENDLINE_PGM(" M");
}
else
SENDLINE_PGM(" 999:999");
}
break;
case 8: // A8 GET SD LIST
#if ENABLED(SDSUPPORT)
SelectedFile[0] = 0;
RenderCurrentFileList();
#endif
break;
case 9: // A9 pause sd print
#if ENABLED(SDSUPPORT)
if (isPrintingFromMedia())
PausePrint();
#endif
break;
case 10: // A10 resume sd print
#if ENABLED(SDSUPPORT)
if (isPrintingFromMediaPaused())
ResumePrint();
#endif
break;
case 11: // A11 STOP SD PRINT
TERN_(SDSUPPORT, StopPrint());
break;
case 12: // A12 kill
kill(F(STR_ERR_KILLED));
break;
case 13: // A13 SELECTION FILE
#if ENABLED(SDSUPPORT)
if (isMediaInserted()) {
starpos = (strchr(TFTstrchr_pointer + 4, '*'));
if (TFTstrchr_pointer[4] == '/') {
strcpy(SelectedDirectory, TFTstrchr_pointer + 5);
SelectedFile[0] = 0;
SENDLINE_DBG_PGM("J21", "TFT Serial Debug: Clear file selection... J21 "); // J21 Not File Selected
SENDLINE_PGM("");
}
else if (TFTstrchr_pointer[4] == '<') {
strcpy(SelectedDirectory, TFTstrchr_pointer + 4);
SpecialMenu = true;
SelectedFile[0] = 0;
SENDLINE_DBG_PGM("J21", "TFT Serial Debug: Clear file selection... J21 "); // J21 Not File Selected
SENDLINE_PGM("");
}
else {
SelectedDirectory[0] = 0;
if (starpos) *(starpos - 1) = '\0';
strcpy(SelectedFile, TFTstrchr_pointer + 4);
SENDLINE_DBG_PGM_VAL("J20", "TFT Serial Debug: File Selected... J20 ", SelectedFile); // J20 File Selected
}
}
#endif
break;
case 14: // A14 START PRINTING
#if ENABLED(SDSUPPORT)
if (!isPrinting() && strlen(SelectedFile) > 0)
StartPrint();
#endif
break;
case 15: // A15 RESUMING FROM OUTAGE
// TODO: JBA implement resume form outage
break;
case 16: { // A16 set hotend temp
uint16_t tempvalue;
if (CodeSeen('S')) {
tempvalue = constrain(CodeValue(), 0, 275);
setTargetTemp_celsius(tempvalue, (extruder_t)E0);
}
else if (CodeSeen('C') && !isPrinting()) {
if (getAxisPosition_mm(Z) < 10)
injectCommands(F("G1 Z10")); // RASE Z AXIS
tempvalue = constrain(CodeValue(), 0, 275);
setTargetTemp_celsius(tempvalue, (extruder_t)E0);
}
}
break;
case 17: { // A17 set heated bed temp
uint16_t tempbed;
if (CodeSeen('S')) {
tempbed = constrain(CodeValue(), 0, 100);
setTargetTemp_celsius(tempbed, (heater_t)BED);
}
}
break;
case 18: { // A18 set fan speed
float fanPercent;
if (CodeSeen('S')) {
fanPercent = CodeValue();
fanPercent = constrain(fanPercent, 0, 100);
setTargetFan_percent(fanPercent, FAN0);
}
else
fanPercent = 100;
setTargetFan_percent(fanPercent, FAN0);
SENDLINE_PGM("");
}
break;
case 19: // A19 stop stepper drivers - sent on stop extrude command and on turn motors off command
if (!isPrinting()) {
quickstop_stepper();
stepper.disable_all_steppers();
}
SENDLINE_PGM("");
break;
case 20: // A20 read printing speed
if (CodeSeen('S'))
feedrate_percentage = constrain(CodeValue(), 40, 999);
else
SEND_PGM_VAL("A20V ", feedrate_percentage);
break;
case 21: // A21 all home
if (!isPrinting() && !isPrintingFromMediaPaused()) {
if (CodeSeen('X') || CodeSeen('Y') || CodeSeen('Z')) {
if (CodeSeen('X'))
injectCommands(F("G28X"));
if (CodeSeen('Y'))
injectCommands(F("G28Y"));
if (CodeSeen('Z'))
injectCommands(F("G28Z"));
}
else if (CodeSeen('C')) {
injectCommands_P(G28_STR);
}
}
break;
case 22: // A22 move X/Y/Z or extrude
if (!isPrinting()) {
float coorvalue;
uint16_t movespeed = 0;
char commandStr[30];
char fullCommandStr[38];
commandStr[0] = 0; // empty string
if (CodeSeen('F')) // Set feedrate
movespeed = CodeValue();
if (CodeSeen('X')) { // Move in X direction
coorvalue = CodeValue();
if ((coorvalue <= 0.2) && coorvalue > 0)
sprintf_P(commandStr, PSTR("G1 X0.1F%i"), movespeed);
else if ((coorvalue <= -0.1) && coorvalue > -1)
sprintf_P(commandStr, PSTR("G1 X-0.1F%i"), movespeed);
else
sprintf_P(commandStr, PSTR("G1 X%iF%i"), int(coorvalue), movespeed);
}
else if (CodeSeen('Y')) { // Move in Y direction
coorvalue = CodeValue();
if ((coorvalue <= 0.2) && coorvalue > 0)
sprintf_P(commandStr, PSTR("G1 Y0.1F%i"), movespeed);
else if ((coorvalue <= -0.1) && coorvalue > -1)
sprintf_P(commandStr, PSTR("G1 Y-0.1F%i"), movespeed);
else
sprintf_P(commandStr, PSTR("G1 Y%iF%i"), int(coorvalue), movespeed);
}
else if (CodeSeen('Z')) { // Move in Z direction
coorvalue = CodeValue();
if ((coorvalue <= 0.2) && coorvalue > 0)
sprintf_P(commandStr, PSTR("G1 Z0.1F%i"), movespeed);
else if ((coorvalue <= -0.1) && coorvalue > -1)
sprintf_P(commandStr, PSTR("G1 Z-0.1F%i"), movespeed);
else
sprintf_P(commandStr, PSTR("G1 Z%iF%i"), int(coorvalue), movespeed);
}
else if (CodeSeen('E')) { // Extrude
coorvalue = CodeValue();
if ((coorvalue <= 0.2) && coorvalue > 0)
sprintf_P(commandStr, PSTR("G1 E0.1F%i"), movespeed);
else if ((coorvalue <= -0.1) && coorvalue > -1)
sprintf_P(commandStr, PSTR("G1 E-0.1F%i"), movespeed);
else
sprintf_P(commandStr, PSTR("G1 E%iF500"), int(coorvalue));
}
if (strlen(commandStr) > 0) {
sprintf_P(fullCommandStr, PSTR("G91\n%s\nG90"), commandStr);
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOPGM("TFT Serial Debug: A22 Move final request with gcode... ");
SERIAL_ECHOLN(fullCommandStr);
#endif
injectCommands(fullCommandStr);
}
}
SENDLINE_PGM("");
break;
case 23: // A23 preheat pla
if (!isPrinting()) {
if (getAxisPosition_mm(Z) < 10)
injectCommands(F("G1 Z10")); // RASE Z AXIS
setTargetTemp_celsius(PREHEAT_1_TEMP_BED, (heater_t)BED);
setTargetTemp_celsius(PREHEAT_1_TEMP_HOTEND, (extruder_t)E0);
SENDLINE_PGM("OK");
}
break;
case 24:// A24 preheat abs
if (!isPrinting()) {
if (getAxisPosition_mm(Z) < 10)
injectCommands(F("G1 Z10")); // RASE Z AXIS
setTargetTemp_celsius(PREHEAT_2_TEMP_BED, (heater_t)BED);
setTargetTemp_celsius(PREHEAT_2_TEMP_HOTEND, (extruder_t)E0);
SENDLINE_PGM("OK");
}
break;
case 25: // A25 cool down
if (!isPrinting()) {
setTargetTemp_celsius(0, (heater_t) BED);
setTargetTemp_celsius(0, (extruder_t) E0);
SENDLINE_DBG_PGM("J12", "TFT Serial Debug: Cooling down... J12"); // J12 cool down
}
break;
case 26: // A26 refresh SD
#if ENABLED(SDSUPPORT)
if (isMediaInserted()) {
if (strlen(SelectedDirectory) > 0) {
FileList currentFileList;
if ((SelectedDirectory[0] == '.') && (SelectedDirectory[1] == '.')) {
currentFileList.upDir();
}
else {
if (SelectedDirectory[0] == '<')
HandleSpecialMenu();
else
currentFileList.changeDir(SelectedDirectory);
}
}
}
else {
SENDLINE_DBG_PGM("J02", "TFT Serial Debug: No SD Card mounted to refresh SD A26... J02");
}
SelectedDirectory[0] = 0;
#endif
break;
#if ENABLED(SERVO_ENDSTOPS)
case 27: break; // A27 servos angles adjust
#endif
case 28: // A28 filament test
if (CodeSeen('O'))
NOOP;
else if (CodeSeen('C'))
NOOP;
SENDLINE_PGM("");
break;
case 33: // A33 get version info
SEND_PGM("J33 ");
SENDLINE_PGM(DETAILED_BUILD_VERSION);
break;
default:
break;
}
}
TFTbufindw = (TFTbufindw + 1) % TFTBUFSIZE;
TFTbuflen += 1;
serial3_count = 0; // clear buffer
}
else {
TFTcmdbuffer[TFTbufindw][serial3_count++] = serial3_char;
}
}
}
void AnycubicTFTClass::DoSDCardStateCheck() {
#if BOTH(SDSUPPORT, HAS_SD_DETECT)
bool isInserted = isMediaInserted();
if (isInserted)
SENDLINE_DBG_PGM("J00", "TFT Serial Debug: SD card state changed... isInserted");
else
SENDLINE_DBG_PGM("J01", "TFT Serial Debug: SD card state changed... !isInserted");
#endif
}
void AnycubicTFTClass::DoFilamentRunoutCheck() {
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
// NOTE: getFilamentRunoutState() only returns the runout state if the job is printing
// we want to actually check the status of the pin here, regardless of printstate
if (READ(FIL_RUNOUT1_PIN)) {
if (mediaPrintingState == AMPRINTSTATE_PRINTING || mediaPrintingState == AMPRINTSTATE_PAUSED || mediaPrintingState == AMPRINTSTATE_PAUSE_REQUESTED) {
// play tone to indicate filament is out
injectCommands(F("\nM300 P200 S1567\nM300 P200 S1174\nM300 P200 S1567\nM300 P200 S1174\nM300 P2000 S1567"));
// tell the user that the filament has run out and wait
SENDLINE_DBG_PGM("J23", "TFT Serial Debug: Blocking filament prompt... J23");
}
else {
SENDLINE_DBG_PGM("J15", "TFT Serial Debug: Non blocking filament runout... J15");
}
}
#endif // FILAMENT_RUNOUT_SENSOR
}
void AnycubicTFTClass::StartPrint() {
#if ENABLED(SDSUPPORT)
if (!isPrinting() && strlen(SelectedFile) > 0) {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOPGM("TFT Serial Debug: About to print file ... ");
SERIAL_ECHO(isPrinting());
SERIAL_ECHOPGM(" ");
SERIAL_ECHOLN(SelectedFile);
#endif
mediaPrintingState = AMPRINTSTATE_PRINTING;
mediaPauseState = AMPAUSESTATE_NOT_PAUSED;
printFile(SelectedFile);
}
#endif // SDUPPORT
}
void AnycubicTFTClass::PausePrint() {
#if ENABLED(SDSUPPORT)
if (isPrintingFromMedia() && mediaPrintingState != AMPRINTSTATE_STOP_REQUESTED && mediaPauseState == AMPAUSESTATE_NOT_PAUSED) {
mediaPrintingState = AMPRINTSTATE_PAUSE_REQUESTED;
mediaPauseState = AMPAUSESTATE_NOT_PAUSED; // need the userconfirm method to update pause state
SENDLINE_DBG_PGM("J05", "TFT Serial Debug: SD print pause started... J05"); // J05 printing pause
// for some reason pausing the print doesn't retract the extruder so force a manual one here
injectCommands(F("G91\nG1 E-2 F1800\nG90"));
pausePrint();
}
#endif
}
void AnycubicTFTClass::ResumePrint() {
#if ENABLED(SDSUPPORT)
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
if (READ(FIL_RUNOUT1_PIN)) {
#if ENABLED(ANYCUBIC_LCD_DEBUG)
SERIAL_ECHOLNPGM("TFT Serial Debug: Resume Print with filament sensor still tripped... ");
#endif
// trigger the user message box
DoFilamentRunoutCheck();
// re-enable the continue button
SENDLINE_DBG_PGM("J18", "TFT Serial Debug: Resume Print with filament sensor still tripped... J18");
return;
}