cardreader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. uint32_t CardReader::getFileSize()
  419. {
  420. return filesize;
  421. }
  422. void CardReader::getStatus()
  423. {
  424. if(sdprinting){
  425. SERIAL_PROTOCOL(longFilename);
  426. SERIAL_PROTOCOLPGM("\n");
  427. SERIAL_PROTOCOLRPGM(MSG_SD_PRINTING_BYTE);
  428. SERIAL_PROTOCOL(sdpos);
  429. SERIAL_PROTOCOLPGM("/");
  430. SERIAL_PROTOCOLLN(filesize);
  431. uint16_t time = millis()/60000 - starttime/60000;
  432. SERIAL_PROTOCOL(itostr2(time/60));
  433. SERIAL_PROTOCOL(':');
  434. SERIAL_PROTOCOL(itostr2(time%60));
  435. SERIAL_PROTOCOLPGM("\n");
  436. }
  437. else{
  438. SERIAL_PROTOCOLLNRPGM("Not printing");
  439. }
  440. }
  441. void CardReader::write_command(char *buf)
  442. {
  443. char* begin = buf;
  444. char* npos = 0;
  445. char* end = buf + strlen(buf) - 1;
  446. file.writeError = false;
  447. if((npos = strchr(buf, 'N')) != NULL)
  448. {
  449. begin = strchr(npos, ' ') + 1;
  450. end = strchr(npos, '*') - 1;
  451. }
  452. end[1] = '\r';
  453. end[2] = '\n';
  454. end[3] = '\0';
  455. file.write(begin);
  456. if (file.writeError)
  457. {
  458. SERIAL_ERROR_START;
  459. SERIAL_ERRORLNRPGM(MSG_SD_ERR_WRITE_TO_FILE);
  460. }
  461. }
  462. #define CHUNK_SIZE 64
  463. void CardReader::write_command_no_newline(char *buf)
  464. {
  465. file.write(buf, CHUNK_SIZE);
  466. if (file.writeError)
  467. {
  468. SERIAL_ERROR_START;
  469. SERIAL_ERRORLNRPGM(MSG_SD_ERR_WRITE_TO_FILE);
  470. MYSERIAL.println("An error while writing to the SD Card.");
  471. }
  472. }
  473. void CardReader::checkautostart(bool force)
  474. {
  475. if(!force)
  476. {
  477. if(!autostart_stilltocheck)
  478. return;
  479. if(autostart_atmillis<millis())
  480. return;
  481. }
  482. autostart_stilltocheck=false;
  483. if(!cardOK)
  484. {
  485. initsd();
  486. if(!cardOK) //fail
  487. return;
  488. }
  489. char autoname[30];
  490. sprintf_P(autoname, PSTR("auto%i.g"), lastnr);
  491. for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
  492. autoname[i]=tolower(autoname[i]);
  493. dir_t p;
  494. root.rewind();
  495. bool found=false;
  496. while (root.readDir(p, NULL) > 0)
  497. {
  498. for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
  499. p.name[i]=tolower(p.name[i]);
  500. //Serial.print((char*)p.name);
  501. //Serial.print(" ");
  502. //Serial.println(autoname);
  503. if(p.name[9]!='~') //skip safety copies
  504. if(strncmp((char*)p.name,autoname,5)==0)
  505. {
  506. char cmd[30];
  507. // M23: Select SD file
  508. sprintf_P(cmd, PSTR("M23 %s"), autoname);
  509. enquecommand(cmd);
  510. // M24: Start/resume SD print
  511. enquecommand_P(PSTR("M24"));
  512. found=true;
  513. }
  514. }
  515. if(!found)
  516. lastnr=-1;
  517. else
  518. lastnr++;
  519. }
  520. void CardReader::closefile(bool store_location)
  521. {
  522. file.sync();
  523. file.close();
  524. saving = false;
  525. logging = false;
  526. if(store_location)
  527. {
  528. //future: store printer state, filename and position for continuing a stopped print
  529. // so one can unplug the printer and continue printing the next day.
  530. }
  531. }
  532. void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
  533. {
  534. curDir=&workDir;
  535. lsAction=LS_GetFilename;
  536. nrFiles=nr;
  537. curDir->rewind();
  538. lsDive("",*curDir,match);
  539. }
  540. uint16_t CardReader::getnrfilenames()
  541. {
  542. curDir=&workDir;
  543. lsAction=LS_Count;
  544. nrFiles=0;
  545. curDir->rewind();
  546. lsDive("",*curDir);
  547. //SERIAL_ECHOLN(nrFiles);
  548. return nrFiles;
  549. }
  550. void CardReader::chdir(const char * relpath)
  551. {
  552. SdFile newfile;
  553. SdFile *parent=&root;
  554. if(workDir.isOpen())
  555. parent=&workDir;
  556. if(!newfile.open(*parent,relpath, O_READ))
  557. {
  558. SERIAL_ECHO_START;
  559. SERIAL_ECHORPGM(MSG_SD_CANT_ENTER_SUBDIR);
  560. SERIAL_ECHOLN(relpath);
  561. }
  562. else
  563. {
  564. if (workDirDepth < MAX_DIR_DEPTH) {
  565. for (int d = ++workDirDepth; d--;)
  566. workDirParents[d+1] = workDirParents[d];
  567. workDirParents[0]=*parent;
  568. }
  569. workDir=newfile;
  570. }
  571. }
  572. void CardReader::updir()
  573. {
  574. if(workDirDepth > 0)
  575. {
  576. --workDirDepth;
  577. workDir = workDirParents[0];
  578. int d;
  579. for (int d = 0; d < workDirDepth; d++)
  580. workDirParents[d] = workDirParents[d+1];
  581. }
  582. }
  583. void CardReader::printingHasFinished()
  584. {
  585. st_synchronize();
  586. if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
  587. {
  588. file.close();
  589. file_subcall_ctr--;
  590. openFile(filenames[file_subcall_ctr],true,true);
  591. setIndex(filespos[file_subcall_ctr]);
  592. startFileprint();
  593. }
  594. else
  595. {
  596. quickStop();
  597. file.close();
  598. sdprinting = false;
  599. if(SD_FINISHED_STEPPERRELEASE)
  600. {
  601. finishAndDisableSteppers();
  602. //enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  603. }
  604. autotempShutdown();
  605. }
  606. }
  607. bool CardReader::ToshibaFlashAir_GetIP(uint8_t *ip)
  608. {
  609. memset(ip, 0, 4);
  610. return card.readExtMemory(1, 1, 0x400+0x150, 4, ip);
  611. }
  612. #endif //SDSUPPORT