diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde index 5351bce07..03d360326 100644 --- a/Marlin/Marlin.pde +++ b/Marlin/Marlin.pde @@ -219,7 +219,7 @@ void loop() if(buflen) { #ifdef SDSUPPORT - if(card.savetosd) + if(card.saving) { if(strstr(cmdbuffer[bufindr],"M29") == NULL) { @@ -318,7 +318,7 @@ inline void get_command() case 2: case 3: #ifdef SDSUPPORT - if(card.savetosd) + if(card.saving) break; #endif //SDSUPPORT Serial.println("ok"); @@ -342,17 +342,17 @@ inline void get_command() } } #ifdef SDSUPPORT - if(!card.sdmode || serial_count!=0){ + if(!card.sdprinting || serial_count!=0){ return; } - while( card.filesize > card.sdpos && buflen < BUFSIZE) { - short n = card.file.read(); - serial_char = (char)n; - if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1) + while( !card.eof() && buflen < BUFSIZE) { + + serial_char = card.get(); + if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1)) { - card.sdpos = card.file.curPosition(); - if(card.sdpos >= card.filesize){ - card.sdmode = false; + + if(card.eof()){ + card.sdprinting = false; Serial.println("echo: Done printing file"); stoptime=millis(); char time[30]; @@ -565,93 +565,52 @@ inline void process_commands() case 20: // M20 - list SD card Serial.println("Begin file list"); - card.root.ls(); + card.ls(); Serial.println("End file list"); break; case 21: // M21 - init SD card - card.sdmode = false; + card.initsd(); break; case 22: //M22 - release SD card - card.sdmode = false; - card.sdactive = false; + card.release(); + break; case 23: //M23 - Select file - if(card.sdactive){ - card.sdmode = false; - card.file.close(); - starpos = (strchr(strchr_pointer + 4,'*')); - if(starpos!=NULL) - *(starpos-1)='\0'; - if (card.file.open(&card.root, strchr_pointer + 4, O_READ)) { - Serial.print("File opened:"); - Serial.print(strchr_pointer + 4); - Serial.print(" Size:"); - Serial.println(card.file.fileSize()); - card.sdpos = 0; - card.filesize = card.file.fileSize(); - Serial.println("File selected"); - } - else{ - Serial.println("file.open failed"); - } - } + starpos = (strchr(strchr_pointer + 4,'*')); + if(starpos!=NULL) + *(starpos-1)='\0'; + card.selectFile(strchr_pointer + 4); break; case 24: //M24 - Start SD print - if(card.sdactive){ - card.sdmode = true; - starttime=millis(); - } + card.startFileprint(); + starttime=millis(); break; case 25: //M25 - Pause SD print - if(card.sdmode){ - card.sdmode = false; - } + card.pauseSDPrint(); break; case 26: //M26 - Set SD index - if(card.sdactive && code_seen('S')){ - card.sdpos = code_value_long(); - card.file.seekSet(card.sdpos); + if(card.cardOK && code_seen('S')){ + card.setIndex(code_value_long()); + } break; case 27: //M27 - Get SD status - if(card.sdactive){ - Serial.print("SD printing byte "); - Serial.print(card.sdpos); - Serial.print("/"); - Serial.println(card.filesize); - } - else{ - Serial.println("Not SD printing"); - } + card.getStatus(); break; case 28: //M28 - Start SD write - if(card.sdactive){ - char* npos = 0; - card.file.close(); - card.sdmode = false; - starpos = (strchr(strchr_pointer + 4,'*')); - if(starpos != NULL){ - npos = strchr(cmdbuffer[bufindr], 'N'); - strchr_pointer = strchr(npos,' ') + 1; - *(starpos-1) = '\0'; - } - if (!card.file.open(&card.root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) - { - Serial.print("open failed, File: "); - Serial.print(strchr_pointer + 4); - Serial.print("."); - } - else{ - card.savetosd = true; - Serial.print("Writing to file: "); - Serial.println(strchr_pointer + 4); - } + starpos = (strchr(strchr_pointer + 4,'*')); + if(starpos != NULL){ + char* npos = strchr(cmdbuffer[bufindr], 'N'); + strchr_pointer = strchr(npos,' ') + 1; + *(starpos-1) = '\0'; } + card.startFilewrite(strchr_pointer+4); + break; case 29: //M29 - Stop SD write //processed in write to file routine above - //savetosd = false; + //card,saving = false; break; #endif //SDSUPPORT diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h index a67374ffa..583c55c74 100644 --- a/Marlin/cardreader.h +++ b/Marlin/cardreader.h @@ -18,26 +18,38 @@ public: void checkautostart(bool x); void closefile(); + void release(); + void startFileprint(); + void startFilewrite(char *name); + void pauseSDPrint(); + void getStatus(); + + void selectFile(char* name); void getfilename(const uint8_t nr); uint8_t getnrfilenames(); + + + inline void ls() {root.ls();}; + inline bool eof() { sdpos = file.curPosition();return sdpos>=filesize ;}; + inline char get() { int16_t n = file.read(); return (n!=-1)?(char)n:'\n';}; + inline void setIndex(long index) {sdpos = index;file.seekSet(index);}; public: - bool savetosd; - SdFile file; - uint32_t filesize; - uint32_t sdpos ; - bool sdmode ; - SdFile root; - bool sdactive ; + bool saving; + bool sdprinting ; + bool cardOK ; char filename[11]; private: - Sd2Card card; - SdVolume volume; + SdFile root; + Sd2Card card; + SdVolume volume; + SdFile file; + uint32_t filesize; + //int16_t n; + unsigned long autostart_atmillis; + uint32_t sdpos ; - //int16_t n; - unsigned long autostart_atmillis; - - bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware. + bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware. }; #endif //SDSUPPORT diff --git a/Marlin/cardreader.pde b/Marlin/cardreader.pde index 5773641e4..9f94de0bc 100644 --- a/Marlin/cardreader.pde +++ b/Marlin/cardreader.pde @@ -5,9 +5,9 @@ CardReader::CardReader() { filesize = 0; sdpos = 0; - sdmode = false; - sdactive = false; - savetosd = false; + sdprinting = false; + cardOK = false; + saving = false; autostart_atmillis=0; 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() void CardReader::initsd() { - sdactive = false; + cardOK = false; #if SDSS >- 1 if(root.isOpen()) root.close(); @@ -41,12 +41,90 @@ void CardReader::initsd() } else { - sdactive = true; + cardOK = true; SERIAL_ECHOLN("SD card ok"); } #endif //SDSS } +void CardReader::release() +{ + sdprinting = false; + cardOK = false; +} +void CardReader::startFileprint() +{ + if(cardOK) + { + sdprinting = true; + + } +} + +void CardReader::pauseSDPrint() +{ + if(sdprinting) + { + sdprinting = false; + } +} + +void CardReader::selectFile(char* name) +{ + if(cardOK){ + sdprinting = false; + file.close(); + + if (file.open(&root, name, O_READ)) { + Serial.print("File opened:"); + Serial.print(name); + Serial.print(" Size:"); + filesize = file.fileSize(); + Serial.println(filesize); + sdpos = 0; + + Serial.println("File selected"); + } + else{ + Serial.println("file.open failed"); + } + } +} + +void CardReader::startFilewrite(char *name) +{ + if(cardOK) + { + + file.close(); + sdprinting = false; + + if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) + { + Serial.print("open failed, File: "); + Serial.print(name); + Serial.print("."); + } + else{ + saving = true; + Serial.print("Writing to file: "); + Serial.println(name); + } + } +} + +void CardReader::getStatus() +{ + if(cardOK){ + Serial.print("SD printing byte "); + Serial.print(sdpos); + Serial.print("/"); + Serial.println(filesize); + } + else{ + Serial.println("Not SD printing"); + } +} void CardReader::write_command(char *buf) { char* begin = buf; @@ -80,10 +158,10 @@ void CardReader::checkautostart(bool force) return; } autostart_stilltocheck=false; - if(!sdactive) + if(!cardOK) { initsd(); - if(!sdactive) //fail + if(!cardOK) //fail return; } static int lastnr=0; @@ -122,9 +200,9 @@ void CardReader::checkautostart(bool force) void CardReader::closefile() { - file.sync(); + file.sync(); file.close(); - savetosd = false; + saving = false; } void CardReader::getfilename(const uint8_t nr) diff --git a/Marlin/ultralcd.pde b/Marlin/ultralcd.pde index 121d8ff7f..77d7151d9 100644 --- a/Marlin/ultralcd.pde +++ b/Marlin/ultralcd.pde @@ -1124,7 +1124,7 @@ void MainMenu::showSD() if(force_lcd_update) { clear(); - if(card.sdactive) + if(card.cardOK) { nrfiles=card.getnrfilenames(); } @@ -1312,7 +1312,7 @@ void MainMenu::showMainMenu() if(true) #endif { - if(card.sdmode) + if(card.sdprinting) lcd.print(" Stop Print \x7E"); else lcd.print(" Card Menu \x7E"); @@ -1327,7 +1327,7 @@ void MainMenu::showMainMenu() #endif if((activeline==line)&&CLICKED) { - card.sdmode = false; + card.sdprinting = false; BLOCK; status=Main_SD; beepshort(); @@ -1377,7 +1377,7 @@ void MainMenu::update() } else { - card.sdactive=false; + card.release(); lcd_status("Card removed"); } }