|
@@ -37,13 +37,11 @@
|
37
|
37
|
#include "stepper.h"
|
38
|
38
|
#include "temperature.h"
|
39
|
39
|
#include "motion_control.h"
|
|
40
|
+#include "cardreader.h"
|
40
|
41
|
|
41
|
42
|
|
42
|
43
|
char version_string[] = "1.0.0 Alpha 1";
|
43
|
44
|
|
44
|
|
-#ifdef SDSUPPORT
|
45
|
|
- #include "SdFat.h"
|
46
|
|
-#endif //SDSUPPORT
|
47
|
45
|
|
48
|
46
|
|
49
|
47
|
// look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
|
|
@@ -112,7 +110,11 @@ char version_string[] = "1.0.0 Alpha 1";
|
112
|
110
|
//===========================================================================
|
113
|
111
|
extern float HeaterPower;
|
114
|
112
|
|
115
|
|
-//public variables
|
|
113
|
+
|
|
114
|
+//===========================================================================
|
|
115
|
+//=============================public variables=============================
|
|
116
|
+//===========================================================================
|
|
117
|
+CardReader card;
|
116
|
118
|
float homing_feedrate[] = HOMING_FEEDRATE;
|
117
|
119
|
bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
|
118
|
120
|
volatile int feedmultiply=100; //100->1 200->2
|
|
@@ -138,7 +140,6 @@ static bool relative_mode_e = false; //Determines Absolute or Relative E Codes
|
138
|
140
|
static uint8_t fanpwm=0;
|
139
|
141
|
|
140
|
142
|
|
141
|
|
-// comm variables
|
142
|
143
|
|
143
|
144
|
static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
|
144
|
145
|
static bool fromsd[BUFSIZE];
|
|
@@ -163,140 +164,12 @@ static unsigned long stepper_inactive_time = 0;
|
163
|
164
|
static unsigned long starttime=0;
|
164
|
165
|
static unsigned long stoptime=0;
|
165
|
166
|
|
166
|
|
-#ifdef SDSUPPORT
|
167
|
|
- static Sd2Card card;
|
168
|
|
- static SdVolume volume;
|
169
|
|
- static SdFile root;
|
170
|
|
- static SdFile file;
|
171
|
|
- static uint32_t filesize = 0;
|
172
|
|
- static uint32_t sdpos = 0;
|
173
|
|
- static bool sdmode = false;
|
174
|
|
- static bool sdactive = false;
|
175
|
|
- static bool savetosd = false;
|
176
|
|
- static int16_t n;
|
177
|
|
- static unsigned long autostart_atmillis=0;
|
178
|
|
-
|
179
|
|
- static bool autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
|
180
|
|
-#endif //SDSUPPORT
|
|
167
|
+
|
181
|
168
|
|
182
|
169
|
//===========================================================================
|
183
|
170
|
//=============================ROUTINES=============================
|
184
|
171
|
//===========================================================================
|
185
|
|
-#ifdef SDSUPPORT
|
186
|
|
- void initsd()
|
187
|
|
- {
|
188
|
|
- sdactive = false;
|
189
|
|
- #if SDSS >- 1
|
190
|
|
- if(root.isOpen())
|
191
|
|
- root.close();
|
192
|
|
- if (!card.init(SPI_FULL_SPEED,SDSS))
|
193
|
|
- {
|
194
|
|
- //if (!card.init(SPI_HALF_SPEED,SDSS))
|
195
|
|
- SERIAL_ECHOLN("SD init fail");
|
196
|
|
- }
|
197
|
|
- else if (!volume.init(&card))
|
198
|
|
- {
|
199
|
|
- SERIAL_ERRORLN("volume.init failed");
|
200
|
|
- }
|
201
|
|
- else if (!root.openRoot(&volume))
|
202
|
|
- {
|
203
|
|
- SERIAL_ERRORLN("openRoot failed");
|
204
|
|
- }
|
205
|
|
- else
|
206
|
|
- {
|
207
|
|
- sdactive = true;
|
208
|
|
- SERIAL_ECHOLN("SD card ok");
|
209
|
|
- }
|
210
|
|
- #endif //SDSS
|
211
|
|
- }
|
212
|
172
|
|
213
|
|
- void quickinitsd()
|
214
|
|
- {
|
215
|
|
- sdactive=false;
|
216
|
|
- autostart_atmillis=millis()+5000;
|
217
|
|
- }
|
218
|
|
-
|
219
|
|
- inline void write_command(char *buf)
|
220
|
|
- {
|
221
|
|
- char* begin = buf;
|
222
|
|
- char* npos = 0;
|
223
|
|
- char* end = buf + strlen(buf) - 1;
|
224
|
|
-
|
225
|
|
- file.writeError = false;
|
226
|
|
- if((npos = strchr(buf, 'N')) != NULL)
|
227
|
|
- {
|
228
|
|
- begin = strchr(npos, ' ') + 1;
|
229
|
|
- end = strchr(npos, '*') - 1;
|
230
|
|
- }
|
231
|
|
- end[1] = '\r';
|
232
|
|
- end[2] = '\n';
|
233
|
|
- end[3] = '\0';
|
234
|
|
- file.write(begin);
|
235
|
|
- if (file.writeError)
|
236
|
|
- {
|
237
|
|
- SERIAL_ERRORLN("error writing to file");
|
238
|
|
- }
|
239
|
|
- }
|
240
|
|
-
|
241
|
|
-
|
242
|
|
- void checkautostart(bool force)
|
243
|
|
- {
|
244
|
|
- //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
|
245
|
|
- if(!force)
|
246
|
|
- {
|
247
|
|
- if(!autostart_stilltocheck)
|
248
|
|
- return;
|
249
|
|
- if(autostart_atmillis<millis())
|
250
|
|
- return;
|
251
|
|
- }
|
252
|
|
- autostart_stilltocheck=false;
|
253
|
|
- if(!sdactive)
|
254
|
|
- {
|
255
|
|
- initsd();
|
256
|
|
- if(!sdactive) //fail
|
257
|
|
- return;
|
258
|
|
- }
|
259
|
|
- static int lastnr=0;
|
260
|
|
- char autoname[30];
|
261
|
|
- sprintf(autoname,"auto%i.g",lastnr);
|
262
|
|
- for(int i=0;i<(int)strlen(autoname);i++)
|
263
|
|
- autoname[i]=tolower(autoname[i]);
|
264
|
|
- dir_t p;
|
265
|
|
-
|
266
|
|
- root.rewind();
|
267
|
|
-
|
268
|
|
- bool found=false;
|
269
|
|
- while (root.readDir(p) > 0)
|
270
|
|
- {
|
271
|
|
- for(int i=0;i<(int)strlen((char*)p.name);i++)
|
272
|
|
- p.name[i]=tolower(p.name[i]);
|
273
|
|
- //Serial.print((char*)p.name);
|
274
|
|
- //Serial.print(" ");
|
275
|
|
- //Serial.println(autoname);
|
276
|
|
- if(p.name[9]!='~') //skip safety copies
|
277
|
|
- if(strncmp((char*)p.name,autoname,5)==0)
|
278
|
|
- {
|
279
|
|
- char cmd[30];
|
280
|
|
-
|
281
|
|
- sprintf(cmd,"M23 %s",autoname);
|
282
|
|
- //sprintf(cmd,"M115");
|
283
|
|
- //enquecommand("G92 Z0");
|
284
|
|
- //enquecommand("G1 Z10 F2000");
|
285
|
|
- //enquecommand("G28 X-105 Y-105");
|
286
|
|
- enquecommand(cmd);
|
287
|
|
- enquecommand("M24");
|
288
|
|
- found=true;
|
289
|
|
- }
|
290
|
|
- }
|
291
|
|
- if(!found)
|
292
|
|
- lastnr=-1;
|
293
|
|
- else
|
294
|
|
- lastnr++;
|
295
|
|
- }
|
296
|
|
-#else //NO SD SUPORT
|
297
|
|
- inline void checkautostart(bool x){};
|
298
|
|
-
|
299
|
|
-#endif //SDSUPPORT
|
300
|
173
|
|
301
|
174
|
|
302
|
175
|
//adds an command to the main command buffer
|
|
@@ -331,14 +204,6 @@ void setup()
|
331
|
204
|
axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
|
332
|
205
|
}
|
333
|
206
|
|
334
|
|
- #ifdef SDSUPPORT
|
335
|
|
- //power to SD reader
|
336
|
|
- #if SDPOWER > -1
|
337
|
|
- SET_OUTPUT(SDPOWER);
|
338
|
|
- WRITE(SDPOWER,HIGH);
|
339
|
|
- #endif //SDPOWER
|
340
|
|
- quickinitsd();
|
341
|
|
- #endif //SDSUPPORT
|
342
|
207
|
|
343
|
208
|
plan_init(); // Initialize planner;
|
344
|
209
|
st_init(); // Initialize stepper;
|
|
@@ -350,22 +215,20 @@ void loop()
|
350
|
215
|
{
|
351
|
216
|
if(buflen<3)
|
352
|
217
|
get_command();
|
353
|
|
- checkautostart(false);
|
|
218
|
+ card.checkautostart(false);
|
354
|
219
|
if(buflen)
|
355
|
220
|
{
|
356
|
221
|
#ifdef SDSUPPORT
|
357
|
|
- if(savetosd)
|
|
222
|
+ if(card.savetosd)
|
358
|
223
|
{
|
359
|
224
|
if(strstr(cmdbuffer[bufindr],"M29") == NULL)
|
360
|
225
|
{
|
361
|
|
- write_command(cmdbuffer[bufindr]);
|
|
226
|
+ card.write_command(cmdbuffer[bufindr]);
|
362
|
227
|
Serial.println("ok");
|
363
|
228
|
}
|
364
|
229
|
else
|
365
|
230
|
{
|
366
|
|
- file.sync();
|
367
|
|
- file.close();
|
368
|
|
- savetosd = false;
|
|
231
|
+ card.closefile();
|
369
|
232
|
Serial.println("Done saving file.");
|
370
|
233
|
}
|
371
|
234
|
}
|
|
@@ -455,7 +318,7 @@ inline void get_command()
|
455
|
318
|
case 2:
|
456
|
319
|
case 3:
|
457
|
320
|
#ifdef SDSUPPORT
|
458
|
|
- if(savetosd)
|
|
321
|
+ if(card.savetosd)
|
459
|
322
|
break;
|
460
|
323
|
#endif //SDSUPPORT
|
461
|
324
|
Serial.println("ok");
|
|
@@ -479,17 +342,17 @@ inline void get_command()
|
479
|
342
|
}
|
480
|
343
|
}
|
481
|
344
|
#ifdef SDSUPPORT
|
482
|
|
- if(!sdmode || serial_count!=0){
|
|
345
|
+ if(!card.sdmode || serial_count!=0){
|
483
|
346
|
return;
|
484
|
347
|
}
|
485
|
|
- while( filesize > sdpos && buflen < BUFSIZE) {
|
486
|
|
- n = file.read();
|
|
348
|
+ while( card.filesize > card.sdpos && buflen < BUFSIZE) {
|
|
349
|
+ short n = card.file.read();
|
487
|
350
|
serial_char = (char)n;
|
488
|
351
|
if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1)
|
489
|
352
|
{
|
490
|
|
- sdpos = file.curPosition();
|
491
|
|
- if(sdpos >= filesize){
|
492
|
|
- sdmode = false;
|
|
353
|
+ card.sdpos = card.file.curPosition();
|
|
354
|
+ if(card.sdpos >= card.filesize){
|
|
355
|
+ card.sdmode = false;
|
493
|
356
|
Serial.println("echo: Done printing file");
|
494
|
357
|
stoptime=millis();
|
495
|
358
|
char time[30];
|
|
@@ -500,7 +363,7 @@ inline void get_command()
|
500
|
363
|
sprintf(time,"echo: %i min, %i sec",min,sec);
|
501
|
364
|
Serial.println(time);
|
502
|
365
|
LCD_MESSAGE(time);
|
503
|
|
- checkautostart(true);
|
|
366
|
+ card.checkautostart(true);
|
504
|
367
|
}
|
505
|
368
|
if(!serial_count)
|
506
|
369
|
return; //if empty line
|
|
@@ -702,31 +565,31 @@ inline void process_commands()
|
702
|
565
|
|
703
|
566
|
case 20: // M20 - list SD card
|
704
|
567
|
Serial.println("Begin file list");
|
705
|
|
- root.ls();
|
|
568
|
+ card.root.ls();
|
706
|
569
|
Serial.println("End file list");
|
707
|
570
|
break;
|
708
|
571
|
case 21: // M21 - init SD card
|
709
|
|
- sdmode = false;
|
710
|
|
- initsd();
|
|
572
|
+ card.sdmode = false;
|
|
573
|
+ card.initsd();
|
711
|
574
|
break;
|
712
|
575
|
case 22: //M22 - release SD card
|
713
|
|
- sdmode = false;
|
714
|
|
- sdactive = false;
|
|
576
|
+ card.sdmode = false;
|
|
577
|
+ card.sdactive = false;
|
715
|
578
|
break;
|
716
|
579
|
case 23: //M23 - Select file
|
717
|
|
- if(sdactive){
|
718
|
|
- sdmode = false;
|
719
|
|
- file.close();
|
|
580
|
+ if(card.sdactive){
|
|
581
|
+ card.sdmode = false;
|
|
582
|
+ card.file.close();
|
720
|
583
|
starpos = (strchr(strchr_pointer + 4,'*'));
|
721
|
584
|
if(starpos!=NULL)
|
722
|
585
|
*(starpos-1)='\0';
|
723
|
|
- if (file.open(&root, strchr_pointer + 4, O_READ)) {
|
|
586
|
+ if (card.file.open(&card.root, strchr_pointer + 4, O_READ)) {
|
724
|
587
|
Serial.print("File opened:");
|
725
|
588
|
Serial.print(strchr_pointer + 4);
|
726
|
589
|
Serial.print(" Size:");
|
727
|
|
- Serial.println(file.fileSize());
|
728
|
|
- sdpos = 0;
|
729
|
|
- filesize = file.fileSize();
|
|
590
|
+ Serial.println(card.file.fileSize());
|
|
591
|
+ card.sdpos = 0;
|
|
592
|
+ card.filesize = card.file.fileSize();
|
730
|
593
|
Serial.println("File selected");
|
731
|
594
|
}
|
732
|
595
|
else{
|
|
@@ -735,52 +598,52 @@ inline void process_commands()
|
735
|
598
|
}
|
736
|
599
|
break;
|
737
|
600
|
case 24: //M24 - Start SD print
|
738
|
|
- if(sdactive){
|
739
|
|
- sdmode = true;
|
|
601
|
+ if(card.sdactive){
|
|
602
|
+ card.sdmode = true;
|
740
|
603
|
starttime=millis();
|
741
|
604
|
}
|
742
|
605
|
break;
|
743
|
606
|
case 25: //M25 - Pause SD print
|
744
|
|
- if(sdmode){
|
745
|
|
- sdmode = false;
|
|
607
|
+ if(card.sdmode){
|
|
608
|
+ card.sdmode = false;
|
746
|
609
|
}
|
747
|
610
|
break;
|
748
|
611
|
case 26: //M26 - Set SD index
|
749
|
|
- if(sdactive && code_seen('S')){
|
750
|
|
- sdpos = code_value_long();
|
751
|
|
- file.seekSet(sdpos);
|
|
612
|
+ if(card.sdactive && code_seen('S')){
|
|
613
|
+ card.sdpos = code_value_long();
|
|
614
|
+ card.file.seekSet(card.sdpos);
|
752
|
615
|
}
|
753
|
616
|
break;
|
754
|
617
|
case 27: //M27 - Get SD status
|
755
|
|
- if(sdactive){
|
|
618
|
+ if(card.sdactive){
|
756
|
619
|
Serial.print("SD printing byte ");
|
757
|
|
- Serial.print(sdpos);
|
|
620
|
+ Serial.print(card.sdpos);
|
758
|
621
|
Serial.print("/");
|
759
|
|
- Serial.println(filesize);
|
|
622
|
+ Serial.println(card.filesize);
|
760
|
623
|
}
|
761
|
624
|
else{
|
762
|
625
|
Serial.println("Not SD printing");
|
763
|
626
|
}
|
764
|
627
|
break;
|
765
|
628
|
case 28: //M28 - Start SD write
|
766
|
|
- if(sdactive){
|
|
629
|
+ if(card.sdactive){
|
767
|
630
|
char* npos = 0;
|
768
|
|
- file.close();
|
769
|
|
- sdmode = false;
|
|
631
|
+ card.file.close();
|
|
632
|
+ card.sdmode = false;
|
770
|
633
|
starpos = (strchr(strchr_pointer + 4,'*'));
|
771
|
634
|
if(starpos != NULL){
|
772
|
635
|
npos = strchr(cmdbuffer[bufindr], 'N');
|
773
|
636
|
strchr_pointer = strchr(npos,' ') + 1;
|
774
|
637
|
*(starpos-1) = '\0';
|
775
|
638
|
}
|
776
|
|
- if (!file.open(&root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
|
|
639
|
+ if (!card.file.open(&card.root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
|
777
|
640
|
{
|
778
|
641
|
Serial.print("open failed, File: ");
|
779
|
642
|
Serial.print(strchr_pointer + 4);
|
780
|
643
|
Serial.print(".");
|
781
|
644
|
}
|
782
|
645
|
else{
|
783
|
|
- savetosd = true;
|
|
646
|
+ card.savetosd = true;
|
784
|
647
|
Serial.print("Writing to file: ");
|
785
|
648
|
Serial.println(strchr_pointer + 4);
|
786
|
649
|
}
|