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 7.4KB

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