Clean up autostart handling

This commit is contained in:
Scott Lahteine
2018-05-01 01:01:02 -05:00
parent c87b0e21be
commit 05fca752d6
5 changed files with 46 additions and 55 deletions

View File

@ -62,15 +62,13 @@ CardReader::CardReader() {
workDirDepth = 0;
ZERO(workDirParents);
autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
autostart_index = 0;
// Disable autostart until card is initialized
autostart_index = -1;
//power to SD reader
#if SDPOWER > -1
OUT_WRITE(SDPOWER, HIGH);
#endif // SDPOWER
next_autostart_ms = millis() + 5000;
#endif
}
char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
@ -607,40 +605,42 @@ void CardReader::write_command(char *buf) {
}
}
void CardReader::checkautostart(bool force) {
if (!force && (!autostart_stilltocheck || PENDING(millis(), next_autostart_ms)))
return;
//
// Run the next autostart file. Called:
// - On boot after successful card init
// - After finishing the previous autostart file
// - From the LCD command to run the autostart file
//
autostart_stilltocheck = false;
void CardReader::checkautostart() {
if (!cardOK) {
initsd();
if (!cardOK) return; // fail
}
if (autostart_index < 0 || sdprinting) return;
char autoname[10];
sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
if (!cardOK) initsd();
dir_t p;
root.rewind();
bool found = false;
while (root.readDir(p, NULL) > 0) {
for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
openAndPrintFile(autoname);
found = true;
if (cardOK) {
char autoname[10];
sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
dir_t p;
root.rewind();
while (root.readDir(p, NULL) > 0) {
for (int8_t i = (int8_t)strlen((char*)p.name); i--;) p.name[i] = tolower(p.name[i]);
if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
openAndPrintFile(autoname);
autostart_index++;
return;
}
}
}
if (!found)
autostart_index = -1;
else
autostart_index++;
autostart_index = -1;
}
void CardReader::closefile(bool store_location) {
void CardReader::beginautostart() {
autostart_index = 0;
setroot();
}
void CardReader::closefile(const bool store_location) {
file.sync();
file.close();
saving = logging = false;

View File

@ -39,16 +39,14 @@ public:
void initsd();
void write_command(char *buf);
// Files auto[0-9].g on the sd card are performed in sequence.
// This is to delay autostart and hence the initialisation of
// the sd card to some seconds after the normal init, so the
// device is available soon after a reset.
void checkautostart(bool x);
void beginautostart();
void checkautostart();
void openFile(char* name, const bool read, const bool subcall=false);
void openLogFile(char* name);
void removeFile(const char * const name);
void closefile(bool store_location=false);
void closefile(const bool store_location=false);
void release();
void openAndPrintFile(const char *name);
void startFileprint();
@ -212,9 +210,6 @@ private:
char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
uint32_t filesize, sdpos;
millis_t next_autostart_ms;
bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
LsAction lsAction; //stored for recursion.
uint16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
char* diveDirName;