My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cardreader.cpp 10KB

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