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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. //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(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. static int lastnr=0;
  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. sdprinting = false;
  384. if(SD_FINISHED_STEPPERRELEASE)
  385. {
  386. finishAndDisableSteppers();
  387. }
  388. }
  389. #endif //SDSUPPORT