My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cardreader.pde 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. if(name[0]=='/')
  168. {
  169. dirname_start=strchr(name,'/')+1;
  170. while(dirname_start>0)
  171. {
  172. dirname_end=strchr(dirname_start,'/');
  173. //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
  174. //SERIAL_ECHO("end :");SERIAL_ECHOLN((int)(dirname_end-name));
  175. if(dirname_end>0 && dirname_end>dirname_start)
  176. {
  177. char subdirname[13];
  178. strncpy(subdirname, dirname_start, dirname_end-dirname_start);
  179. subdirname[dirname_end-dirname_start]=0;
  180. SERIAL_ECHOLN(subdirname);
  181. if(!myDir.open(curDir,subdirname,O_READ))
  182. {
  183. SERIAL_PROTOCOLPGM("open failed, File: ");
  184. SERIAL_PROTOCOL(subdirname);
  185. SERIAL_PROTOCOLLNPGM(".");
  186. return;
  187. }
  188. else
  189. ;//SERIAL_ECHOLN("dive ok");
  190. curDir=&myDir;
  191. dirname_start=dirname_end+1;
  192. }
  193. else // the reminder after all /fsa/fdsa/ is the filename
  194. {
  195. fname=dirname_start;
  196. //SERIAL_ECHOLN("remaider");
  197. //SERIAL_ECHOLN(fname);
  198. break;
  199. }
  200. }
  201. }
  202. if(read)
  203. {
  204. if (file.open(curDir, fname, O_READ))
  205. {
  206. filesize = file.fileSize();
  207. SERIAL_PROTOCOLPGM("File opened:");
  208. SERIAL_PROTOCOL(fname);
  209. SERIAL_PROTOCOLPGM(" Size:");
  210. SERIAL_PROTOCOLLN(filesize);
  211. sdpos = 0;
  212. SERIAL_PROTOCOLLNPGM("File selected");
  213. }
  214. else
  215. {
  216. SERIAL_PROTOCOLPGM("open failed, File: ");
  217. SERIAL_PROTOCOL(fname);
  218. SERIAL_PROTOCOLLNPGM(".");
  219. }
  220. }
  221. else
  222. { //write
  223. if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  224. {
  225. SERIAL_PROTOCOLPGM("open failed, File: ");
  226. SERIAL_PROTOCOL(fname);
  227. SERIAL_PROTOCOLLNPGM(".");
  228. }
  229. else
  230. {
  231. saving = true;
  232. SERIAL_PROTOCOLPGM("Writing to file: ");
  233. SERIAL_PROTOCOLLN(name);
  234. }
  235. }
  236. }
  237. void CardReader::getStatus()
  238. {
  239. if(cardOK){
  240. SERIAL_PROTOCOLPGM("SD printing byte ");
  241. SERIAL_PROTOCOL(sdpos);
  242. SERIAL_PROTOCOLPGM("/");
  243. SERIAL_PROTOCOLLN(filesize);
  244. }
  245. else{
  246. SERIAL_PROTOCOLLNPGM("Not SD printing");
  247. }
  248. }
  249. void CardReader::write_command(char *buf)
  250. {
  251. char* begin = buf;
  252. char* npos = 0;
  253. char* end = buf + strlen(buf) - 1;
  254. file.writeError = false;
  255. if((npos = strchr(buf, 'N')) != NULL)
  256. {
  257. begin = strchr(npos, ' ') + 1;
  258. end = strchr(npos, '*') - 1;
  259. }
  260. end[1] = '\r';
  261. end[2] = '\n';
  262. end[3] = '\0';
  263. file.write(begin);
  264. if (file.writeError)
  265. {
  266. SERIAL_ERROR_START;
  267. SERIAL_ERRORLNPGM("error writing to file");
  268. }
  269. }
  270. void CardReader::checkautostart(bool force)
  271. {
  272. if(!force)
  273. {
  274. if(!autostart_stilltocheck)
  275. return;
  276. if(autostart_atmillis<millis())
  277. return;
  278. }
  279. autostart_stilltocheck=false;
  280. if(!cardOK)
  281. {
  282. initsd();
  283. if(!cardOK) //fail
  284. return;
  285. }
  286. static int lastnr=0;
  287. char autoname[30];
  288. sprintf(autoname,"auto%i.g",lastnr);
  289. for(int8_t i=0;i<(int)strlen(autoname);i++)
  290. autoname[i]=tolower(autoname[i]);
  291. dir_t p;
  292. root.rewind();
  293. bool found=false;
  294. while (root.readDir(p) > 0)
  295. {
  296. for(int8_t i=0;i<(int)strlen((char*)p.name);i++)
  297. p.name[i]=tolower(p.name[i]);
  298. //Serial.print((char*)p.name);
  299. //Serial.print(" ");
  300. //Serial.println(autoname);
  301. if(p.name[9]!='~') //skip safety copies
  302. if(strncmp((char*)p.name,autoname,5)==0)
  303. {
  304. char cmd[30];
  305. sprintf(cmd,"M23 %s",autoname);
  306. enquecommand(cmd);
  307. enquecommand("M24");
  308. found=true;
  309. }
  310. }
  311. if(!found)
  312. lastnr=-1;
  313. else
  314. lastnr++;
  315. }
  316. void CardReader::closefile()
  317. {
  318. file.sync();
  319. file.close();
  320. saving = false;
  321. }
  322. void CardReader::getfilename(const uint8_t nr)
  323. {
  324. lsAction=LS_GetFilename;
  325. nrFiles=nr;
  326. curDir->rewind();
  327. lsDive("",*curDir);
  328. }
  329. uint16_t CardReader::getnrfilenames()
  330. {
  331. lsAction=LS_Count;
  332. nrFiles=0;
  333. curDir->rewind();
  334. lsDive("",*curDir);
  335. return nrFiles;
  336. }
  337. void CardReader::cd(char * absolutPath)
  338. {
  339. }
  340. #endif //SDSUPPORT