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.pde 8.6KB

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