Browse Source

made cardreader more selfsustained

Bernhard Kubicek 13 years ago
parent
commit
fc0064e525
4 changed files with 148 additions and 99 deletions
  1. 33
    74
      Marlin/Marlin.pde
  2. 24
    12
      Marlin/cardreader.h
  3. 87
    9
      Marlin/cardreader.pde
  4. 4
    4
      Marlin/ultralcd.pde

+ 33
- 74
Marlin/Marlin.pde View File

219
   if(buflen)
219
   if(buflen)
220
   {
220
   {
221
     #ifdef SDSUPPORT
221
     #ifdef SDSUPPORT
222
-      if(card.savetosd)
222
+      if(card.saving)
223
       {
223
       {
224
 	if(strstr(cmdbuffer[bufindr],"M29") == NULL)
224
 	if(strstr(cmdbuffer[bufindr],"M29") == NULL)
225
 	{
225
 	{
318
           case 2:
318
           case 2:
319
           case 3:
319
           case 3:
320
 	    #ifdef SDSUPPORT
320
 	    #ifdef SDSUPPORT
321
-            if(card.savetosd)
321
+            if(card.saving)
322
               break;
322
               break;
323
 	    #endif //SDSUPPORT
323
 	    #endif //SDSUPPORT
324
             Serial.println("ok"); 
324
             Serial.println("ok"); 
342
     }
342
     }
343
   }
343
   }
344
   #ifdef SDSUPPORT
344
   #ifdef SDSUPPORT
345
-  if(!card.sdmode || serial_count!=0){
345
+  if(!card.sdprinting || serial_count!=0){
346
     return;
346
     return;
347
   }
347
   }
348
-  while( card.filesize > card.sdpos  && buflen < BUFSIZE) {
349
-    short n = card.file.read();
350
-    serial_char = (char)n;
351
-    if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1) 
348
+  while( !card.eof()  && buflen < BUFSIZE) {
349
+    
350
+    serial_char = card.get();
351
+    if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1)) 
352
     {
352
     {
353
-      card.sdpos = card.file.curPosition();
354
-      if(card.sdpos >= card.filesize){
355
-        card.sdmode = false;
353
+     
354
+      if(card.eof()){
355
+        card.sdprinting = false;
356
         Serial.println("echo: Done printing file");
356
         Serial.println("echo: Done printing file");
357
         stoptime=millis();
357
         stoptime=millis();
358
         char time[30];
358
         char time[30];
565
 
565
 
566
     case 20: // M20 - list SD card
566
     case 20: // M20 - list SD card
567
       Serial.println("Begin file list");
567
       Serial.println("Begin file list");
568
-      card.root.ls();
568
+      card.ls();
569
       Serial.println("End file list");
569
       Serial.println("End file list");
570
       break;
570
       break;
571
     case 21: // M21 - init SD card
571
     case 21: // M21 - init SD card
572
-      card.sdmode = false;
572
+      
573
       card.initsd();
573
       card.initsd();
574
       break;
574
       break;
575
     case 22: //M22 - release SD card
575
     case 22: //M22 - release SD card
576
-      card.sdmode = false;
577
-      card.sdactive = false;
576
+      card.release();
577
+
578
       break;
578
       break;
579
     case 23: //M23 - Select file
579
     case 23: //M23 - Select file
580
-      if(card.sdactive){
581
-        card.sdmode = false;
582
-        card.file.close();
583
-        starpos = (strchr(strchr_pointer + 4,'*'));
584
-        if(starpos!=NULL)
585
-          *(starpos-1)='\0';
586
-        if (card.file.open(&card.root, strchr_pointer + 4, O_READ)) {
587
-          Serial.print("File opened:");
588
-          Serial.print(strchr_pointer + 4);
589
-          Serial.print(" Size:");
590
-          Serial.println(card.file.fileSize());
591
-          card.sdpos = 0;
592
-          card.filesize = card.file.fileSize();
593
-          Serial.println("File selected");
594
-        }
595
-        else{
596
-          Serial.println("file.open failed");
597
-        }
598
-      }
580
+      starpos = (strchr(strchr_pointer + 4,'*'));
581
+      if(starpos!=NULL)
582
+        *(starpos-1)='\0';
583
+      card.selectFile(strchr_pointer + 4);
599
       break;
584
       break;
600
     case 24: //M24 - Start SD print
585
     case 24: //M24 - Start SD print
601
-      if(card.sdactive){
602
-        card.sdmode = true;
603
-	starttime=millis();
604
-      }
586
+      card.startFileprint();
587
+      starttime=millis();
605
       break;
588
       break;
606
     case 25: //M25 - Pause SD print
589
     case 25: //M25 - Pause SD print
607
-      if(card.sdmode){
608
-        card.sdmode = false;
609
-      }
590
+      card.pauseSDPrint();
610
       break;
591
       break;
611
     case 26: //M26 - Set SD index
592
     case 26: //M26 - Set SD index
612
-      if(card.sdactive && code_seen('S')){
613
-        card.sdpos = code_value_long();
614
-        card.file.seekSet(card.sdpos);
593
+      if(card.cardOK && code_seen('S')){
594
+        card.setIndex(code_value_long());
595
+        
615
       }
596
       }
616
       break;
597
       break;
617
     case 27: //M27 - Get SD status
598
     case 27: //M27 - Get SD status
618
-      if(card.sdactive){
619
-        Serial.print("SD printing byte ");
620
-        Serial.print(card.sdpos);
621
-        Serial.print("/");
622
-        Serial.println(card.filesize);
623
-      }
624
-      else{
625
-        Serial.println("Not SD printing");
626
-      }
599
+      card.getStatus();
627
       break;
600
       break;
628
     case 28: //M28 - Start SD write
601
     case 28: //M28 - Start SD write
629
-      if(card.sdactive){
630
-        char* npos = 0;
631
-        card.file.close();
632
-        card.sdmode = false;
633
-        starpos = (strchr(strchr_pointer + 4,'*'));
634
-        if(starpos != NULL){
635
-          npos = strchr(cmdbuffer[bufindr], 'N');
636
-          strchr_pointer = strchr(npos,' ') + 1;
637
-          *(starpos-1) = '\0';
638
-        }
639
-        if (!card.file.open(&card.root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
640
-        {
641
-          Serial.print("open failed, File: ");
642
-          Serial.print(strchr_pointer + 4);
643
-          Serial.print(".");
644
-        }
645
-        else{
646
-          card.savetosd = true;
647
-          Serial.print("Writing to file: ");
648
-          Serial.println(strchr_pointer + 4);
649
-        }
602
+      starpos = (strchr(strchr_pointer + 4,'*'));
603
+      if(starpos != NULL){
604
+        char* npos = strchr(cmdbuffer[bufindr], 'N');
605
+        strchr_pointer = strchr(npos,' ') + 1;
606
+        *(starpos-1) = '\0';
650
       }
607
       }
608
+      card.startFilewrite(strchr_pointer+4);
609
+      
651
       break;
610
       break;
652
     case 29: //M29 - Stop SD write
611
     case 29: //M29 - Stop SD write
653
       //processed in write to file routine above
612
       //processed in write to file routine above
654
-      //savetosd = false;
613
+      //card,saving = false;
655
       break;
614
       break;
656
     #endif //SDSUPPORT
615
     #endif //SDSUPPORT
657
 
616
 

+ 24
- 12
Marlin/cardreader.h View File

18
   void checkautostart(bool x); 
18
   void checkautostart(bool x); 
19
   
19
   
20
   void closefile();
20
   void closefile();
21
+  void release();
22
+  void startFileprint();
23
+  void startFilewrite(char *name);
24
+  void pauseSDPrint();
25
+  void getStatus();
26
+  
27
+  void selectFile(char* name);
21
   void getfilename(const uint8_t nr);
28
   void getfilename(const uint8_t nr);
22
   uint8_t getnrfilenames();
29
   uint8_t getnrfilenames();
30
+  
31
+
32
+  inline void ls() {root.ls();};
33
+  inline bool eof() { sdpos = file.curPosition();return sdpos>=filesize ;};
34
+  inline char get() {  int16_t n = file.read(); return (n!=-1)?(char)n:'\n';};
35
+  inline void setIndex(long index) {sdpos = index;file.seekSet(index);};
23
 
36
 
24
 public:
37
 public:
25
-  bool savetosd;
38
+  bool saving;
39
+  bool sdprinting ;  
40
+  bool cardOK ;
41
+  char filename[11];
42
+private:
43
+  SdFile root;
44
+  Sd2Card card;
45
+  SdVolume volume;
26
   SdFile file;
46
   SdFile file;
27
   uint32_t filesize;
47
   uint32_t filesize;
48
+  //int16_t n;
49
+  unsigned long autostart_atmillis;
28
   uint32_t sdpos ;
50
   uint32_t sdpos ;
29
-  bool sdmode ;
30
-  SdFile root;
31
-  bool sdactive ;
32
-  char filename[11];
33
-private:
34
-   Sd2Card card;
35
-   SdVolume volume;
36
-
37
-   //int16_t n;
38
-   unsigned long autostart_atmillis;
39
 
51
 
40
-   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
52
+  bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
41
 };
53
 };
42
   
54
   
43
 #endif //SDSUPPORT
55
 #endif //SDSUPPORT

+ 87
- 9
Marlin/cardreader.pde View File

5
 {
5
 {
6
    filesize = 0;
6
    filesize = 0;
7
    sdpos = 0;
7
    sdpos = 0;
8
-   sdmode = false;
9
-   sdactive = false;
10
-   savetosd = false;
8
+   sdprinting = false;
9
+   cardOK = false;
10
+   saving = false;
11
    autostart_atmillis=0;
11
    autostart_atmillis=0;
12
 
12
 
13
    autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
13
    autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
22
 
22
 
23
 void CardReader::initsd()
23
 void CardReader::initsd()
24
 {
24
 {
25
-  sdactive = false;
25
+  cardOK = false;
26
   #if SDSS >- 1
26
   #if SDSS >- 1
27
     if(root.isOpen())
27
     if(root.isOpen())
28
       root.close();
28
       root.close();
41
     }
41
     }
42
     else 
42
     else 
43
     {
43
     {
44
-      sdactive = true;
44
+      cardOK = true;
45
       SERIAL_ECHOLN("SD card ok");
45
       SERIAL_ECHOLN("SD card ok");
46
     }
46
     }
47
   #endif //SDSS
47
   #endif //SDSS
48
 }
48
 }
49
+void CardReader::release()
50
+{
51
+  sdprinting = false;
52
+  cardOK = false;
53
+}
54
+
55
+void CardReader::startFileprint()
56
+{
57
+  if(cardOK)
58
+  {
59
+    sdprinting = true;
60
+    
61
+  }
62
+}
63
+
64
+void CardReader::pauseSDPrint()
65
+{
66
+  if(sdprinting)
67
+  {
68
+    sdprinting = false;
69
+  }
70
+}
71
+
72
+void CardReader::selectFile(char* name)
73
+{
74
+  if(cardOK){
75
+    sdprinting = false;
76
+    file.close();
77
+   
78
+    if (file.open(&root, name, O_READ)) {
79
+      Serial.print("File opened:");
80
+      Serial.print(name);
81
+      Serial.print(" Size:");
82
+      filesize = file.fileSize();
83
+      Serial.println(filesize);
84
+      sdpos = 0;
85
+      
86
+      Serial.println("File selected");
87
+    }
88
+    else{
89
+      Serial.println("file.open failed");
90
+    }
91
+  }
92
+}
49
 
93
 
94
+void CardReader::startFilewrite(char *name)
95
+{
96
+  if(cardOK)
97
+  {
98
+    
99
+    file.close();
100
+    sdprinting = false;
101
+    
102
+    if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
103
+    {
104
+      Serial.print("open failed, File: ");
105
+      Serial.print(name);
106
+      Serial.print(".");
107
+    }
108
+    else{
109
+      saving = true;
110
+      Serial.print("Writing to file: ");
111
+      Serial.println(name);
112
+    }
113
+  }
114
+}
115
+
116
+void CardReader::getStatus()
117
+{
118
+  if(cardOK){
119
+    Serial.print("SD printing byte ");
120
+    Serial.print(sdpos);
121
+    Serial.print("/");
122
+    Serial.println(filesize);
123
+  }
124
+  else{
125
+    Serial.println("Not SD printing");
126
+  }
127
+}
50
 void CardReader::write_command(char *buf)
128
 void CardReader::write_command(char *buf)
51
 {
129
 {
52
   char* begin = buf;
130
   char* begin = buf;
80
       return;
158
       return;
81
   }
159
   }
82
   autostart_stilltocheck=false;
160
   autostart_stilltocheck=false;
83
-  if(!sdactive)
161
+  if(!cardOK)
84
   {
162
   {
85
     initsd();
163
     initsd();
86
-    if(!sdactive) //fail
164
+    if(!cardOK) //fail
87
       return;
165
       return;
88
   }
166
   }
89
   static int lastnr=0;
167
   static int lastnr=0;
122
 
200
 
123
 void CardReader::closefile()
201
 void CardReader::closefile()
124
 {
202
 {
125
- file.sync();
203
+  file.sync();
126
   file.close();
204
   file.close();
127
-  savetosd = false; 
205
+  saving = false; 
128
 }
206
 }
129
 
207
 
130
 void CardReader::getfilename(const uint8_t nr)
208
 void CardReader::getfilename(const uint8_t nr)

+ 4
- 4
Marlin/ultralcd.pde View File

1124
  if(force_lcd_update)
1124
  if(force_lcd_update)
1125
  {
1125
  {
1126
    clear();
1126
    clear();
1127
-  if(card.sdactive)
1127
+  if(card.cardOK)
1128
   {
1128
   {
1129
     nrfiles=card.getnrfilenames();
1129
     nrfiles=card.getnrfilenames();
1130
   }
1130
   }
1312
             if(true)
1312
             if(true)
1313
           #endif
1313
           #endif
1314
           {
1314
           {
1315
-            if(card.sdmode)
1315
+            if(card.sdprinting)
1316
               lcd.print(" Stop Print   \x7E");
1316
               lcd.print(" Stop Print   \x7E");
1317
             else
1317
             else
1318
               lcd.print(" Card Menu    \x7E");
1318
               lcd.print(" Card Menu    \x7E");
1327
         #endif
1327
         #endif
1328
         if((activeline==line)&&CLICKED)
1328
         if((activeline==line)&&CLICKED)
1329
         {
1329
         {
1330
-          card.sdmode = false;
1330
+          card.sdprinting = false;
1331
           BLOCK;
1331
           BLOCK;
1332
           status=Main_SD;
1332
           status=Main_SD;
1333
           beepshort();
1333
           beepshort();
1377
       }
1377
       }
1378
       else
1378
       else
1379
       {
1379
       {
1380
-        card.sdactive=false;
1380
+        card.release();
1381
         lcd_status("Card removed");
1381
         lcd_status("Card removed");
1382
       }
1382
       }
1383
     }
1383
     }

Loading…
Cancel
Save