cardreader.cpp 14 KB

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