My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cardreader.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #include "Marlin.h"
  2. #include "cardreader.h"
  3. #include "ultralcd.h"
  4. #include "stepper.h"
  5. #include "temperature.h"
  6. #include "language.h"
  7. #ifdef SDSUPPORT
  8. CardReader::CardReader()
  9. {
  10. #ifdef SDCARD_SORT_ALPHA
  11. sort_count = 0;
  12. #endif
  13. filesize = 0;
  14. sdpos = 0;
  15. sdprinting = false;
  16. cardOK = false;
  17. saving = false;
  18. logging = false;
  19. autostart_atmillis=0;
  20. workDirDepth = 0;
  21. file_subcall_ctr=0;
  22. memset(workDirParents, 0, sizeof(workDirParents));
  23. autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
  24. lastnr=0;
  25. //power to SD reader
  26. #if SDPOWER > -1
  27. SET_OUTPUT(SDPOWER);
  28. WRITE(SDPOWER,HIGH);
  29. #endif //SDPOWER
  30. autostart_atmillis=millis()+5000;
  31. }
  32. char *createFilename(char *buffer, const dir_t &p) //buffer>12characters
  33. {
  34. char *pos=buffer;
  35. for (uint8_t i = 0; i < 11; i++) {
  36. if (p.name[i] == ' ') continue;
  37. if (i == 8) *pos++ = '.';
  38. *pos++ = p.name[i];
  39. }
  40. *pos++ = 0;
  41. return buffer;
  42. }
  43. void CardReader::lsDive(const char *prepend,SdFile parent)
  44. {
  45. dir_t p;
  46. uint8_t cnt=0;
  47. while (parent.readDir(p, longFilename) > 0)
  48. {
  49. if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
  50. {
  51. char path[FILENAME_LENGTH*2];
  52. char lfilename[FILENAME_LENGTH];
  53. createFilename(lfilename,p);
  54. path[0]=0;
  55. if(strlen(prepend)==0) //avoid leading / if already in prepend
  56. {
  57. strcat(path,"/");
  58. }
  59. strcat(path,prepend);
  60. strcat(path,lfilename);
  61. strcat(path,"/");
  62. //Serial.print(path);
  63. SdFile dir;
  64. if(!dir.open(parent,lfilename, O_READ))
  65. {
  66. if(lsAction==LS_SerialPrint)
  67. {
  68. SERIAL_ECHO_START;
  69. SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
  70. SERIAL_ECHOLN(lfilename);
  71. }
  72. }
  73. lsDive(path,dir);
  74. //close done automatically by destructor of SdFile
  75. }
  76. else
  77. {
  78. if (p.name[0] == DIR_NAME_FREE) break;
  79. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  80. if (longFilename[0] != '\0' &&
  81. (longFilename[0] == '.' || longFilename[0] == '_')) continue;
  82. if ( p.name[0] == '.')
  83. {
  84. if ( p.name[1] != '.')
  85. continue;
  86. }
  87. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  88. filenameIsDir=DIR_IS_SUBDIR(&p);
  89. if(!filenameIsDir)
  90. {
  91. if(p.name[8]!='G') continue;
  92. if(p.name[9]=='~') continue;
  93. }
  94. //if(cnt++!=nr) continue;
  95. createFilename(filename,p);
  96. if(lsAction==LS_SerialPrint)
  97. {
  98. SERIAL_PROTOCOL(prepend);
  99. SERIAL_PROTOCOLLN(filename);
  100. }
  101. else if(lsAction==LS_Count)
  102. {
  103. nrFiles++;
  104. }
  105. else if(lsAction==LS_GetFilename)
  106. {
  107. if (cnt == nrFiles) return;
  108. cnt++;
  109. }
  110. }
  111. }
  112. }
  113. void CardReader::ls()
  114. {
  115. lsAction=LS_SerialPrint;
  116. root.rewind();
  117. lsDive("",root);
  118. }
  119. void CardReader::initsd()
  120. {
  121. cardOK = false;
  122. if(root.isOpen())
  123. root.close();
  124. #ifdef SDSLOW
  125. if (!card.init(SPI_HALF_SPEED,SDSS))
  126. #else
  127. if (!card.init(SPI_FULL_SPEED,SDSS))
  128. #endif
  129. {
  130. //if (!card.init(SPI_HALF_SPEED,SDSS))
  131. SERIAL_ECHO_START;
  132. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  133. }
  134. else if (!volume.init(&card))
  135. {
  136. SERIAL_ERROR_START;
  137. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  138. }
  139. else if (!root.openRoot(&volume))
  140. {
  141. SERIAL_ERROR_START;
  142. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  143. }
  144. else
  145. {
  146. cardOK = true;
  147. SERIAL_ECHO_START;
  148. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  149. }
  150. workDir=root;
  151. curDir=&root;
  152. #ifdef SDCARD_SORT_ALPHA
  153. presort();
  154. #endif
  155. /*
  156. if(!workDir.openRoot(&volume))
  157. {
  158. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  159. }
  160. */
  161. }
  162. void CardReader::setroot()
  163. {
  164. /*if(!workDir.openRoot(&volume))
  165. {
  166. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  167. }*/
  168. workDir=root;
  169. curDir=&workDir;
  170. #ifdef SDCARD_SORT_ALPHA
  171. presort();
  172. #endif
  173. }
  174. void CardReader::release()
  175. {
  176. sdprinting = false;
  177. cardOK = false;
  178. }
  179. void CardReader::startFileprint()
  180. {
  181. if(cardOK)
  182. {
  183. sdprinting = true;
  184. flush_presort();
  185. }
  186. }
  187. void CardReader::pauseSDPrint()
  188. {
  189. if(sdprinting)
  190. {
  191. sdprinting = false;
  192. }
  193. }
  194. void CardReader::openLogFile(char* name)
  195. {
  196. logging = true;
  197. openFile(name, false);
  198. }
  199. void CardReader::getAbsFilename(char *t)
  200. {
  201. uint8_t cnt=0;
  202. *t='/';t++;cnt++;
  203. for(uint8_t i=0;i<workDirDepth;i++)
  204. {
  205. workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
  206. while(*t!=0 && cnt< MAXPATHNAMELENGTH)
  207. {t++;cnt++;} //crawl counter forward.
  208. }
  209. if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
  210. file.getFilename(t);
  211. else
  212. t[0]=0;
  213. }
  214. void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
  215. {
  216. if(!cardOK)
  217. return;
  218. if(file.isOpen()) //replacing current file by new file, or subfile call
  219. {
  220. if(!replace_current)
  221. {
  222. if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
  223. {
  224. SERIAL_ERROR_START;
  225. SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
  226. SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
  227. kill();
  228. return;
  229. }
  230. SERIAL_ECHO_START;
  231. SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
  232. SERIAL_ECHO(name);
  233. SERIAL_ECHOPGM("\" parent:\"");
  234. //store current filename and position
  235. getAbsFilename(filenames[file_subcall_ctr]);
  236. SERIAL_ECHO(filenames[file_subcall_ctr]);
  237. SERIAL_ECHOPGM("\" pos");
  238. SERIAL_ECHOLN(sdpos);
  239. filespos[file_subcall_ctr]=sdpos;
  240. file_subcall_ctr++;
  241. }
  242. else
  243. {
  244. SERIAL_ECHO_START;
  245. SERIAL_ECHOPGM("Now doing file: ");
  246. SERIAL_ECHOLN(name);
  247. }
  248. file.close();
  249. }
  250. else //opening fresh file
  251. {
  252. file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
  253. SERIAL_ECHO_START;
  254. SERIAL_ECHOPGM("Now fresh file: ");
  255. SERIAL_ECHOLN(name);
  256. }
  257. sdprinting = false;
  258. SdFile myDir;
  259. curDir=&root;
  260. char *fname=name;
  261. char *dirname_start,*dirname_end;
  262. if(name[0]=='/')
  263. {
  264. dirname_start=strchr(name,'/')+1;
  265. while(dirname_start>0)
  266. {
  267. dirname_end=strchr(dirname_start,'/');
  268. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  269. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  270. if(dirname_end>0 && dirname_end>dirname_start)
  271. {
  272. char subdirname[FILENAME_LENGTH];
  273. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  274. subdirname[dirname_end-dirname_start]=0;
  275. SERIAL_ECHOLN(subdirname);
  276. if(!myDir.open(curDir,subdirname,O_READ))
  277. {
  278. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  279. SERIAL_PROTOCOL(subdirname);
  280. SERIAL_PROTOCOLLNPGM(".");
  281. return;
  282. }
  283. else
  284. {
  285. //SERIAL_ECHOLN("dive ok");
  286. }
  287. curDir=&myDir;
  288. dirname_start=dirname_end+1;
  289. }
  290. else // the reminder after all /fsa/fdsa/ is the filename
  291. {
  292. fname=dirname_start;
  293. //SERIAL_ECHOLN("remaider");
  294. //SERIAL_ECHOLN(fname);
  295. break;
  296. }
  297. }
  298. }
  299. else //relative path
  300. {
  301. curDir=&workDir;
  302. }
  303. if(read)
  304. {
  305. if (file.open(curDir, fname, O_READ))
  306. {
  307. filesize = file.fileSize();
  308. SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
  309. SERIAL_PROTOCOL(fname);
  310. SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
  311. SERIAL_PROTOCOLLN(filesize);
  312. sdpos = 0;
  313. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  314. lcd_setstatus(fname);
  315. }
  316. else
  317. {
  318. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  319. SERIAL_PROTOCOL(fname);
  320. SERIAL_PROTOCOLLNPGM(".");
  321. }
  322. }
  323. else
  324. { //write
  325. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  326. {
  327. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  328. SERIAL_PROTOCOL(fname);
  329. SERIAL_PROTOCOLLNPGM(".");
  330. }
  331. else
  332. {
  333. saving = true;
  334. SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
  335. SERIAL_PROTOCOLLN(name);
  336. lcd_setstatus(fname);
  337. }
  338. }
  339. }
  340. void CardReader::removeFile(char* name)
  341. {
  342. if(!cardOK)
  343. return;
  344. file.close();
  345. sdprinting = false;
  346. SdFile myDir;
  347. curDir=&root;
  348. char *fname=name;
  349. char *dirname_start,*dirname_end;
  350. if(name[0]=='/')
  351. {
  352. dirname_start=strchr(name,'/')+1;
  353. while(dirname_start>0)
  354. {
  355. dirname_end=strchr(dirname_start,'/');
  356. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  357. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  358. if(dirname_end>0 && dirname_end>dirname_start)
  359. {
  360. char subdirname[FILENAME_LENGTH];
  361. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  362. subdirname[dirname_end-dirname_start]=0;
  363. SERIAL_ECHOLN(subdirname);
  364. if(!myDir.open(curDir,subdirname,O_READ))
  365. {
  366. SERIAL_PROTOCOLPGM("open failed, File: ");
  367. SERIAL_PROTOCOL(subdirname);
  368. SERIAL_PROTOCOLLNPGM(".");
  369. return;
  370. }
  371. else
  372. {
  373. //SERIAL_ECHOLN("dive ok");
  374. }
  375. curDir=&myDir;
  376. dirname_start=dirname_end+1;
  377. }
  378. else // the reminder after all /fsa/fdsa/ is the filename
  379. {
  380. fname=dirname_start;
  381. //SERIAL_ECHOLN("remaider");
  382. //SERIAL_ECHOLN(fname);
  383. break;
  384. }
  385. }
  386. }
  387. else //relative path
  388. {
  389. curDir=&workDir;
  390. }
  391. if (file.remove(curDir, fname))
  392. {
  393. SERIAL_PROTOCOLPGM("File deleted:");
  394. SERIAL_PROTOCOLLN(fname);
  395. sdpos = 0;
  396. #ifdef SDCARD_SORT_ALPHA
  397. presort();
  398. #endif
  399. }
  400. else
  401. {
  402. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  403. SERIAL_PROTOCOL(fname);
  404. SERIAL_PROTOCOLLNPGM(".");
  405. }
  406. }
  407. void CardReader::getStatus()
  408. {
  409. if(cardOK){
  410. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  411. SERIAL_PROTOCOL(sdpos);
  412. SERIAL_PROTOCOLPGM("/");
  413. SERIAL_PROTOCOLLN(filesize);
  414. }
  415. else{
  416. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  417. }
  418. }
  419. void CardReader::write_command(char *buf)
  420. {
  421. char* begin = buf;
  422. char* npos = 0;
  423. char* end = buf + strlen(buf) - 1;
  424. file.writeError = false;
  425. if((npos = strchr(buf, 'N')) != NULL)
  426. {
  427. begin = strchr(npos, ' ') + 1;
  428. end = strchr(npos, '*') - 1;
  429. }
  430. end[1] = '\r';
  431. end[2] = '\n';
  432. end[3] = '\0';
  433. file.write(begin);
  434. if (file.writeError)
  435. {
  436. SERIAL_ERROR_START;
  437. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  438. }
  439. }
  440. void CardReader::checkautostart(bool force)
  441. {
  442. if(!force)
  443. {
  444. if(!autostart_stilltocheck)
  445. return;
  446. if(autostart_atmillis<millis())
  447. return;
  448. }
  449. autostart_stilltocheck=false;
  450. if(!cardOK)
  451. {
  452. initsd();
  453. if(!cardOK) //fail
  454. return;
  455. }
  456. char autoname[30];
  457. sprintf_P(autoname, PSTR("auto%i.g"), lastnr);
  458. for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
  459. autoname[i]=tolower(autoname[i]);
  460. dir_t p;
  461. root.rewind();
  462. bool found=false;
  463. while (root.readDir(p, NULL) > 0)
  464. {
  465. for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
  466. p.name[i]=tolower(p.name[i]);
  467. //Serial.print((char*)p.name);
  468. //Serial.print(" ");
  469. //Serial.println(autoname);
  470. if(p.name[9]!='~') //skip safety copies
  471. if(strncmp((char*)p.name,autoname,5)==0)
  472. {
  473. char cmd[30];
  474. sprintf_P(cmd, PSTR("M23 %s"), autoname);
  475. enquecommand(cmd);
  476. enquecommand_P(PSTR("M24"));
  477. found=true;
  478. }
  479. }
  480. if(!found)
  481. lastnr=-1;
  482. else
  483. lastnr++;
  484. }
  485. void CardReader::closefile(bool store_location)
  486. {
  487. file.sync();
  488. file.close();
  489. saving = false;
  490. logging = false;
  491. if(store_location)
  492. {
  493. //future: store printer state, filename and position for continuing a stopped print
  494. // so one can unplug the printer and continue printing the next day.
  495. }
  496. }
  497. void CardReader::getfilename(const uint16_t nr)
  498. {
  499. #if defined(SDCARD_SORT_ALPHA) && SORT_USES_RAM && SORT_USES_MORE_RAM
  500. if (nr < sort_count) {
  501. strcpy(filename, sortshort[nr]);
  502. strcpy(longFilename, sortnames[nr]);
  503. filenameIsDir = isDir[nr];
  504. return;
  505. }
  506. #endif
  507. curDir=&workDir;
  508. lsAction=LS_GetFilename;
  509. nrFiles=nr;
  510. curDir->rewind();
  511. lsDive("",*curDir);
  512. }
  513. uint16_t CardReader::getnrfilenames()
  514. {
  515. curDir=&workDir;
  516. lsAction=LS_Count;
  517. nrFiles=0;
  518. curDir->rewind();
  519. lsDive("",*curDir);
  520. //SERIAL_ECHOLN(nrFiles);
  521. return nrFiles;
  522. }
  523. void CardReader::chdir(const char * relpath)
  524. {
  525. SdFile newfile;
  526. SdFile *parent=&root;
  527. if(workDir.isOpen())
  528. parent=&workDir;
  529. if(!newfile.open(*parent,relpath, O_READ))
  530. {
  531. SERIAL_ECHO_START;
  532. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  533. SERIAL_ECHOLN(relpath);
  534. }
  535. else
  536. {
  537. if (workDirDepth < MAX_DIR_DEPTH) {
  538. for (int d = ++workDirDepth; d--;)
  539. workDirParents[d+1] = workDirParents[d];
  540. workDirParents[0]=*parent;
  541. }
  542. workDir=newfile;
  543. #ifdef SDCARD_SORT_ALPHA
  544. presort();
  545. #endif
  546. }
  547. }
  548. void CardReader::updir()
  549. {
  550. if (workDirDepth > 0)
  551. {
  552. --workDirDepth;
  553. workDir = workDirParents[0];
  554. for (int d = 0; d < workDirDepth; d++)
  555. workDirParents[d] = workDirParents[d+1];
  556. #ifdef SDCARD_SORT_ALPHA
  557. presort();
  558. #endif
  559. }
  560. }
  561. #ifdef SDCARD_SORT_ALPHA
  562. /**
  563. * Get the name of a file in the current directory by sort-index
  564. */
  565. void CardReader::getfilename_sorted(const uint16_t nr) {
  566. getfilename(nr < sort_count ? sort_order[nr] : nr);
  567. }
  568. /**
  569. * Read all the files and produce a sort key
  570. *
  571. * We can do this in 3 ways...
  572. * - Minimal RAM: Read two filenames at a time sorting along...
  573. * - Some RAM: Buffer the directory and return filenames from RAM
  574. * - Some RAM: Buffer the directory just for this sort
  575. */
  576. void CardReader::presort()
  577. {
  578. flush_presort();
  579. uint16_t fileCnt = getnrfilenames();
  580. if (fileCnt > 0) {
  581. if (fileCnt > SORT_LIMIT) fileCnt = SORT_LIMIT;
  582. #if SORT_USES_RAM
  583. #if SORT_USES_MORE_RAM
  584. sortshort = (char**)calloc(fileCnt, sizeof(char*));
  585. sortnames = (char**)calloc(fileCnt, sizeof(char*));
  586. #else
  587. char *sortnames[fileCnt];
  588. #endif
  589. #else
  590. char name1[LONG_FILENAME_LENGTH+1];
  591. #endif
  592. #if FOLDER_SORTING != 0
  593. #if SORT_USES_RAM && SORT_USES_MORE_RAM
  594. isDir = (uint8_t*)calloc(fileCnt, sizeof(uint8_t));
  595. #else
  596. uint8_t isDir[fileCnt];
  597. #endif
  598. #endif
  599. sort_order = new uint8_t[fileCnt];
  600. if (fileCnt > 1) {
  601. // Init sort order. If using RAM then read all filenames now.
  602. for (uint16_t i=0; i<fileCnt; i++) {
  603. sort_order[i] = i;
  604. #if SORT_USES_RAM
  605. getfilename(i);
  606. sortnames[i] = strdup(longFilename[0] ? longFilename : filename);
  607. #if SORT_USES_MORE_RAM
  608. sortshort[i] = strdup(filename);
  609. #endif
  610. // char out[30];
  611. // sprintf_P(out, PSTR("---- %i %s %s"), i, filenameIsDir ? "D" : " ", sortnames[i]);
  612. // SERIAL_ECHOLN(out);
  613. #if FOLDER_SORTING != 0
  614. isDir[i] = filenameIsDir;
  615. #endif
  616. #endif
  617. }
  618. // Bubble Sort
  619. for (uint16_t i=fileCnt; --i;) {
  620. bool cmp, didSwap = false;
  621. for (uint16_t j=0; j<i; ++j) {
  622. uint16_t s1 = j, s2 = j+1, o1 = sort_order[s1], o2 = sort_order[s2];
  623. #if SORT_USES_RAM
  624. #if FOLDER_SORTING != 0
  625. cmp = (isDir[o1] == isDir[o2]) ? (strcasecmp(sortnames[o1], sortnames[o2]) > 0) : isDir[FOLDER_SORTING > 0 ? o1 : o2];
  626. #else
  627. cmp = strcasecmp(sortnames[o1], sortnames[o2]) > 0;
  628. #endif
  629. #else
  630. getfilename(o1);
  631. strcpy(name1, longFilename[0] ? longFilename : filename);
  632. #if FOLDER_SORTING != 0
  633. bool dir1 = filenameIsDir;
  634. #endif
  635. getfilename(o2);
  636. char *name2 = longFilename[0] ? longFilename : filename;
  637. #if FOLDER_SORTING != 0
  638. cmp = (dir1 == filenameIsDir) ? (strcasecmp(name1, name2) > 0) : (FOLDER_SORTING > 0 ? dir1 : !dir1);
  639. #else
  640. cmp = strcasecmp(name1, name2) > 0;
  641. #endif
  642. #endif
  643. if (cmp) {
  644. sort_order[s1] = o2;
  645. sort_order[s2] = o1;
  646. didSwap = true;
  647. }
  648. }
  649. if (!didSwap) break;
  650. }
  651. #if SORT_USES_RAM && !SORT_USES_MORE_RAM
  652. for (uint16_t i=0; i<fileCnt; ++i) free(sortnames[i]);
  653. #endif
  654. }
  655. else {
  656. sort_order[0] = 0;
  657. #if SORT_USES_RAM && SORT_USES_MORE_RAM
  658. sortnames = (char**)malloc(sizeof(char*));
  659. sortshort = (char**)malloc(sizeof(char*));
  660. isDir = (uint8_t*)malloc(sizeof(uint8_t));
  661. getfilename(0);
  662. sortnames[0] = strdup(longFilename[0] ? longFilename : filename);
  663. sortshort[0] = strdup(filename);
  664. isDir[0] = filenameIsDir;
  665. #endif
  666. }
  667. sort_count = fileCnt;
  668. }
  669. }
  670. void CardReader::flush_presort() {
  671. if (sort_count > 0) {
  672. #if SORT_USES_RAM && SORT_USES_MORE_RAM
  673. for (uint8_t i=0; i<sort_count; ++i) {
  674. free(sortshort[i]);
  675. free(sortnames[i]);
  676. }
  677. free(sortshort);
  678. free(sortnames);
  679. #endif
  680. delete sort_order;
  681. sort_count = 0;
  682. }
  683. }
  684. #endif // SDCARD_SORT_ALPHA
  685. void CardReader::printingHasFinished()
  686. {
  687. st_synchronize();
  688. if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
  689. {
  690. file.close();
  691. file_subcall_ctr--;
  692. openFile(filenames[file_subcall_ctr],true,true);
  693. setIndex(filespos[file_subcall_ctr]);
  694. startFileprint();
  695. }
  696. else
  697. {
  698. quickStop();
  699. file.close();
  700. sdprinting = false;
  701. if(SD_FINISHED_STEPPERRELEASE)
  702. {
  703. //finishAndDisableSteppers();
  704. enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  705. }
  706. autotempShutdown();
  707. #ifdef SDCARD_SORT_ALPHA
  708. presort();
  709. #endif
  710. }
  711. }
  712. #endif //SDSUPPORT