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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. if (!card.init(SPI_FULL_SPEED,SDSS))
  126. {
  127. //if (!card.init(SPI_HALF_SPEED,SDSS))
  128. SERIAL_ECHO_START;
  129. SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
  130. }
  131. else if (!volume.init(&card))
  132. {
  133. SERIAL_ERROR_START;
  134. SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
  135. }
  136. else if (!root.openRoot(&volume))
  137. {
  138. SERIAL_ERROR_START;
  139. SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
  140. }
  141. else
  142. {
  143. cardOK = true;
  144. SERIAL_ECHO_START;
  145. SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
  146. }
  147. workDir=root;
  148. curDir=&root;
  149. /*
  150. if(!workDir.openRoot(&volume))
  151. {
  152. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  153. }
  154. */
  155. }
  156. void CardReader::setroot()
  157. {
  158. /*if(!workDir.openRoot(&volume))
  159. {
  160. SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
  161. }*/
  162. workDir=root;
  163. curDir=&workDir;
  164. }
  165. void CardReader::release()
  166. {
  167. sdprinting = false;
  168. cardOK = false;
  169. }
  170. void CardReader::startFileprint()
  171. {
  172. if(cardOK)
  173. {
  174. sdprinting = true;
  175. }
  176. }
  177. void CardReader::pauseSDPrint()
  178. {
  179. if(sdprinting)
  180. {
  181. sdprinting = false;
  182. }
  183. }
  184. void CardReader::openLogFile(char* name)
  185. {
  186. logging = true;
  187. openFile(name, false);
  188. }
  189. void CardReader::openFile(char* name,bool read)
  190. {
  191. if(!cardOK)
  192. return;
  193. file.close();
  194. sdprinting = false;
  195. SdFile myDir;
  196. curDir=&root;
  197. char *fname=name;
  198. char *dirname_start,*dirname_end;
  199. if(name[0]=='/')
  200. {
  201. dirname_start=strchr(name,'/')+1;
  202. while(dirname_start>0)
  203. {
  204. dirname_end=strchr(dirname_start,'/');
  205. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  206. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  207. if(dirname_end>0 && dirname_end>dirname_start)
  208. {
  209. char subdirname[13];
  210. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  211. subdirname[dirname_end-dirname_start]=0;
  212. SERIAL_ECHOLN(subdirname);
  213. if(!myDir.open(curDir,subdirname,O_READ))
  214. {
  215. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  216. SERIAL_PROTOCOL(subdirname);
  217. SERIAL_PROTOCOLLNPGM(".");
  218. return;
  219. }
  220. else
  221. {
  222. //SERIAL_ECHOLN("dive ok");
  223. }
  224. curDir=&myDir;
  225. dirname_start=dirname_end+1;
  226. }
  227. else // the reminder after all /fsa/fdsa/ is the filename
  228. {
  229. fname=dirname_start;
  230. //SERIAL_ECHOLN("remaider");
  231. //SERIAL_ECHOLN(fname);
  232. break;
  233. }
  234. }
  235. }
  236. else //relative path
  237. {
  238. curDir=&workDir;
  239. }
  240. if(read)
  241. {
  242. if (file.open(curDir, fname, O_READ))
  243. {
  244. filesize = file.fileSize();
  245. SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
  246. SERIAL_PROTOCOL(fname);
  247. SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
  248. SERIAL_PROTOCOLLN(filesize);
  249. sdpos = 0;
  250. SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
  251. lcd_setstatus(fname);
  252. }
  253. else
  254. {
  255. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  256. SERIAL_PROTOCOL(fname);
  257. SERIAL_PROTOCOLLNPGM(".");
  258. }
  259. }
  260. else
  261. { //write
  262. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  263. {
  264. SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
  265. SERIAL_PROTOCOL(fname);
  266. SERIAL_PROTOCOLLNPGM(".");
  267. }
  268. else
  269. {
  270. saving = true;
  271. SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
  272. SERIAL_PROTOCOLLN(name);
  273. lcd_setstatus(fname);
  274. }
  275. }
  276. }
  277. void CardReader::removeFile(char* name)
  278. {
  279. if(!cardOK)
  280. return;
  281. file.close();
  282. sdprinting = false;
  283. SdFile myDir;
  284. curDir=&root;
  285. char *fname=name;
  286. char *dirname_start,*dirname_end;
  287. if(name[0]=='/')
  288. {
  289. dirname_start=strchr(name,'/')+1;
  290. while(dirname_start>0)
  291. {
  292. dirname_end=strchr(dirname_start,'/');
  293. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  294. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  295. if(dirname_end>0 && dirname_end>dirname_start)
  296. {
  297. char subdirname[13];
  298. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  299. subdirname[dirname_end-dirname_start]=0;
  300. SERIAL_ECHOLN(subdirname);
  301. if(!myDir.open(curDir,subdirname,O_READ))
  302. {
  303. SERIAL_PROTOCOLPGM("open failed, File: ");
  304. SERIAL_PROTOCOL(subdirname);
  305. SERIAL_PROTOCOLLNPGM(".");
  306. return;
  307. }
  308. else
  309. {
  310. //SERIAL_ECHOLN("dive ok");
  311. }
  312. curDir=&myDir;
  313. dirname_start=dirname_end+1;
  314. }
  315. else // the reminder after all /fsa/fdsa/ is the filename
  316. {
  317. fname=dirname_start;
  318. //SERIAL_ECHOLN("remaider");
  319. //SERIAL_ECHOLN(fname);
  320. break;
  321. }
  322. }
  323. }
  324. else //relative path
  325. {
  326. curDir=&workDir;
  327. }
  328. if (file.remove(curDir, fname))
  329. {
  330. SERIAL_PROTOCOLPGM("File deleted:");
  331. SERIAL_PROTOCOL(fname);
  332. sdpos = 0;
  333. }
  334. else
  335. {
  336. SERIAL_PROTOCOLPGM("Deletion failed, File: ");
  337. SERIAL_PROTOCOL(fname);
  338. SERIAL_PROTOCOLLNPGM(".");
  339. }
  340. }
  341. void CardReader::getStatus()
  342. {
  343. if(cardOK){
  344. SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
  345. SERIAL_PROTOCOL(sdpos);
  346. SERIAL_PROTOCOLPGM("/");
  347. SERIAL_PROTOCOLLN(filesize);
  348. }
  349. else{
  350. SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
  351. }
  352. }
  353. void CardReader::write_command(char *buf)
  354. {
  355. char* begin = buf;
  356. char* npos = 0;
  357. char* end = buf + strlen(buf) - 1;
  358. file.writeError = false;
  359. if((npos = strchr(buf, 'N')) != NULL)
  360. {
  361. begin = strchr(npos, ' ') + 1;
  362. end = strchr(npos, '*') - 1;
  363. }
  364. end[1] = '\r';
  365. end[2] = '\n';
  366. end[3] = '\0';
  367. file.write(begin);
  368. if (file.writeError)
  369. {
  370. SERIAL_ERROR_START;
  371. SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
  372. }
  373. }
  374. void CardReader::checkautostart(bool force)
  375. {
  376. if(!force)
  377. {
  378. if(!autostart_stilltocheck)
  379. return;
  380. if(autostart_atmillis<millis())
  381. return;
  382. }
  383. autostart_stilltocheck=false;
  384. if(!cardOK)
  385. {
  386. initsd();
  387. if(!cardOK) //fail
  388. return;
  389. }
  390. char autoname[30];
  391. sprintf_P(autoname, PSTR("auto%i.g"), lastnr);
  392. for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
  393. autoname[i]=tolower(autoname[i]);
  394. dir_t p;
  395. root.rewind();
  396. bool found=false;
  397. while (root.readDir(p, NULL) > 0)
  398. {
  399. for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
  400. p.name[i]=tolower(p.name[i]);
  401. //Serial.print((char*)p.name);
  402. //Serial.print(" ");
  403. //Serial.println(autoname);
  404. if(p.name[9]!='~') //skip safety copies
  405. if(strncmp((char*)p.name,autoname,5)==0)
  406. {
  407. char cmd[30];
  408. sprintf_P(cmd, PSTR("M23 %s"), autoname);
  409. enquecommand(cmd);
  410. enquecommand_P(PSTR("M24"));
  411. found=true;
  412. }
  413. }
  414. if(!found)
  415. lastnr=-1;
  416. else
  417. lastnr++;
  418. }
  419. void CardReader::closefile()
  420. {
  421. file.sync();
  422. file.close();
  423. saving = false;
  424. logging = false;
  425. }
  426. void CardReader::getfilename(const uint8_t nr)
  427. {
  428. curDir=&workDir;
  429. lsAction=LS_GetFilename;
  430. nrFiles=nr;
  431. curDir->rewind();
  432. lsDive("",*curDir);
  433. }
  434. uint16_t CardReader::getnrfilenames()
  435. {
  436. curDir=&workDir;
  437. lsAction=LS_Count;
  438. nrFiles=0;
  439. curDir->rewind();
  440. lsDive("",*curDir);
  441. //SERIAL_ECHOLN(nrFiles);
  442. return nrFiles;
  443. }
  444. void CardReader::chdir(const char * relpath)
  445. {
  446. SdFile newfile;
  447. SdFile *parent=&root;
  448. if(workDir.isOpen())
  449. parent=&workDir;
  450. if(!newfile.open(*parent,relpath, O_READ))
  451. {
  452. SERIAL_ECHO_START;
  453. SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
  454. SERIAL_ECHOLN(relpath);
  455. }
  456. else
  457. {
  458. workDirParentParent=workDirParent;
  459. workDirParent=*parent;
  460. workDir=newfile;
  461. }
  462. }
  463. void CardReader::updir()
  464. {
  465. if(!workDir.isRoot())
  466. {
  467. workDir=workDirParent;
  468. workDirParent=workDirParentParent;
  469. }
  470. }
  471. void CardReader::printingHasFinished()
  472. {
  473. st_synchronize();
  474. quickStop();
  475. file.close();
  476. sdprinting = false;
  477. if(SD_FINISHED_STEPPERRELEASE)
  478. {
  479. //finishAndDisableSteppers();
  480. enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
  481. }
  482. autotempShutdown();
  483. }
  484. #endif //SDSUPPORT