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,7 +219,7 @@ void loop()
219 219
   if(buflen)
220 220
   {
221 221
     #ifdef SDSUPPORT
222
-      if(card.savetosd)
222
+      if(card.saving)
223 223
       {
224 224
 	if(strstr(cmdbuffer[bufindr],"M29") == NULL)
225 225
 	{
@@ -318,7 +318,7 @@ inline void get_command()
318 318
           case 2:
319 319
           case 3:
320 320
 	    #ifdef SDSUPPORT
321
-            if(card.savetosd)
321
+            if(card.saving)
322 322
               break;
323 323
 	    #endif //SDSUPPORT
324 324
             Serial.println("ok"); 
@@ -342,17 +342,17 @@ inline void get_command()
342 342
     }
343 343
   }
344 344
   #ifdef SDSUPPORT
345
-  if(!card.sdmode || serial_count!=0){
345
+  if(!card.sdprinting || serial_count!=0){
346 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 356
         Serial.println("echo: Done printing file");
357 357
         stoptime=millis();
358 358
         char time[30];
@@ -565,93 +565,52 @@ inline void process_commands()
565 565
 
566 566
     case 20: // M20 - list SD card
567 567
       Serial.println("Begin file list");
568
-      card.root.ls();
568
+      card.ls();
569 569
       Serial.println("End file list");
570 570
       break;
571 571
     case 21: // M21 - init SD card
572
-      card.sdmode = false;
572
+      
573 573
       card.initsd();
574 574
       break;
575 575
     case 22: //M22 - release SD card
576
-      card.sdmode = false;
577
-      card.sdactive = false;
576
+      card.release();
577
+
578 578
       break;
579 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 584
       break;
600 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 588
       break;
606 589
     case 25: //M25 - Pause SD print
607
-      if(card.sdmode){
608
-        card.sdmode = false;
609
-      }
590
+      card.pauseSDPrint();
610 591
       break;
611 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 597
       break;
617 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 600
       break;
628 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 610
       break;
652 611
     case 29: //M29 - Stop SD write
653 612
       //processed in write to file routine above
654
-      //savetosd = false;
613
+      //card,saving = false;
655 614
       break;
656 615
     #endif //SDSUPPORT
657 616
 

+ 24
- 12
Marlin/cardreader.h View File

@@ -18,26 +18,38 @@ public:
18 18
   void checkautostart(bool x); 
19 19
   
20 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 28
   void getfilename(const uint8_t nr);
22 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 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 46
   SdFile file;
27 47
   uint32_t filesize;
48
+  //int16_t n;
49
+  unsigned long autostart_atmillis;
28 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 55
 #endif //SDSUPPORT

+ 87
- 9
Marlin/cardreader.pde View File

@@ -5,9 +5,9 @@ CardReader::CardReader()
5 5
 {
6 6
    filesize = 0;
7 7
    sdpos = 0;
8
-   sdmode = false;
9
-   sdactive = false;
10
-   savetosd = false;
8
+   sdprinting = false;
9
+   cardOK = false;
10
+   saving = false;
11 11
    autostart_atmillis=0;
12 12
 
13 13
    autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
@@ -22,7 +22,7 @@ CardReader::CardReader()
22 22
 
23 23
 void CardReader::initsd()
24 24
 {
25
-  sdactive = false;
25
+  cardOK = false;
26 26
   #if SDSS >- 1
27 27
     if(root.isOpen())
28 28
       root.close();
@@ -41,12 +41,90 @@ void CardReader::initsd()
41 41
     }
42 42
     else 
43 43
     {
44
-      sdactive = true;
44
+      cardOK = true;
45 45
       SERIAL_ECHOLN("SD card ok");
46 46
     }
47 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 128
 void CardReader::write_command(char *buf)
51 129
 {
52 130
   char* begin = buf;
@@ -80,10 +158,10 @@ void CardReader::checkautostart(bool force)
80 158
       return;
81 159
   }
82 160
   autostart_stilltocheck=false;
83
-  if(!sdactive)
161
+  if(!cardOK)
84 162
   {
85 163
     initsd();
86
-    if(!sdactive) //fail
164
+    if(!cardOK) //fail
87 165
       return;
88 166
   }
89 167
   static int lastnr=0;
@@ -122,9 +200,9 @@ void CardReader::checkautostart(bool force)
122 200
 
123 201
 void CardReader::closefile()
124 202
 {
125
- file.sync();
203
+  file.sync();
126 204
   file.close();
127
-  savetosd = false; 
205
+  saving = false; 
128 206
 }
129 207
 
130 208
 void CardReader::getfilename(const uint8_t nr)

+ 4
- 4
Marlin/ultralcd.pde View File

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

Loading…
Cancel
Save