My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cardreader.cpp 18KB

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