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.

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