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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #include "cardreader.h"
  2. #ifdef SDSUPPORT
  3. #include "Marlin.h"
  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::release()
  147. {
  148. sdprinting = false;
  149. cardOK = false;
  150. }
  151. void CardReader::startFileprint()
  152. {
  153. if(cardOK)
  154. {
  155. sdprinting = true;
  156. }
  157. }
  158. void CardReader::pauseSDPrint()
  159. {
  160. if(sdprinting)
  161. {
  162. sdprinting = false;
  163. }
  164. }
  165. void CardReader::openFile(char* name,bool read)
  166. {
  167. if(!cardOK)
  168. return;
  169. file.close();
  170. sdprinting = false;
  171. SdFile myDir;
  172. curDir=&root;
  173. char *fname=name;
  174. char *dirname_start,*dirname_end;
  175. if(name[0]=='/')
  176. {
  177. dirname_start=strchr(name,'/')+1;
  178. while(dirname_start>0)
  179. {
  180. dirname_end=strchr(dirname_start,'/');
  181. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  182. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  183. if(dirname_end>0 && dirname_end>dirname_start)
  184. {
  185. char subdirname[13];
  186. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  187. subdirname[dirname_end-dirname_start]=0;
  188. SERIAL_ECHOLN(subdirname);
  189. if(!myDir.open(curDir,subdirname,O_READ))
  190. {
  191. SERIAL_PROTOCOLPGM("open failed, File: ");
  192. SERIAL_PROTOCOL(subdirname);
  193. SERIAL_PROTOCOLLNPGM(".");
  194. return;
  195. }
  196. else
  197. ;//SERIAL_ECHOLN("dive ok");
  198. curDir=&myDir;
  199. dirname_start=dirname_end+1;
  200. }
  201. else // the reminder after all /fsa/fdsa/ is the filename
  202. {
  203. fname=dirname_start;
  204. //SERIAL_ECHOLN("remaider");
  205. //SERIAL_ECHOLN(fname);
  206. break;
  207. }
  208. }
  209. }
  210. else //relative path
  211. {
  212. curDir=&workDir;
  213. }
  214. if(read)
  215. {
  216. if (file.open(curDir, fname, O_READ))
  217. {
  218. filesize = file.fileSize();
  219. SERIAL_PROTOCOLPGM("File opened:");
  220. SERIAL_PROTOCOL(fname);
  221. SERIAL_PROTOCOLPGM(" Size:");
  222. SERIAL_PROTOCOLLN(filesize);
  223. sdpos = 0;
  224. SERIAL_PROTOCOLLNPGM("File selected");
  225. LCD_MESSAGE(fname);
  226. }
  227. else
  228. {
  229. SERIAL_PROTOCOLPGM("open failed, File: ");
  230. SERIAL_PROTOCOL(fname);
  231. SERIAL_PROTOCOLLNPGM(".");
  232. }
  233. }
  234. else
  235. { //write
  236. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  237. {
  238. SERIAL_PROTOCOLPGM("open failed, File: ");
  239. SERIAL_PROTOCOL(fname);
  240. SERIAL_PROTOCOLLNPGM(".");
  241. }
  242. else
  243. {
  244. saving = true;
  245. SERIAL_PROTOCOLPGM("Writing to file: ");
  246. SERIAL_PROTOCOLLN(name);
  247. LCD_MESSAGE(fname);
  248. }
  249. }
  250. }
  251. void CardReader::getStatus()
  252. {
  253. if(cardOK){
  254. SERIAL_PROTOCOLPGM("SD printing byte ");
  255. SERIAL_PROTOCOL(sdpos);
  256. SERIAL_PROTOCOLPGM("/");
  257. SERIAL_PROTOCOLLN(filesize);
  258. }
  259. else{
  260. SERIAL_PROTOCOLLNPGM("Not SD printing");
  261. }
  262. }
  263. void CardReader::write_command(char *buf)
  264. {
  265. char* begin = buf;
  266. char* npos = 0;
  267. char* end = buf + strlen(buf) - 1;
  268. file.writeError = false;
  269. if((npos = strchr(buf, 'N')) != NULL)
  270. {
  271. begin = strchr(npos, ' ') + 1;
  272. end = strchr(npos, '*') - 1;
  273. }
  274. end[1] = '\r';
  275. end[2] = '\n';
  276. end[3] = '\0';
  277. file.write(begin);
  278. if (file.writeError)
  279. {
  280. SERIAL_ERROR_START;
  281. SERIAL_ERRORLNPGM("error writing to file");
  282. }
  283. }
  284. void CardReader::checkautostart(bool force)
  285. {
  286. if(!force)
  287. {
  288. if(!autostart_stilltocheck)
  289. return;
  290. if(autostart_atmillis<millis())
  291. return;
  292. }
  293. autostart_stilltocheck=false;
  294. if(!cardOK)
  295. {
  296. initsd();
  297. if(!cardOK) //fail
  298. return;
  299. }
  300. char autoname[30];
  301. sprintf(autoname,"auto%i.g",lastnr);
  302. for(int8_t i=0;i<(int)strlen(autoname);i++)
  303. autoname[i]=tolower(autoname[i]);
  304. dir_t p;
  305. root.rewind();
  306. bool found=false;
  307. while (root.readDir(p) > 0)
  308. {
  309. for(int8_t i=0;i<(int)strlen((char*)p.name);i++)
  310. p.name[i]=tolower(p.name[i]);
  311. //Serial.print((char*)p.name);
  312. //Serial.print(" ");
  313. //Serial.println(autoname);
  314. if(p.name[9]!='~') //skip safety copies
  315. if(strncmp((char*)p.name,autoname,5)==0)
  316. {
  317. char cmd[30];
  318. sprintf(cmd,"M23 %s",autoname);
  319. enquecommand(cmd);
  320. enquecommand("M24");
  321. found=true;
  322. }
  323. }
  324. if(!found)
  325. lastnr=-1;
  326. else
  327. lastnr++;
  328. }
  329. void CardReader::closefile()
  330. {
  331. file.sync();
  332. file.close();
  333. saving = false;
  334. }
  335. void CardReader::getfilename(const uint8_t nr)
  336. {
  337. curDir=&workDir;
  338. lsAction=LS_GetFilename;
  339. nrFiles=nr;
  340. curDir->rewind();
  341. lsDive("",*curDir);
  342. }
  343. uint16_t CardReader::getnrfilenames()
  344. {
  345. curDir=&workDir;
  346. lsAction=LS_Count;
  347. nrFiles=0;
  348. curDir->rewind();
  349. lsDive("",*curDir);
  350. //SERIAL_ECHOLN(nrFiles);
  351. return nrFiles;
  352. }
  353. void CardReader::chdir(const char * relpath)
  354. {
  355. SdFile newfile;
  356. SdFile *parent=&root;
  357. if(workDir.isOpen())
  358. parent=&workDir;
  359. if(!newfile.open(*parent,relpath, O_READ))
  360. {
  361. SERIAL_ECHO_START;
  362. SERIAL_ECHOPGM("Cannot enter subdir:");
  363. SERIAL_ECHOLN(relpath);
  364. }
  365. else
  366. {
  367. workDirParentParent=workDirParent;
  368. workDirParent=*parent;
  369. workDir=newfile;
  370. }
  371. }
  372. void CardReader::updir()
  373. {
  374. if(!workDir.isRoot())
  375. {
  376. workDir=workDirParent;
  377. workDirParent=workDirParentParent;
  378. }
  379. }
  380. void CardReader::printingHasFinished()
  381. {
  382. st_synchronize();
  383. quickStop();
  384. sdprinting = false;
  385. stop_heating_wait=true;
  386. if(SD_FINISHED_STEPPERRELEASE)
  387. {
  388. //finishAndDisableSteppers();
  389. enquecommand(SD_FINISHED_RELEASECOMMAND);
  390. }
  391. autotempShutdown();
  392. }
  393. #endif //SDSUPPORT