My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

cardreader.cpp 8.7KB

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