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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. filesize = 0;
  11. sdpos = 0;
  12. sdprinting = false;
  13. cardOK = false;
  14. saving = false;
  15. logging = false;
  16. autostart_atmillis=0;
  17. workDirDepth = 0;
  18. file_subcall_ctr=0;
  19. memset(workDirParents, 0, sizeof(workDirParents));
  20. autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
  21. lastnr=0;
  22. //power to SD reader
  23. #if SDPOWER > -1
  24. SET_OUTPUT(SDPOWER);
  25. WRITE(SDPOWER,HIGH);
  26. #endif //SDPOWER
  27. autostart_atmillis=millis()+5000;
  28. }
  29. char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
  30. {
  31. char *pos=buffer;
  32. for (uint8_t i = 0; i < 11; i++)
  33. {
  34. if (p.name[i] == ' ')continue;
  35. if (i == 8)
  36. {
  37. *pos++='.';
  38. }
  39. *pos++=p.name[i];
  40. }
  41. *pos++=0;
  42. return buffer;
  43. }
  44. void CardReader::lsDive(const char *prepend,SdFile parent)
  45. {
  46. dir_t p;
  47. uint8_t cnt=0;
  48. while (parent.readDir(p, longFilename) > 0)
  49. {
  50. if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
  51. {
  52. char path[13*2];
  53. char lfilename[13];
  54. createFilename(lfilename,p);
  55. path[0]=0;
  56. if(strlen(prepend)==0) //avoid leading / if already in prepend
  57. {
  58. strcat(path,"/");
  59. }
  60. strcat(path,prepend);
  61. strcat(path,lfilename);
  62. strcat(path,"/");
  63. //Serial.print(path);
  64. SdFile dir;
  65. if(!dir.open(parent,lfilename, O_READ))
  66. {
  67. if(lsAction==LS_SerialPrint)
  68. {
  69. SERIAL_ECHO_START;
  70. SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
  71. SERIAL_ECHOLN(lfilename);
  72. }
  73. }
  74. lsDive(path,dir);
  75. //close done automatically by destructor of SdFile
  76. }
  77. else
  78. {
  79. if (p.name[0] == DIR_NAME_FREE) break;
  80. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  81. if (longFilename[0] != '\0' &&
  82. (longFilename[0] == '.' || longFilename[0] == '_')) continue;
  83. if ( p.name[0] == '.')
  84. {
  85. if ( p.name[1] != '.')
  86. continue;
  87. }
  88. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  89. filenameIsDir=DIR_IS_SUBDIR(&p);
  90. if(!filenameIsDir)
  91. {
  92. if(p.name[8]!='G') continue;
  93. if(p.name[9]=='~') continue;
  94. }
  95. //if(cnt++!=nr) continue;
  96. createFilename(filename,p);
  97. if(lsAction==LS_SerialPrint)
  98. {
  99. SERIAL_PROTOCOL(prepend);
  100. SERIAL_PROTOCOLLN(filename);
  101. }
  102. else if(lsAction==LS_Count)
  103. {
  104. nrFiles++;
  105. }
  106. else if(lsAction==LS_GetFilename)
  107. {
  108. if(cnt==nrFiles)
  109. return;
  110. cnt++;
  111. }
  112. }
  113. }
  114. }
  115. void CardReader::ls()
  116. {
  117. lsAction=LS_SerialPrint;
  118. if(lsAction==LS_Count)
  119. nrFiles=0;
  120. root.rewind();
  121. lsDive("",root);
  122. }
  123. void CardReader::initsd()
  124. {
  125. cardOK = false;
  126. if(root.isOpen())
  127. root.close();
  128. #ifdef SDSLOW
  129. if (!card.init(SPI_HALF_SPEED,SDSS))
  130. #else
  131. if (!card.init(SPI_FULL_SPEED,SDSS))
  132. #endif
  133. {
  134. //if (!card.init(SPI_HALF_SPEED,SDSS))
  135. SERIAL_ECHO_START;
  136. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  137. }
  138. else if (!volume.init(&card))
  139. {
  140. SERIAL_ERROR_START;
  141. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  142. }
  143. else if (!root.openRoot(&volume))
  144. {
  145. SERIAL_ERROR_START;
  146. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  147. }
  148. else
  149. {
  150. cardOK = true;
  151. SERIAL_ECHO_START;
  152. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  153. }
  154. workDir=root;
  155. curDir=&root;
  156. /*
  157. if(!workDir.openRoot(&volume))
  158. {
  159. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  160. }
  161. */
  162. }
  163. void CardReader::setroot()
  164. {
  165. /*if(!workDir.openRoot(&volume))
  166. {
  167. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  168. }*/
  169. workDir=root;
  170. curDir=&workDir;
  171. }
  172. void CardReader::release()
  173. {
  174. sdprinting = false;
  175. cardOK = false;
  176. }
  177. void CardReader::startFileprint()
  178. {
  179. if(cardOK)
  180. {
  181. sdprinting = true;
  182. }
  183. }
  184. void CardReader::pauseSDPrint()
  185. {
  186. if(sdprinting)
  187. {
  188. sdprinting = false;
  189. }
  190. }
  191. void CardReader::openLogFile(char* name)
  192. {
  193. logging = true;
  194. openFile(name, false);
  195. }
  196. void CardReader::getAbsFilename(char *t)
  197. {
  198. uint8_t cnt=0;
  199. *t='/';t++;cnt++;
  200. for(uint8_t i=0;i<workDirDepth;i++)
  201. {
  202. workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
  203. while(*t!=0 && cnt< MAXPATHNAMELENGTH)
  204. {t++;cnt++;} //crawl counter forward.
  205. }
  206. if(cnt<MAXPATHNAMELENGTH-13)
  207. file.getFilename(t);
  208. else
  209. t[0]=0;
  210. }
  211. void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
  212. {
  213. if(!cardOK)
  214. return;
  215. if(file.isOpen()) //replacing current file by new file, or subfile call
  216. {
  217. if(!replace_current)
  218. {
  219. if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
  220. {
  221. SERIAL_ERROR_START;
  222. SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
  223. SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
  224. kill();
  225. return;
  226. }
  227. SERIAL_ECHO_START;
  228. SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
  229. SERIAL_ECHO(name);
  230. SERIAL_ECHOPGM("\" parent:\"");
  231. //store current filename and position
  232. getAbsFilename(filenames[file_subcall_ctr]);
  233. SERIAL_ECHO(filenames[file_subcall_ctr]);
  234. SERIAL_ECHOPGM("\" pos");
  235. SERIAL_ECHOLN(sdpos);
  236. filespos[file_subcall_ctr]=sdpos;
  237. file_subcall_ctr++;
  238. }
  239. else
  240. {
  241. SERIAL_ECHO_START;
  242. SERIAL_ECHOPGM("Now doing file: ");
  243. SERIAL_ECHOLN(name);
  244. }
  245. file.close();
  246. }
  247. else //opening fresh file
  248. {
  249. file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
  250. SERIAL_ECHO_START;
  251. SERIAL_ECHOPGM("Now fresh file: ");
  252. SERIAL_ECHOLN(name);
  253. }
  254. sdprinting = false;
  255. SdFile myDir;
  256. curDir=&root;
  257. char *fname=name;
  258. char *dirname_start,*dirname_end;
  259. if(name[0]=='/')
  260. {
  261. dirname_start=strchr(name,'/')+1;
  262. while(dirname_start>0)
  263. {
  264. dirname_end=strchr(dirname_start,'/');
  265. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  266. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  267. if(dirname_end>0 && dirname_end>dirname_start)
  268. {
  269. char subdirname[13];
  270. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  271. subdirname[dirname_end-dirname_start]=0;
  272. SERIAL_ECHOLN(subdirname);
  273. if(!myDir.open(curDir,subdirname,O_READ))
  274. {
  275. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  276. SERIAL_PROTOCOL(subdirname);
  277. SERIAL_PROTOCOLLNPGM(".");
  278. return;
  279. }
  280. else
  281. {
  282. //SERIAL_ECHOLN("dive ok");
  283. }
  284. curDir=&myDir;
  285. dirname_start=dirname_end+1;
  286. }
  287. else // the reminder after all /fsa/fdsa/ is the filename
  288. {
  289. fname=dirname_start;
  290. //SERIAL_ECHOLN("remaider");
  291. //SERIAL_ECHOLN(fname);
  292. break;
  293. }
  294. }
  295. }
  296. else //relative path
  297. {
  298. curDir=&workDir;
  299. }
  300. if(read)
  301. {
  302. if (file.open(curDir, fname, O_READ))
  303. {
  304. filesize = file.fileSize();
  305. SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
  306. SERIAL_PROTOCOL(fname);
  307. SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
  308. SERIAL_PROTOCOLLN(filesize);
  309. sdpos = 0;
  310. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  311. lcd_setstatus(fname);
  312. }
  313. else
  314. {
  315. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  316. SERIAL_PROTOCOL(fname);
  317. SERIAL_PROTOCOLLNPGM(".");
  318. }
  319. }
  320. else
  321. { //write
  322. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  323. {
  324. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  325. SERIAL_PROTOCOL(fname);
  326. SERIAL_PROTOCOLLNPGM(".");
  327. }
  328. else
  329. {
  330. saving = true;
  331. SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
  332. SERIAL_PROTOCOLLN(name);
  333. lcd_setstatus(fname);
  334. }
  335. }
  336. }
  337. void CardReader::removeFile(char* name)
  338. {
  339. if(!cardOK)
  340. return;
  341. file.close();
  342. sdprinting = false;
  343. SdFile myDir;
  344. curDir=&root;
  345. char *fname=name;
  346. char *dirname_start,*dirname_end;
  347. if(name[0]=='/')
  348. {
  349. dirname_start=strchr(name,'/')+1;
  350. while(dirname_start>0)
  351. {
  352. dirname_end=strchr(dirname_start,'/');
  353. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  354. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  355. if(dirname_end>0 && dirname_end>dirname_start)
  356. {
  357. char subdirname[13];
  358. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  359. subdirname[dirname_end-dirname_start]=0;
  360. SERIAL_ECHOLN(subdirname);
  361. if(!myDir.open(curDir,subdirname,O_READ))
  362. {
  363. SERIAL_PROTOCOLPGM("open failed, File: ");
  364. SERIAL_PROTOCOL(subdirname);
  365. SERIAL_PROTOCOLLNPGM(".");
  366. return;
  367. }
  368. else
  369. {
  370. //SERIAL_ECHOLN("dive ok");
  371. }
  372. curDir=&myDir;
  373. dirname_start=dirname_end+1;
  374. }
  375. else // the reminder after all /fsa/fdsa/ is the filename
  376. {
  377. fname=dirname_start;
  378. //SERIAL_ECHOLN("remaider");
  379. //SERIAL_ECHOLN(fname);
  380. break;
  381. }
  382. }
  383. }
  384. else //relative path
  385. {
  386. curDir=&workDir;
  387. }
  388. if (file.remove(curDir, fname))
  389. {
  390. SERIAL_PROTOCOLPGM("File deleted:");
  391. SERIAL_PROTOCOLLN(fname);
  392. sdpos = 0;
  393. }
  394. else
  395. {
  396. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  397. SERIAL_PROTOCOL(fname);
  398. SERIAL_PROTOCOLLNPGM(".");
  399. }
  400. }
  401. void CardReader::getStatus()
  402. {
  403. if(cardOK){
  404. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  405. SERIAL_PROTOCOL(sdpos);
  406. SERIAL_PROTOCOLPGM("/");
  407. SERIAL_PROTOCOLLN(filesize);
  408. }
  409. else{
  410. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  411. }
  412. }
  413. void CardReader::write_command(char *buf)
  414. {
  415. char* begin = buf;
  416. char* npos = 0;
  417. char* end = buf + strlen(buf) - 1;
  418. file.writeError = false;
  419. if((npos = strchr(buf, 'N')) != NULL)
  420. {
  421. begin = strchr(npos, ' ') + 1;
  422. end = strchr(npos, '*') - 1;
  423. }
  424. end[1] = '\r';
  425. end[2] = '\n';
  426. end[3] = '\0';
  427. file.write(begin);
  428. if (file.writeError)
  429. {
  430. SERIAL_ERROR_START;
  431. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  432. }
  433. }
  434. void CardReader::checkautostart(bool force)
  435. {
  436. if(!force)
  437. {
  438. if(!autostart_stilltocheck)
  439. return;
  440. if(autostart_atmillis<millis())
  441. return;
  442. }
  443. autostart_stilltocheck=false;
  444. if(!cardOK)
  445. {
  446. initsd();
  447. if(!cardOK) //fail
  448. return;
  449. }
  450. char autoname[30];
  451. sprintf_P(autoname, PSTR("auto%i.g"), lastnr);
  452. for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
  453. autoname[i]=tolower(autoname[i]);
  454. dir_t p;
  455. root.rewind();
  456. bool found=false;
  457. while (root.readDir(p, NULL) > 0)
  458. {
  459. for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
  460. p.name[i]=tolower(p.name[i]);
  461. //Serial.print((char*)p.name);
  462. //Serial.print(" ");
  463. //Serial.println(autoname);
  464. if(p.name[9]!='~') //skip safety copies
  465. if(strncmp((char*)p.name,autoname,5)==0)
  466. {
  467. char cmd[30];
  468. sprintf_P(cmd, PSTR("M23 %s"), autoname);
  469. enquecommand(cmd);
  470. enquecommand_P(PSTR("M24"));
  471. found=true;
  472. }
  473. }
  474. if(!found)
  475. lastnr=-1;
  476. else
  477. lastnr++;
  478. }
  479. void CardReader::closefile(bool store_location)
  480. {
  481. file.sync();
  482. file.close();
  483. saving = false;
  484. logging = false;
  485. if(store_location)
  486. {
  487. //future: store printer state, filename and position for continuing a stopped print
  488. // so one can unplug the printer and continue printing the next day.
  489. }
  490. }
  491. void CardReader::getfilename(const uint8_t nr)
  492. {
  493. curDir=&workDir;
  494. lsAction=LS_GetFilename;
  495. nrFiles=nr;
  496. curDir->rewind();
  497. lsDive("",*curDir);
  498. }
  499. uint16_t CardReader::getnrfilenames()
  500. {
  501. curDir=&workDir;
  502. lsAction=LS_Count;
  503. nrFiles=0;
  504. curDir->rewind();
  505. lsDive("",*curDir);
  506. //SERIAL_ECHOLN(nrFiles);
  507. return nrFiles;
  508. }
  509. void CardReader::chdir(const char * relpath)
  510. {
  511. SdFile newfile;
  512. SdFile *parent=&root;
  513. if(workDir.isOpen())
  514. parent=&workDir;
  515. if(!newfile.open(*parent,relpath, O_READ))
  516. {
  517. SERIAL_ECHO_START;
  518. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  519. SERIAL_ECHOLN(relpath);
  520. }
  521. else
  522. {
  523. if (workDirDepth < MAX_DIR_DEPTH) {
  524. for (int d = ++workDirDepth; d--;)
  525. workDirParents[d+1] = workDirParents[d];
  526. workDirParents[0]=*parent;
  527. }
  528. workDir=newfile;
  529. }
  530. }
  531. void CardReader::updir()
  532. {
  533. if(workDirDepth > 0)
  534. {
  535. --workDirDepth;
  536. workDir = workDirParents[0];
  537. int d;
  538. for (int d = 0; d < workDirDepth; d++)
  539. workDirParents[d] = workDirParents[d+1];
  540. }
  541. }
  542. void CardReader::printingHasFinished()
  543. {
  544. st_synchronize();
  545. if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
  546. {
  547. file.close();
  548. file_subcall_ctr--;
  549. openFile(filenames[file_subcall_ctr],true,true);
  550. setIndex(filespos[file_subcall_ctr]);
  551. startFileprint();
  552. }
  553. else
  554. {
  555. quickStop();
  556. file.close();
  557. sdprinting = false;
  558. if(SD_FINISHED_STEPPERRELEASE)
  559. {
  560. //finishAndDisableSteppers();
  561. enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  562. }
  563. autotempShutdown();
  564. }
  565. }
  566. #endif //SDSUPPORT