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

cardreader.pde 8.5KB

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