-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMarlin_main.cpp
executable file
·4744 lines (3882 loc) · 117 KB
/
Marlin_main.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
/* -*- c++ -*- */
/*
Reprap firmware based on Sprinter and grbl.
Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
This firmware is a mashup between Sprinter and grbl.
(https://github.com/kliment/Sprinter)
(https://github.com/simen/grbl/tree)
It has preliminary support for Matthew Roberts advance algorithm
http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
*/
#include "Marlin.h"
#include "Arduino.h"
#include "ultralcd.h"
#include "planner.h"
#include "stepper.h"
#include "temperature.h"
#include "motion_control.h"
#include "cardreader.h"
#include "watchdog.h"
#include "ConfigurationStore.h"
#include "language.h"
#include "pins_arduino.h"
#include "Marlin_RFID_HWSerial2.h"
//#include "SL032.h"
#include "RFID_ZIM.h"
#if DIGIPOTSS_PIN > -1
#include <SPI.h>
#endif
#define VERSION_STRING "1.1.0.15"
//Stepper Movement Variables
//===========================================================================
//=============================imported variables============================
//===========================================================================
//===========================================================================
//=============================public variables=============================
//===========================================================================
#ifdef SDSUPPORT
CardReader card;
#endif
float homing_feedrate[] = HOMING_FEEDRATE;
bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
int feedmultiply=100; //100->1 200->2
int saved_feedmultiply;
int extrudemultiply=100; //100->1 200->2
//DIY add multi-extruder extrusion multiply support
int extruder_multiply[EXTRUDERS] = {100
#if EXTRUDERS > 1
, 100
// #if EXTRUDERS > 2
// , 100
// #endif
#endif
};
//DIY end - PNI
float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
float Pause_current_position[NUM_STOP_PARAMETERS] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
float add_homeing[3]={0,0,0};
float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
// Extruder offset, only in XY plane
#if EXTRUDERS > 1
float extruder_offset[2][EXTRUDERS] = {
#if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
#endif
};
#endif
float extruders_offset_changement[2]={0,0};
uint8_t active_extruder = 0;
// bool unloading_critical_state = false;
bool unloading_command = false;
//bool Commande_cartridge = false;
int fanSpeed=0;
#ifdef FWRETRACT
bool autoretract_enabled=true;
bool retracted=false;
float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
float retract_recover_length=0, retract_recover_feedrate=8*60;
#endif
//===========================================================================
//=============================private variables=============================
//===========================================================================
const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
static float offset[3] = {0.0, 0.0, 0.0};
static bool home_all_axis = true;
static float feedrate = 1500.0, next_feedrate, saved_feedrate;
static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
static bool relative_mode = false; //Determines Absolute or Relative Coordinates
static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
static bool fromsd[BUFSIZE];
static int bufindr = 0;
static int bufindw = 0;
static int buflen = 0;
//static int i = 0;
static char serial_char;
static int serial_count = 0;
static boolean comment_mode = false;
static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
static char *name_fichier_supprimer;
const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
//static float tt = 0;
//static float bt = 0;
//Inactivity shutdown variables
static unsigned long previous_millis_cmd = 0;
// The two variable for the button
static unsigned long Read_button_1 = 0;
static unsigned long Read_button_2 = 0;
static unsigned long max_inactive_time = 0;
static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME * 1000l;
static unsigned long extruder_inactive_time = DEFAULT_EXTRUDER_DEACTIVE_TIME * 1000l;
static unsigned long max_time_pushing = MAX_TIME_PUSHING * 1000;
unsigned long starttime=0;
unsigned long stoptime=0;
static uint8_t tmp_extruder;
bool Stopped=false;
////////////////////////////// Etat des LED ///////////////////////////////////////////////
bool Strip_Led_State = false;
bool Top_Led_State = false;
bool Antenna_RFID_State = false;
////////////////////////////// FIN ////////////////////////////////////////////////////////////////
///////////////////////// fonction voyage//////////////////////////////////////////////
bool premier_transport = true;
//////////////////////// FIN fonction voyage //////////////////////////////////////////
////////////////////////////////////Fonction pour le RFID/////////////////////////////////////////////////////
// byte checksum_RFID;
// float RFID_data[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
byte TAG_RFID_E0[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
byte TAG_RFID_E1[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
float Distance_Filament_E0 = 0;
float Distance_Filament_E1 = 0;
float Distance_Consumption_E0 = 0;
float Distance_Consumption_E1 = 0;
bool Is_PVA_E0 = false;
bool Is_PVA_E1 = false;
bool Has_Paused_in_Print = false;
///////////////////////////Fonction pour les deux thermistance/////////////////
uint8_t numero_extruder=0;
float TMP0_Target = 0;
float TMP1_Target = 0;
////////////////////////Variable pour l'affichage des caract?res//////////////////////
bool MSG_SDCARD = false;
bool MSG_PRONTERFACE = false;
//////////////////////Numero de ligne////////////////////////////////////
float numero_ligne_sd_card=0;
float numero_ligne_pronterface=0;
float numero_fichier_sd=-1;
// Activation interruption
// bool Desactiver_interruption = false;
//===========================================================================
//=============================ROUTINES=============================
//===========================================================================
void get_arc_coordinates();
bool setTargetedHotend(int code);
//DIY add define functions here
// void Moving_X(float distance_x, float feedrate_x);
// void Moving_Y(float distance_y, float feedrate_y);
// void Moving_Z(float distance_z, float feedrate_z);
// void Moving_E(float distance_e, float feedrate_e);
void Read_offset_E2PROM();
//DIY end - PNI
void serial_echopair_P(const char *s_P, float v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
void serial_echopair_P(const char *s_P, double v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
void serial_echopair_P(const char *s_P, unsigned long v)
{ serialprintPGM(s_P); SERIAL_ECHO(v); }
extern "C"{
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;
int freeMemory() {
int free_memory;
if((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
}
//adds an command to the main command buffer
//thats really done in a non-safe way.
//needs overworking someday
void enquecommand(const char *cmd)
{
if(buflen < BUFSIZE)
{
//this is dangerous if a mixing of serial and this happsens
strcpy(&(cmdbuffer[bufindw][0]),cmd);
SERIAL_ECHO_START;
SERIAL_ECHOPGM("enqueing \"");
SERIAL_ECHO(cmdbuffer[bufindw]);
SERIAL_ECHOLNPGM("\"");
bufindw= (bufindw + 1)%BUFSIZE;
buflen += 1;
}
}
void enquecommand_P(const char *cmd)
{
if(buflen < BUFSIZE)
{
//this is dangerous if a mixing of serial and this happsens
strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
SERIAL_ECHO_START;
SERIAL_ECHOPGM("enqueing \"");
SERIAL_ECHO(cmdbuffer[bufindw]);
SERIAL_ECHOLNPGM("\"");
bufindw= (bufindw + 1)%BUFSIZE;
buflen += 1;
}
}
// DIY functions start
void setup_killpin() {
#if( KILL_PIN>-1 )
pinMode(KILL_PIN,INPUT);
WRITE(KILL_PIN,HIGH);
#endif
}
void setup_photpin() {
#ifdef PHOTOGRAPH_PIN
#if (PHOTOGRAPH_PIN > -1)
SET_OUTPUT(PHOTOGRAPH_PIN);
WRITE(PHOTOGRAPH_PIN, LOW);
#endif
#endif
}
void setup_powerhold() {
#ifdef SUICIDE_PIN
#if (SUICIDE_PIN> -1)
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, HIGH);
#endif
#endif
#if (PS_ON_PIN > -1)
SET_OUTPUT(PS_ON_PIN);
WRITE(PS_ON_PIN, PS_ON_AWAKE);
#endif
}
void suicide_Zim_Setup() {
#if (SUICIDE_Zim_PIN> -1)
SET_OUTPUT(SUICIDE_Zim_PIN);
WRITE(SUICIDE_Zim_PIN, LOW);
#endif
}
void suicide_Zim() {
#if (SUICIDE_Zim_PIN> -1)
WRITE(SUICIDE_Zim_PIN, HIGH);
#endif
}
void setup_green_led() {
#if (Commande_Green> -1)
SET_OUTPUT(Commande_Green);
WRITE(Commande_Green, HIGH);
#endif
}
void declaration_personnal_pin() {
// Fan 2
pinMode(FAN_v2, OUTPUT);
// blue LED
pinMode(LED_blue, OUTPUT);
// right and left StripLed
pinMode(LED_Rampes_droite, OUTPUT);
pinMode(LED_Rampes_gauche, OUTPUT);
//Endstop_voyage
//Atmel
//pinMode(ATMEL_IN_PUSH, INPUT);
#if Endstop_voyage > -1
SET_INPUT(Endstop_voyage);
#ifdef ENDSTOPPULLUP_Endstop_voyage
WRITE(Endstop_voyage,HIGH);
#endif
#endif
#if Endstop_Z_Movement > -1
SET_INPUT(Endstop_Z_Movement);
#ifdef ENDSTOPPULLUP_Endstop_Z_Movement
WRITE(Endstop_Z_Movement,HIGH);
#endif
#endif
#if Endstop_Carre > -1
SET_INPUT(Endstop_Carre);
#ifdef ENDSTOPPULLUP_Endstop_Carre
WRITE(Endstop_Carre,HIGH);
#endif
#endif
}
// DIY functions end
void suicide() {
#ifdef SUICIDE_PIN
#if (SUICIDE_PIN> -1)
SET_OUTPUT(SUICIDE_PIN);
WRITE(SUICIDE_PIN, LOW);
#endif
#endif
}
void setup() {
setup_killpin();
setup_powerhold();
setup_green_led();
suicide_Zim_Setup();
MYSERIAL.begin(BAUDRATE);
//Setup_RFID(); ///////////////////// J'ajoute ma commande au sein du Setup.
MYSERIAL2.begin(BAUDRATE_RFID);
MYSERIAL3.begin(BAUDRATE_RFID);
declaration_personnal_pin();
SERIAL_PROTOCOLLNPGM("start");
SERIAL_ECHO_START;
// Check startup - does nothing if bootloader sets MCUSR to 0
byte mcu = MCUSR;
if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
MCUSR=0;
SERIAL_ECHOPGM(MSG_MARLIN);
SERIAL_ECHOLNPGM(VERSION_STRING);
#ifdef STRING_VERSION_CONFIG_H
#ifdef STRING_CONFIG_H_AUTHOR
SERIAL_ECHO_START;
SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
SERIAL_ECHOPGM(MSG_AUTHOR);
SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
SERIAL_ECHOPGM("Compiled: ");
SERIAL_ECHOLNPGM(__DATE__);
SERIAL_ECHOPGM("\n VERSION : ");
SERIAL_ECHOLNPGM(VERSION_STRING);
#endif
#endif
SERIAL_ECHO_START;
SERIAL_ECHOPGM(MSG_FREE_MEMORY);
SERIAL_ECHO(freeMemory());
SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
for(int8_t i = 0; i < BUFSIZE; i++)
{
fromsd[i] = false;
}
// loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
Config_RetrieveSettings();
tp_init(); // Initialize temperature loop
plan_init(); // Initialize planner;
watchdog_init();
st_init(); // Initialize stepper, this enables interrupts!
setup_photpin();
lcd_init();
#ifdef CONTROLLERFAN_PIN
SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
#endif
#ifdef EXTRUDERFAN_PIN
SET_OUTPUT(EXTRUDERFAN_PIN); //Set pin used for extruder cooling fan
#endif
#ifdef RFID_ZIM
RFID_init();
//envoie_commande(Initialize_Port,length_Initialize_Port,2);
/*
envoie_commande(Set_Antenna_Status_OFF,length_Set_Antenna_Status_OFF,2);
//envoie_commande(Initialize_Port,length_Initialize_Port,3);
envoie_commande(Set_Antenna_Status_OFF,length_Set_Antenna_Status_OFF,3);
Antenna_RFID_State= false;
SERIAL_PROTOCOL("\n");*/
#endif
Read_offset_E2PROM();
SERIAL_PROTOCOL("END_INITIALISATION\n");
}
void loop() {
if(buflen < (BUFSIZE-1))
get_command();
#ifdef SDSUPPORT
card.checkautostart(false);
#endif
if(buflen)
{
#ifdef SDSUPPORT
if(card.saving)
{
if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
{
card.write_command(cmdbuffer[bufindr]);
if(card.logging)
{
process_commands();
}
else
{
SERIAL_PROTOCOL("\n");
SERIAL_PROTOCOLLNPGM(MSG_OK);
}
}
else
{
card.closefile();
SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
SERIAL_PROTOCOLLNPGM(MSG_OK);
}
}
else
{
process_commands();
}
#else
process_commands();
#endif //SDSUPPORT
buflen = (buflen-1);
bufindr = (bufindr + 1)%BUFSIZE;
}
//check heater every n milliseconds
manage_heater();
Read_Power_Button();
CheckTemperatureToActivateFan();
manage_inactivity();
checkHitEndstops();
lcd_update();
Fonction_Transport();
Function_Z_Movement();
Function_Carre();
}
#ifdef RFIDSUPPORT
int cmdPort = -1;
#endif //RFIDSUPPORT
void get_command() {
while( MYSERIAL.available() > 0 && buflen < BUFSIZE)
{
// add RFID support
#ifdef RFIDSUPPORT
if (-1 == cmdPort) {
if (MYSERIAL.available()) {
cmdPort = 0;
}
}
if (0 == cmdPort) {
serial_char = MYSERIAL.read();
}
#else //RFIDSUPPORT
serial_char = MYSERIAL.read();
#endif //RFIDSUPPORT
//DIY here I have re-added messages (just translation, not cleaned - PNI)
if (MSG_PRONTERFACE== true) {
SERIAL_PROTOCOLPGM("I have received this letter: ");
SERIAL_PROTOCOL(serial_char);
SERIAL_PROTOCOLPGM("\n");
}
if (serial_char == '\n') {
numero_ligne_pronterface++;
/* if (fichier_sd==true) {
numero_fichier_sd++;
}
*/
// guess that it's for SD print percentage count which is in deprecation and not in use - PNI
//TODO need clean unused global variable (and why it's a float for integer line number, use unsigned int or long instead for value region)
if(card.saving) {
numero_fichier_sd++;
}
}
//DIY end
if(serial_char == '\n' ||
serial_char == '\r' ||
(serial_char == ':' && comment_mode == false) ||
serial_count >= (MAX_CMD_SIZE - 1) ) {
// RFID support
#ifdef RFIDSUPPORT
cmdPort = -1;
#endif //RFIDSUPPORT
if(!serial_count)
{ //if empty line
comment_mode = false; //for new command
return;
}
cmdbuffer[bufindw][serial_count] = 0; //terminate string
if(!comment_mode)
{
comment_mode = false; //for new command
fromsd[bufindw] = false;
if(strchr(cmdbuffer[bufindw], 'N') != NULL)
{
strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) )
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
SERIAL_ERRORLN(gcode_LastN);
//Serial.println(gcode_N);
FlushSerialRequestResend();
serial_count = 0;
return;
}
if(strchr(cmdbuffer[bufindw], '*') != NULL)
{
byte checksum = 0;
byte count = 0;
while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
strchr_pointer = strchr(cmdbuffer[bufindw], '*');
if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
SERIAL_ERRORLN(gcode_LastN);
FlushSerialRequestResend();
serial_count = 0;
return;
}
//if no errors, continue parsing
}
else
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
SERIAL_ERRORLN(gcode_LastN);
FlushSerialRequestResend();
serial_count = 0;
return;
}
gcode_LastN = gcode_N;
//if no errors, continue parsing
}
else // if we don't receive 'N' but still see '*'
{
if((strchr(cmdbuffer[bufindw], '*') != NULL))
{
SERIAL_ERROR_START;
SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
SERIAL_ERRORLN(gcode_LastN);
serial_count = 0;
return;
}
}
if((strchr(cmdbuffer[bufindw], 'G') != NULL))
{
strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL))))
{
case 0:
case 1:
case 2:
case 3:
if(Stopped == false)
{ // If printer is stopped by an error the G[0-3] codes are ignored.
#ifdef SDSUPPORT
if(card.saving)
break;
#endif //SDSUPPORT
SERIAL_PROTOCOLLNPGM(MSG_OK);
}
else
{
SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
LCD_MESSAGEPGM(MSG_STOPPED);
}
break;
default:
break;
}
}
bufindw = (bufindw + 1)%BUFSIZE;
buflen += 1;
}
serial_count = 0; //clear buffer
}
else
{
if(serial_char == ';')
comment_mode = true;
if(!comment_mode)
cmdbuffer[bufindw][serial_count++] = serial_char;
}
}
#ifdef SDSUPPORT
if(!card.sdprinting || serial_count!=0){
return;
}
while( !card.eof() && buflen < BUFSIZE) {
int16_t n=card.get();
serial_char = (char)n;
//DIY here I have re-added (just translation, not cleaned - PNI)
if (MSG_SDCARD== true) {
SERIAL_PROTOCOLPGM("I have received this letter: ");
SERIAL_PROTOCOL(serial_char);
SERIAL_PROTOCOLPGM("\n");
}
if (serial_char == '\n') {
numero_ligne_sd_card++;
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
//DIY end
if(serial_char == '\n' ||
serial_char == '\r' ||
(serial_char == ':' && comment_mode == false) ||
serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
{
if(card.eof()){
SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
stoptime=millis();
char time[30];
unsigned long t=(stoptime-starttime)/1000;
int hours, minutes;
minutes=(t/60)%60;
hours=t/60/60;
sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
SERIAL_ECHO_START;
SERIAL_ECHOLN(time);
lcd_setstatus(time);
card.printingHasFinished();
card.checkautostart(true);
}
if(!serial_count)
{
comment_mode = false; //for new command
return; //if empty line
}
cmdbuffer[bufindw][serial_count] = 0; //terminate string
// if(!comment_mode){
fromsd[bufindw] = true;
buflen += 1;
bufindw = (bufindw + 1)%BUFSIZE;
// }
comment_mode = false; //for new command
serial_count = 0; //clear buffer
}
else
{
if(serial_char == ';') comment_mode = true;
if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
}
}
#endif //SDSUPPORT
}
float code_value()
{
return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
}
long code_value_long()
{
return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
}
long code_value_long32()
{
return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 32));
}
bool code_seen(char code)
{
strchr_pointer = strchr(cmdbuffer[bufindr], code);
return (strchr_pointer != NULL); //Return True if a character was found
}
#define DEFINE_PGM_READ_ANY(type, reader) \
static inline type pgm_read_any(const type *p) \
{ return pgm_read_##reader##_near(p); }
DEFINE_PGM_READ_ANY(float, float);
DEFINE_PGM_READ_ANY(signed char, byte);
#define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
static const PROGMEM type array##_P[3] = \
{ X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
static inline type array(int axis) \
{ return pgm_read_any(&array##_P[axis]); }
XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
static void axis_is_at_home(int axis) {
current_position[axis] = base_home_pos(axis) + add_homeing[axis];
min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
}
static void homeaxis(int axis) {
#define HOMEAXIS_DO(LETTER) \
((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
if (axis==X_AXIS ? HOMEAXIS_DO(X) :
axis==Y_AXIS ? HOMEAXIS_DO(Y) :
axis==Z_AXIS ? HOMEAXIS_DO(Z) :
0) {
current_position[axis] = 0;
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
destination[axis] = 1.5 * max_length(axis) * home_dir(axis);
feedrate = homing_feedrate[axis];
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
st_synchronize();
current_position[axis] = 0;
plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
destination[axis] = -home_retract_mm(axis) * home_dir(axis);
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
st_synchronize();
destination[axis] = 2*home_retract_mm(axis) * home_dir(axis);
feedrate = homing_feedrate[axis]/2 ;
plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
st_synchronize();
axis_is_at_home(axis);
destination[axis] = current_position[axis];
feedrate = 0.0;
endstops_hit_on_purpose();
}
}
#define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
// start to define DIY function here:
void Message_wait() {
SERIAL_PROTOCOL("wait\n");
}
void SetActiveExtruder(uint8_t new_extruder) {
active_extruder = new_extruder;
extrudemultiply = extruder_multiply[new_extruder];
return;
}
void Moving_X(float distance_x, float feedrate_x) {
destination[X_AXIS] = distance_x + current_position[X_AXIS];
// destination[Y_AXIS] = current_position[Y_AXIS];
// destination[Z_AXIS] = current_position[Z_AXIS];
// destination[E_AXIS] = current_position[E_AXIS];
feedrate = feedrate_x;
prepare_move();
return;
}
void Moving_Y(float distance_y, float feedrate_y) {
destination[Y_AXIS] = distance_y + current_position[Y_AXIS];
// destination[X_AXIS] = current_position[X_AXIS];
// destination[Z_AXIS] = current_position[Z_AXIS];
// destination[E_AXIS] = current_position[E_AXIS];
feedrate = feedrate_y;
prepare_move();
return;
}
FORCE_INLINE void MovingBase_Z(bool relative, float distance_z, float feedrate_z) {
destination[Z_AXIS] = distance_z + relative * current_position[Z_AXIS];
// destination[X_AXIS] = current_position[X_AXIS];
// destination[Y_AXIS] = current_position[Y_AXIS];
// destination[E_AXIS] = current_position[E_AXIS];
feedrate = feedrate_z;
prepare_move();
return;
}
void MovingTo_Z(float distance_z, float feedrate_z) {
MovingBase_Z(false, distance_z, feedrate_z);
return;
}
void Moving_Z(float distance_z, float feedrate_z) {
MovingBase_Z(true, distance_z, feedrate_z);
return;
}
void Moving_E(float distance_e, float feedrate_e) {
destination[E_AXIS] = distance_e + current_position[E_AXIS];
// destination[X_AXIS] = current_position[X_AXIS];
// destination[Y_AXIS] = current_position[Y_AXIS];
// destination[Z_AXIS] = current_position[Z_AXIS];
feedrate = feedrate_e;
prepare_move();
return;
}
FORCE_INLINE void MovingBase_head(bool relative, float distance_x, float distance_y, float feedrate_h) {
destination[X_AXIS] = distance_x + relative * current_position[X_AXIS];
destination[Y_AXIS] = distance_y + relative * current_position[Y_AXIS];
// destination[Z_AXIS] = current_position[Z_AXIS];
// destination[E_AXIS] = current_position[E_AXIS];
feedrate = feedrate_h;
prepare_move();
return;
}
void MovingTo_head(float distance_x, float distance_y, float feedrate_h) {
MovingBase_head(false, distance_x, distance_y, feedrate_h);
return;
}
FORCE_INLINE void Center_head() {
MovingTo_head(75, 75, homing_feedrate[X_AXIS]);
return;
}
void Reverse_20mm() {
float fr_retract = FEEDRATE_EXTRUDE_RETRACT;
uint8_t original_extruder = active_extruder;
// first extruder
if(Distance_Filament_E0 > 0) {
SetActiveExtruder(0);
if (Is_PVA_E0 == true) {
fr_retract = FEEDRATE_EXTRUDE_RETRACT_PVA;
}
Moving_E(-DISTANCE_EXTRUDE_RETRACT, fr_retract);
}
// second extruder
if(Distance_Filament_E1 > 0) {
SetActiveExtruder(1);
fr_retract = FEEDRATE_EXTRUDE_RETRACT;
if (Is_PVA_E1 == true) {
fr_retract = FEEDRATE_EXTRUDE_RETRACT_PVA;
}
Moving_E(-DISTANCE_EXTRUDE_RETRACT, fr_retract);
}
SetActiveExtruder(original_extruder);
return;
}
void Homing_head() {
// Homing
HOMEAXIS(X);
HOMEAXIS(Y);
return;
}
void Moving_X_Y_Z_Voyage() {
float Distance_Load_X = 75;