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

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