cardreader.cpp 14 KB

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