My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cardreader.cpp 11KB

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