first compile with the cardreader class

This commit is contained in:
Bernhard Kubicek 2011-11-06 21:39:53 +01:00
parent 163efdf1c7
commit 01df04e02c
5 changed files with 288 additions and 249 deletions

View file

@ -37,13 +37,11 @@
#include "stepper.h" #include "stepper.h"
#include "temperature.h" #include "temperature.h"
#include "motion_control.h" #include "motion_control.h"
#include "cardreader.h"
char version_string[] = "1.0.0 Alpha 1"; char version_string[] = "1.0.0 Alpha 1";
#ifdef SDSUPPORT
#include "SdFat.h"
#endif //SDSUPPORT
// look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html // 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";
//=========================================================================== //===========================================================================
extern float HeaterPower; extern float HeaterPower;
//public variables
//===========================================================================
//=============================public variables=============================
//===========================================================================
CardReader card;
float homing_feedrate[] = HOMING_FEEDRATE; float homing_feedrate[] = HOMING_FEEDRATE;
bool axis_relative_modes[] = AXIS_RELATIVE_MODES; bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
volatile int feedmultiply=100; //100->1 200->2 volatile int feedmultiply=100; //100->1 200->2
@ -138,7 +140,6 @@ static bool relative_mode_e = false; //Determines Absolute or Relative E Codes
static uint8_t fanpwm=0; static uint8_t fanpwm=0;
// comm variables
static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE]; static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
static bool fromsd[BUFSIZE]; static bool fromsd[BUFSIZE];
@ -163,140 +164,12 @@ static unsigned long stepper_inactive_time = 0;
static unsigned long starttime=0; static unsigned long starttime=0;
static unsigned long stoptime=0; static unsigned long stoptime=0;
#ifdef SDSUPPORT
static Sd2Card card;
static SdVolume volume;
static SdFile root;
static SdFile file;
static uint32_t filesize = 0;
static uint32_t sdpos = 0;
static bool sdmode = false;
static bool sdactive = false;
static bool savetosd = false;
static int16_t n;
static unsigned long autostart_atmillis=0;
static bool autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
#endif //SDSUPPORT
//=========================================================================== //===========================================================================
//=============================ROUTINES============================= //=============================ROUTINES=============================
//=========================================================================== //===========================================================================
#ifdef SDSUPPORT
void initsd()
{
sdactive = false;
#if SDSS >- 1
if(root.isOpen())
root.close();
if (!card.init(SPI_FULL_SPEED,SDSS))
{
//if (!card.init(SPI_HALF_SPEED,SDSS))
SERIAL_ECHOLN("SD init fail");
}
else if (!volume.init(&card))
{
SERIAL_ERRORLN("volume.init failed");
}
else if (!root.openRoot(&volume))
{
SERIAL_ERRORLN("openRoot failed");
}
else
{
sdactive = true;
SERIAL_ECHOLN("SD card ok");
}
#endif //SDSS
}
void quickinitsd()
{
sdactive=false;
autostart_atmillis=millis()+5000;
}
inline void write_command(char *buf)
{
char* begin = buf;
char* npos = 0;
char* end = buf + strlen(buf) - 1;
file.writeError = false;
if((npos = strchr(buf, 'N')) != NULL)
{
begin = strchr(npos, ' ') + 1;
end = strchr(npos, '*') - 1;
}
end[1] = '\r';
end[2] = '\n';
end[3] = '\0';
file.write(begin);
if (file.writeError)
{
SERIAL_ERRORLN("error writing to file");
}
}
void checkautostart(bool force)
{
//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
if(!force)
{
if(!autostart_stilltocheck)
return;
if(autostart_atmillis<millis())
return;
}
autostart_stilltocheck=false;
if(!sdactive)
{
initsd();
if(!sdactive) //fail
return;
}
static int lastnr=0;
char autoname[30];
sprintf(autoname,"auto%i.g",lastnr);
for(int i=0;i<(int)strlen(autoname);i++)
autoname[i]=tolower(autoname[i]);
dir_t p;
root.rewind();
bool found=false;
while (root.readDir(p) > 0)
{
for(int i=0;i<(int)strlen((char*)p.name);i++)
p.name[i]=tolower(p.name[i]);
//Serial.print((char*)p.name);
//Serial.print(" ");
//Serial.println(autoname);
if(p.name[9]!='~') //skip safety copies
if(strncmp((char*)p.name,autoname,5)==0)
{
char cmd[30];
sprintf(cmd,"M23 %s",autoname);
//sprintf(cmd,"M115");
//enquecommand("G92 Z0");
//enquecommand("G1 Z10 F2000");
//enquecommand("G28 X-105 Y-105");
enquecommand(cmd);
enquecommand("M24");
found=true;
}
}
if(!found)
lastnr=-1;
else
lastnr++;
}
#else //NO SD SUPORT
inline void checkautostart(bool x){};
#endif //SDSUPPORT
//adds an command to the main command buffer //adds an command to the main command buffer
@ -331,14 +204,6 @@ void setup()
axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i]; axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
} }
#ifdef SDSUPPORT
//power to SD reader
#if SDPOWER > -1
SET_OUTPUT(SDPOWER);
WRITE(SDPOWER,HIGH);
#endif //SDPOWER
quickinitsd();
#endif //SDSUPPORT
plan_init(); // Initialize planner; plan_init(); // Initialize planner;
st_init(); // Initialize stepper; st_init(); // Initialize stepper;
@ -350,22 +215,20 @@ void loop()
{ {
if(buflen<3) if(buflen<3)
get_command(); get_command();
checkautostart(false); card.checkautostart(false);
if(buflen) if(buflen)
{ {
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(savetosd) if(card.savetosd)
{ {
if(strstr(cmdbuffer[bufindr],"M29") == NULL) if(strstr(cmdbuffer[bufindr],"M29") == NULL)
{ {
write_command(cmdbuffer[bufindr]); card.write_command(cmdbuffer[bufindr]);
Serial.println("ok"); Serial.println("ok");
} }
else else
{ {
file.sync(); card.closefile();
file.close();
savetosd = false;
Serial.println("Done saving file."); Serial.println("Done saving file.");
} }
} }
@ -455,7 +318,7 @@ inline void get_command()
case 2: case 2:
case 3: case 3:
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(savetosd) if(card.savetosd)
break; break;
#endif //SDSUPPORT #endif //SDSUPPORT
Serial.println("ok"); Serial.println("ok");
@ -479,17 +342,17 @@ inline void get_command()
} }
} }
#ifdef SDSUPPORT #ifdef SDSUPPORT
if(!sdmode || serial_count!=0){ if(!card.sdmode || serial_count!=0){
return; return;
} }
while( filesize > sdpos && buflen < BUFSIZE) { while( card.filesize > card.sdpos && buflen < BUFSIZE) {
n = file.read(); short n = card.file.read();
serial_char = (char)n; serial_char = (char)n;
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) || n == -1)
{ {
sdpos = file.curPosition(); card.sdpos = card.file.curPosition();
if(sdpos >= filesize){ if(card.sdpos >= card.filesize){
sdmode = false; card.sdmode = false;
Serial.println("echo: Done printing file"); Serial.println("echo: Done printing file");
stoptime=millis(); stoptime=millis();
char time[30]; char time[30];
@ -500,7 +363,7 @@ inline void get_command()
sprintf(time,"echo: %i min, %i sec",min,sec); sprintf(time,"echo: %i min, %i sec",min,sec);
Serial.println(time); Serial.println(time);
LCD_MESSAGE(time); LCD_MESSAGE(time);
checkautostart(true); card.checkautostart(true);
} }
if(!serial_count) if(!serial_count)
return; //if empty line return; //if empty line
@ -702,31 +565,31 @@ 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");
root.ls(); card.root.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
sdmode = false; card.sdmode = false;
initsd(); card.initsd();
break; break;
case 22: //M22 - release SD card case 22: //M22 - release SD card
sdmode = false; card.sdmode = false;
sdactive = false; card.sdactive = false;
break; break;
case 23: //M23 - Select file case 23: //M23 - Select file
if(sdactive){ if(card.sdactive){
sdmode = false; card.sdmode = false;
file.close(); 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 (file.open(&root, strchr_pointer + 4, O_READ)) { if (card.file.open(&card.root, strchr_pointer + 4, O_READ)) {
Serial.print("File opened:"); Serial.print("File opened:");
Serial.print(strchr_pointer + 4); Serial.print(strchr_pointer + 4);
Serial.print(" Size:"); Serial.print(" Size:");
Serial.println(file.fileSize()); Serial.println(card.file.fileSize());
sdpos = 0; card.sdpos = 0;
filesize = file.fileSize(); card.filesize = card.file.fileSize();
Serial.println("File selected"); Serial.println("File selected");
} }
else{ else{
@ -735,52 +598,52 @@ inline void process_commands()
} }
break; break;
case 24: //M24 - Start SD print case 24: //M24 - Start SD print
if(sdactive){ if(card.sdactive){
sdmode = true; card.sdmode = true;
starttime=millis(); starttime=millis();
} }
break; break;
case 25: //M25 - Pause SD print case 25: //M25 - Pause SD print
if(sdmode){ if(card.sdmode){
sdmode = false; card.sdmode = false;
} }
break; break;
case 26: //M26 - Set SD index case 26: //M26 - Set SD index
if(sdactive && code_seen('S')){ if(card.sdactive && code_seen('S')){
sdpos = code_value_long(); card.sdpos = code_value_long();
file.seekSet(sdpos); card.file.seekSet(card.sdpos);
} }
break; break;
case 27: //M27 - Get SD status case 27: //M27 - Get SD status
if(sdactive){ if(card.sdactive){
Serial.print("SD printing byte "); Serial.print("SD printing byte ");
Serial.print(sdpos); Serial.print(card.sdpos);
Serial.print("/"); Serial.print("/");
Serial.println(filesize); Serial.println(card.filesize);
} }
else{ else{
Serial.println("Not SD printing"); Serial.println("Not SD printing");
} }
break; break;
case 28: //M28 - Start SD write case 28: //M28 - Start SD write
if(sdactive){ if(card.sdactive){
char* npos = 0; char* npos = 0;
file.close(); card.file.close();
sdmode = false; card.sdmode = false;
starpos = (strchr(strchr_pointer + 4,'*')); starpos = (strchr(strchr_pointer + 4,'*'));
if(starpos != NULL){ if(starpos != NULL){
npos = strchr(cmdbuffer[bufindr], 'N'); npos = strchr(cmdbuffer[bufindr], 'N');
strchr_pointer = strchr(npos,' ') + 1; strchr_pointer = strchr(npos,' ') + 1;
*(starpos-1) = '\0'; *(starpos-1) = '\0';
} }
if (!file.open(&root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) if (!card.file.open(&card.root, strchr_pointer+4, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
{ {
Serial.print("open failed, File: "); Serial.print("open failed, File: ");
Serial.print(strchr_pointer + 4); Serial.print(strchr_pointer + 4);
Serial.print("."); Serial.print(".");
} }
else{ else{
savetosd = true; card.savetosd = true;
Serial.print("Writing to file: "); Serial.print("Writing to file: ");
Serial.println(strchr_pointer + 4); Serial.println(strchr_pointer + 4);
} }

47
Marlin/cardreader.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef __CARDREADERH
#define __CARDREADERH
#ifdef SDSUPPORT
#include "SdFat.h"
class CardReader
{
public:
CardReader();
void initsd();
void write_command(char *buf);
//files auto[0-9].g on the sd card are performed in a row
//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
void checkautostart(bool x);
void closefile();
void getfilename(const uint8_t nr);
uint8_t getnrfilenames();
public:
bool savetosd;
SdFile file;
uint32_t filesize;
uint32_t sdpos ;
bool sdmode ;
SdFile root;
bool sdactive ;
char filename[11];
private:
Sd2Card card;
SdVolume volume;
//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.
};
#endif //SDSUPPORT
#endif

177
Marlin/cardreader.pde Normal file
View file

@ -0,0 +1,177 @@
#ifdef SDSUPPORT
#include "cardreader.h"
CardReader::CardReader()
{
filesize = 0;
sdpos = 0;
sdmode = false;
sdactive = false;
savetosd = 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.
//power to SD reader
#if SDPOWER > -1
SET_OUTPUT(SDPOWER);
WRITE(SDPOWER,HIGH);
#endif //SDPOWER
autostart_atmillis=millis()+5000;
}
void CardReader::initsd()
{
sdactive = false;
#if SDSS >- 1
if(root.isOpen())
root.close();
if (!card.init(SPI_FULL_SPEED,SDSS))
{
//if (!card.init(SPI_HALF_SPEED,SDSS))
SERIAL_ECHOLN("SD init fail");
}
else if (!volume.init(&card))
{
SERIAL_ERRORLN("volume.init failed");
}
else if (!root.openRoot(&volume))
{
SERIAL_ERRORLN("openRoot failed");
}
else
{
sdactive = true;
SERIAL_ECHOLN("SD card ok");
}
#endif //SDSS
}
void CardReader::write_command(char *buf)
{
char* begin = buf;
char* npos = 0;
char* end = buf + strlen(buf) - 1;
file.writeError = false;
if((npos = strchr(buf, 'N')) != NULL)
{
begin = strchr(npos, ' ') + 1;
end = strchr(npos, '*') - 1;
}
end[1] = '\r';
end[2] = '\n';
end[3] = '\0';
file.write(begin);
if (file.writeError)
{
SERIAL_ERRORLN("error writing to file");
}
}
void CardReader::checkautostart(bool force)
{
if(!force)
{
if(!autostart_stilltocheck)
return;
if(autostart_atmillis<millis())
return;
}
autostart_stilltocheck=false;
if(!sdactive)
{
initsd();
if(!sdactive) //fail
return;
}
static int lastnr=0;
char autoname[30];
sprintf(autoname,"auto%i.g",lastnr);
for(int i=0;i<(int)strlen(autoname);i++)
autoname[i]=tolower(autoname[i]);
dir_t p;
root.rewind();
bool found=false;
while (root.readDir(p) > 0)
{
for(int i=0;i<(int)strlen((char*)p.name);i++)
p.name[i]=tolower(p.name[i]);
//Serial.print((char*)p.name);
//Serial.print(" ");
//Serial.println(autoname);
if(p.name[9]!='~') //skip safety copies
if(strncmp((char*)p.name,autoname,5)==0)
{
char cmd[30];
sprintf(cmd,"M23 %s",autoname);
enquecommand(cmd);
enquecommand("M24");
found=true;
}
}
if(!found)
lastnr=-1;
else
lastnr++;
}
void CardReader::closefile()
{
file.sync();
file.close();
savetosd = false;
}
void CardReader::getfilename(const uint8_t nr)
{
dir_t p;
root.rewind();
uint8_t cnt=0;
filename[0]='\0';
while (root.readDir(p) > 0)
{
if (p.name[0] == DIR_NAME_FREE) break;
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
if(p.name[8]!='G') continue;
if(p.name[9]=='~') continue;
if(cnt++!=nr) continue;
//Serial.println((char*)p.name);
uint8_t writepos=0;
for (uint8_t i = 0; i < 11; i++)
{
if (p.name[i] == ' ') continue;
if (i == 8) {
filename[writepos++]='.';
}
filename[writepos++]=p.name[i];
}
filename[writepos++]=0;
}
}
uint8_t CardReader::getnrfilenames()
{
dir_t p;
root.rewind();
uint8_t cnt=0;
while (root.readDir(p) > 0)
{
if (p.name[0] == DIR_NAME_FREE) break;
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
if(p.name[8]!='G') continue;
if(p.name[9]=='~') continue;
cnt++;
}
return cnt;
}
#endif //SDSUPPORT

View file

@ -57,7 +57,6 @@
public: public:
MainMenu(); MainMenu();
void update(); void update();
void getfilename(const uint8_t nr);
uint8_t activeline; uint8_t activeline;
MainStatus status; MainStatus status;
uint8_t displayStartingRow; uint8_t displayStartingRow;
@ -71,7 +70,7 @@
int lastencoderpos; int lastencoderpos;
int8_t lineoffset; int8_t lineoffset;
int8_t lastlineoffset; int8_t lastlineoffset;
char filename[11];
bool linechanging; bool linechanging;
}; };

View file

@ -6,6 +6,7 @@ extern volatile int feedmultiply;
extern volatile bool feedmultiplychanged; extern volatile bool feedmultiplychanged;
extern long position[4]; extern long position[4];
extern CardReader card;
static char messagetext[LCD_WIDTH]=""; static char messagetext[LCD_WIDTH]="";
@ -1107,56 +1108,8 @@ void MainMenu::showControl()
#include "SdFat.h" #include "SdFat.h"
void MainMenu::getfilename(const uint8_t nr)
{
#ifdef SDSUPPORT
dir_t p;
root.rewind();
uint8_t cnt=0;
filename[0]='\0';
while (root.readDir(p) > 0)
{
if (p.name[0] == DIR_NAME_FREE) break;
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
if(p.name[8]!='G') continue;
if(p.name[9]=='~') continue;
if(cnt++!=nr) continue;
//Serial.println((char*)p.name);
uint8_t writepos=0;
for (uint8_t i = 0; i < 11; i++)
{
if (p.name[i] == ' ') continue;
if (i == 8) {
filename[writepos++]='.';
}
filename[writepos++]=p.name[i];
}
filename[writepos++]=0;
}
#endif
}
uint8_t getnrfilenames()
{
#ifdef SDSUPPORT
dir_t p;
root.rewind();
uint8_t cnt=0;
while (root.readDir(p) > 0)
{
if (p.name[0] == DIR_NAME_FREE) break;
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
if(p.name[8]!='G') continue;
if(p.name[9]=='~') continue;
cnt++;
}
return cnt;
#else
return 0;
#endif
}
void MainMenu::showSD() void MainMenu::showSD()
{ {
@ -1171,9 +1124,9 @@ void MainMenu::showSD()
if(force_lcd_update) if(force_lcd_update)
{ {
clear(); clear();
if(sdactive) if(card.sdactive)
{ {
nrfiles=getnrfilenames(); nrfiles=card.getnrfilenames();
} }
else else
{ {
@ -1223,9 +1176,9 @@ void MainMenu::showSD()
{ {
BLOCK; BLOCK;
beepshort(); beepshort();
initsd(); card.initsd();
force_lcd_update=true; force_lcd_update=true;
nrfiles=getnrfilenames(); nrfiles=card.getnrfilenames();
} }
}break; }break;
default: default:
@ -1234,24 +1187,24 @@ void MainMenu::showSD()
{ {
if(force_lcd_update) if(force_lcd_update)
{ {
getfilename(i-2); card.getfilename(i-2);
//Serial.print("Filenr:");Serial.println(i-2); //Serial.print("Filenr:");Serial.println(i-2);
lcd.setCursor(0,line);lcd.print(" ");lcd.print(filename); lcd.setCursor(0,line);lcd.print(" ");lcd.print(card.filename);
} }
if((activeline==line) && CLICKED) if((activeline==line) && CLICKED)
{ {
BLOCK BLOCK
getfilename(i-2); card.getfilename(i-2);
char cmd[30]; char cmd[30];
for(int i=0;i<strlen(filename);i++) for(int i=0;i<strlen(card.filename);i++)
filename[i]=tolower(filename[i]); card.filename[i]=tolower(card.filename[i]);
sprintf(cmd,"M23 %s",filename); sprintf(cmd,"M23 %s",card.filename);
//sprintf(cmd,"M115"); //sprintf(cmd,"M115");
enquecommand(cmd); enquecommand(cmd);
enquecommand("M24"); enquecommand("M24");
beep(); beep();
status=Main_Status; status=Main_Status;
lcd_status(filename); lcd_status(card.filename);
} }
} }
@ -1359,7 +1312,7 @@ void MainMenu::showMainMenu()
if(true) if(true)
#endif #endif
{ {
if(sdmode) if(card.sdmode)
lcd.print(" Stop Print \x7E"); lcd.print(" Stop Print \x7E");
else else
lcd.print(" Card Menu \x7E"); lcd.print(" Card Menu \x7E");
@ -1374,7 +1327,7 @@ void MainMenu::showMainMenu()
#endif #endif
if((activeline==line)&&CLICKED) if((activeline==line)&&CLICKED)
{ {
sdmode = false; card.sdmode = false;
BLOCK; BLOCK;
status=Main_SD; status=Main_SD;
beepshort(); beepshort();
@ -1419,12 +1372,12 @@ void MainMenu::update()
//Serial.println("echo: SD CHANGE"); //Serial.println("echo: SD CHANGE");
if(CARDINSERTED) if(CARDINSERTED)
{ {
initsd(); card.initsd();
lcd_status("Card inserted"); lcd_status("Card inserted");
} }
else else
{ {
sdactive=false; card.sdactive=false;
lcd_status("Card removed"); lcd_status("Card removed");
} }
} }