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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include "cardreader.h"
  2. //#include <unistd.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. //power to SD reader
  14. #if SDPOWER > -1
  15. SET_OUTPUT(SDPOWER);
  16. WRITE(SDPOWER,HIGH);
  17. #endif //SDPOWER
  18. autostart_atmillis=millis()+5000;
  19. }
  20. char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
  21. {
  22. char *pos=buffer;
  23. for (uint8_t i = 0; i < 11; i++)
  24. {
  25. if (p.name[i] == ' ')continue;
  26. if (i == 8)
  27. {
  28. *pos++='.';
  29. }
  30. *pos++=p.name[i];
  31. }
  32. *pos++=0;
  33. return buffer;
  34. }
  35. void CardReader::lsDive(char *prepend,SdFile parent)
  36. {
  37. dir_t p;
  38. uint8_t cnt=0;
  39. while (parent.readDir(p) > 0)
  40. {
  41. if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename)
  42. {
  43. char path[13*2];
  44. char lfilename[13];
  45. createFilename(lfilename,p);
  46. path[0]=0;
  47. if(strlen(prepend)==0) //avoid leading / if already in prepend
  48. {
  49. strcat(path,"/");
  50. }
  51. strcat(path,prepend);
  52. strcat(path,lfilename);
  53. strcat(path,"/");
  54. //Serial.print(path);
  55. SdFile dir;
  56. if(!dir.open(parent,lfilename, O_READ))
  57. {
  58. if(lsAction==LS_SerialPrint)
  59. {
  60. SERIAL_ECHO_START;
  61. SERIAL_ECHOLN("Cannot open subdir");
  62. SERIAL_ECHOLN(lfilename);
  63. }
  64. }
  65. lsDive(path,dir);
  66. //close done automatically by destructor of SdFile
  67. }
  68. else
  69. {
  70. if (p.name[0] == DIR_NAME_FREE) break;
  71. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  72. if ( p.name[0] == '.')
  73. {
  74. if ( p.name[1] != '.')
  75. continue;
  76. }
  77. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  78. filenameIsDir=DIR_IS_SUBDIR(&p);
  79. if(!filenameIsDir)
  80. {
  81. if(p.name[8]!='G') continue;
  82. if(p.name[9]=='~') continue;
  83. }
  84. //if(cnt++!=nr) continue;
  85. createFilename(filename,p);
  86. if(lsAction==LS_SerialPrint)
  87. {
  88. SERIAL_PROTOCOL(prepend);
  89. SERIAL_PROTOCOLLN(filename);
  90. }
  91. else if(lsAction==LS_Count)
  92. {
  93. nrFiles++;
  94. }
  95. else if(lsAction==LS_GetFilename)
  96. {
  97. if(cnt==nrFiles)
  98. return;
  99. cnt++;
  100. }
  101. }
  102. }
  103. }
  104. void CardReader::ls()
  105. {
  106. lsAction=LS_SerialPrint;
  107. if(lsAction==LS_Count)
  108. nrFiles=0;
  109. root.rewind();
  110. lsDive("",root);
  111. }
  112. void CardReader::initsd()
  113. {
  114. cardOK = false;
  115. if(root.isOpen())
  116. root.close();
  117. if (!card.init(SPI_FULL_SPEED,SDSS))
  118. {
  119. //if (!card.init(SPI_HALF_SPEED,SDSS))
  120. SERIAL_ECHO_START;
  121. SERIAL_ECHOLNPGM("SD init fail");
  122. }
  123. else if (!volume.init(&card))
  124. {
  125. SERIAL_ERROR_START;
  126. SERIAL_ERRORLNPGM("volume.init failed");
  127. }
  128. else if (!root.openRoot(&volume))
  129. {
  130. SERIAL_ERROR_START;
  131. SERIAL_ERRORLNPGM("openRoot failed");
  132. }
  133. else
  134. {
  135. cardOK = true;
  136. SERIAL_ECHO_START;
  137. SERIAL_ECHOLNPGM("SD card ok");
  138. }
  139. curDir=&root;
  140. if(!workDir.openRoot(&volume))
  141. {
  142. SERIAL_ECHOLNPGM("workDir open failed");
  143. }
  144. }
  145. void CardReader::release()
  146. {
  147. sdprinting = false;
  148. cardOK = false;
  149. }
  150. void CardReader::startFileprint()
  151. {
  152. if(cardOK)
  153. {
  154. sdprinting = true;
  155. }
  156. }
  157. void CardReader::pauseSDPrint()
  158. {
  159. if(sdprinting)
  160. {
  161. sdprinting = false;
  162. }
  163. }
  164. void CardReader::openFile(char* name,bool read)
  165. {
  166. if(!cardOK)
  167. return;
  168. file.close();
  169. sdprinting = false;
  170. SdFile myDir;
  171. curDir=&root;
  172. char *fname=name;
  173. char *dirname_start,*dirname_end;
  174. if(name[0]=='/')
  175. {
  176. dirname_start=strchr(name,'/')+1;
  177. while(dirname_start>0)
  178. {
  179. dirname_end=strchr(dirname_start,'/');
  180. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  181. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  182. if(dirname_end>0 && dirname_end>dirname_start)
  183. {
  184. char subdirname[13];
  185. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  186. subdirname[dirname_end-dirname_start]=0;
  187. SERIAL_ECHOLN(subdirname);
  188. if(!myDir.open(curDir,subdirname,O_READ))
  189. {
  190. SERIAL_PROTOCOLPGM("open failed, File: ");
  191. SERIAL_PROTOCOL(subdirname);
  192. SERIAL_PROTOCOLLNPGM(".");
  193. return;
  194. }
  195. else
  196. ;//SERIAL_ECHOLN("dive ok");
  197. curDir=&myDir;
  198. dirname_start=dirname_end+1;
  199. }
  200. else // the reminder after all /fsa/fdsa/ is the filename
  201. {
  202. fname=dirname_start;
  203. //SERIAL_ECHOLN("remaider");
  204. //SERIAL_ECHOLN(fname);
  205. break;
  206. }
  207. }
  208. }
  209. else //relative path
  210. {
  211. curDir=&workDir;
  212. }
  213. if(read)
  214. {
  215. if (file.open(curDir, fname, O_READ))
  216. {
  217. filesize = file.fileSize();
  218. SERIAL_PROTOCOLPGM("File opened:");
  219. SERIAL_PROTOCOL(fname);
  220. SERIAL_PROTOCOLPGM(" Size:");
  221. SERIAL_PROTOCOLLN(filesize);
  222. sdpos = 0;
  223. SERIAL_PROTOCOLLNPGM("File selected");
  224. LCD_MESSAGE(fname);
  225. }
  226. else
  227. {
  228. SERIAL_PROTOCOLPGM("open failed, File: ");
  229. SERIAL_PROTOCOL(fname);
  230. SERIAL_PROTOCOLLNPGM(".");
  231. }
  232. }
  233. else
  234. { //write
  235. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  236. {
  237. SERIAL_PROTOCOLPGM("open failed, File: ");
  238. SERIAL_PROTOCOL(fname);
  239. SERIAL_PROTOCOLLNPGM(".");
  240. }
  241. else
  242. {
  243. saving = true;
  244. SERIAL_PROTOCOLPGM("Writing to file: ");
  245. SERIAL_PROTOCOLLN(name);
  246. LCD_MESSAGE(fname);
  247. }
  248. }
  249. }
  250. void CardReader::getStatus()
  251. {
  252. if(cardOK){
  253. SERIAL_PROTOCOLPGM("SD printing byte ");
  254. SERIAL_PROTOCOL(sdpos);
  255. SERIAL_PROTOCOLPGM("/");
  256. SERIAL_PROTOCOLLN(filesize);
  257. }
  258. else{
  259. SERIAL_PROTOCOLLNPGM("Not SD printing");
  260. }
  261. }
  262. void CardReader::write_command(char *buf)
  263. {
  264. char* begin = buf;
  265. char* npos = 0;
  266. char* end = buf + strlen(buf) - 1;
  267. file.writeError = false;
  268. if((npos = strchr(buf, 'N')) != NULL)
  269. {
  270. begin = strchr(npos, ' ') + 1;
  271. end = strchr(npos, '*') - 1;
  272. }
  273. end[1] = '\r';
  274. end[2] = '\n';
  275. end[3] = '\0';
  276. file.write(begin);
  277. if (file.writeError)
  278. {
  279. SERIAL_ERROR_START;
  280. SERIAL_ERRORLNPGM("error writing to file");
  281. }
  282. }
  283. void CardReader::checkautostart(bool force)
  284. {
  285. if(!force)
  286. {
  287. if(!autostart_stilltocheck)
  288. return;
  289. if(autostart_atmillis<millis())
  290. return;
  291. }
  292. autostart_stilltocheck=false;
  293. if(!cardOK)
  294. {
  295. initsd();
  296. if(!cardOK) //fail
  297. return;
  298. }
  299. static int lastnr=0;
  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. #endif //SDSUPPORT