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

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