made cardreader more selfsustained

This commit is contained in:
Bernhard Kubicek 2011-11-06 22:48:15 +01:00
parent 01df04e02c
commit fc0064e525
4 changed files with 149 additions and 100 deletions

View file

@ -219,7 +219,7 @@ void loop()
if(buflen) if(buflen)
{ {
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(card.savetosd) if(card.saving)
{ {
if(strstr(cmdbuffer[bufindr],"M29") == NULL) if(strstr(cmdbuffer[bufindr],"M29") == NULL)
{ {
@ -318,7 +318,7 @@ inline void get_command()
case 2: case 2:
case 3: case 3:
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(card.savetosd) if(card.saving)
break; break;
#endif //SDSUPPORT #endif //SDSUPPORT
Serial.println("ok"); Serial.println("ok");
@ -342,17 +342,17 @@ inline void get_command()
} }
} }
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(!card.sdmode || serial_count!=0){ if(!card.sdprinting || serial_count!=0){
return; return;
} }
while( card.filesize > card.sdpos && buflen < BUFSIZE) { while( !card.eof() && buflen < BUFSIZE) {
short n = card.file.read();
serial_char = (char)n; serial_char = card.get();
if(serial_char == '\n' || serial_char == '\r' || serial_char == ':' || serial_count >= (MAX_CMD_SIZE - 1) || n == -1) 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){ if(card.eof()){
card.sdmode = false; card.sdprinting = false;
Serial.println("echo: Done printing file"); Serial.println("echo: Done printing file");
stoptime=millis(); stoptime=millis();
char time[30]; char time[30];
@ -565,93 +565,52 @@ inline void process_commands()
case 20: // M20 - list SD card case 20: // M20 - list SD card
Serial.println("Begin file list"); Serial.println("Begin file list");
card.root.ls(); card.ls();
Serial.println("End file list"); Serial.println("End file list");
break; break;
case 21: // M21 - init SD card case 21: // M21 - init SD card
card.sdmode = false;
card.initsd(); card.initsd();
break; break;
case 22: //M22 - release SD card case 22: //M22 - release SD card
card.sdmode = false; card.release();
card.sdactive = false;
break; break;
case 23: //M23 - Select file case 23: //M23 - Select file
if(card.sdactive){
card.sdmode = false;
card.file.close();
starpos = (strchr(strchr_pointer + 4,'*')); starpos = (strchr(strchr_pointer + 4,'*'));
if(starpos!=NULL) if(starpos!=NULL)
*(starpos-1)='\0'; *(starpos-1)='\0';
if (card.file.open(&card.root, strchr_pointer + 4, O_READ)) { card.selectFile(strchr_pointer + 4);
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");
}
}
break; break;
case 24: //M24 - Start SD print case 24: //M24 - Start SD print
if(card.sdactive){ card.startFileprint();
card.sdmode = true;
starttime=millis(); starttime=millis();
}
break; break;
case 25: //M25 - Pause SD print case 25: //M25 - Pause SD print
if(card.sdmode){ card.pauseSDPrint();
card.sdmode = false;
}
break; break;
case 26: //M26 - Set SD index case 26: //M26 - Set SD index
if(card.sdactive && code_seen('S')){ if(card.cardOK && code_seen('S')){
card.sdpos = code_value_long(); card.setIndex(code_value_long());
card.file.seekSet(card.sdpos);
} }
break; break;
case 27: //M27 - Get SD status case 27: //M27 - Get SD status
if(card.sdactive){ card.getStatus();
Serial.print("SD printing byte ");
Serial.print(card.sdpos);
Serial.print("/");
Serial.println(card.filesize);
}
else{
Serial.println("Not SD printing");
}
break; break;
case 28: //M28 - Start SD write case 28: //M28 - Start SD write
if(card.sdactive){
char* npos = 0;
card.file.close();
card.sdmode = false;
starpos = (strchr(strchr_pointer + 4,'*')); starpos = (strchr(strchr_pointer + 4,'*'));
if(starpos != NULL){ if(starpos != NULL){
npos = strchr(cmdbuffer[bufindr], 'N'); char* npos = strchr(cmdbuffer[bufindr], 'N');
strchr_pointer = strchr(npos,' ') + 1; strchr_pointer = strchr(npos,' ') + 1;
*(starpos-1) = '\0'; *(starpos-1) = '\0';
} }
if (!card.file.open(&card.root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) card.startFilewrite(strchr_pointer+4);
{
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);
}
}
break; break;
case 29: //M29 - Stop SD write case 29: //M29 - Stop SD write
//processed in write to file routine above //processed in write to file routine above
//savetosd = false; //card,saving = false;
break; break;
#endif //SDSUPPORT #endif //SDSUPPORT

View file

@ -18,24 +18,36 @@ public:
void checkautostart(bool x); void checkautostart(bool x);
void closefile(); void closefile();
void release();
void startFileprint();
void startFilewrite(char *name);
void pauseSDPrint();
void getStatus();
void selectFile(char* name);
void getfilename(const uint8_t nr); void getfilename(const uint8_t nr);
uint8_t getnrfilenames(); 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: public:
bool savetosd; bool saving;
SdFile file; bool sdprinting ;
uint32_t filesize; bool cardOK ;
uint32_t sdpos ;
bool sdmode ;
SdFile root;
bool sdactive ;
char filename[11]; char filename[11];
private: private:
SdFile root;
Sd2Card card; Sd2Card card;
SdVolume volume; SdVolume volume;
SdFile file;
uint32_t filesize;
//int16_t n; //int16_t n;
unsigned long autostart_atmillis; unsigned long autostart_atmillis;
uint32_t sdpos ;
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.
}; };

View file

@ -5,9 +5,9 @@ CardReader::CardReader()
{ {
filesize = 0; filesize = 0;
sdpos = 0; sdpos = 0;
sdmode = false; sdprinting = false;
sdactive = false; cardOK = false;
savetosd = false; saving = false;
autostart_atmillis=0; 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. 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() void CardReader::initsd()
{ {
sdactive = false; cardOK = false;
#if SDSS >- 1 #if SDSS >- 1
if(root.isOpen()) if(root.isOpen())
root.close(); root.close();
@ -41,12 +41,90 @@ void CardReader::initsd()
} }
else else
{ {
sdactive = true; cardOK = true;
SERIAL_ECHOLN("SD card ok"); SERIAL_ECHOLN("SD card ok");
} }
#endif //SDSS #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) void CardReader::write_command(char *buf)
{ {
char* begin = buf; char* begin = buf;
@ -80,10 +158,10 @@ void CardReader::checkautostart(bool force)
return; return;
} }
autostart_stilltocheck=false; autostart_stilltocheck=false;
if(!sdactive) if(!cardOK)
{ {
initsd(); initsd();
if(!sdactive) //fail if(!cardOK) //fail
return; return;
} }
static int lastnr=0; static int lastnr=0;
@ -124,7 +202,7 @@ void CardReader::closefile()
{ {
file.sync(); file.sync();
file.close(); file.close();
savetosd = false; saving = false;
} }
void CardReader::getfilename(const uint8_t nr) void CardReader::getfilename(const uint8_t nr)

View file

@ -1124,7 +1124,7 @@ void MainMenu::showSD()
if(force_lcd_update) if(force_lcd_update)
{ {
clear(); clear();
if(card.sdactive) if(card.cardOK)
{ {
nrfiles=card.getnrfilenames(); nrfiles=card.getnrfilenames();
} }
@ -1312,7 +1312,7 @@ void MainMenu::showMainMenu()
if(true) if(true)
#endif #endif
{ {
if(card.sdmode) if(card.sdprinting)
lcd.print(" Stop Print \x7E"); lcd.print(" Stop Print \x7E");
else else
lcd.print(" Card Menu \x7E"); lcd.print(" Card Menu \x7E");
@ -1327,7 +1327,7 @@ void MainMenu::showMainMenu()
#endif #endif
if((activeline==line)&&CLICKED) if((activeline==line)&&CLICKED)
{ {
card.sdmode = false; card.sdprinting = false;
BLOCK; BLOCK;
status=Main_SD; status=Main_SD;
beepshort(); beepshort();
@ -1377,7 +1377,7 @@ void MainMenu::update()
} }
else else
{ {
card.sdactive=false; card.release();
lcd_status("Card removed"); lcd_status("Card removed");
} }
} }