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

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