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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. 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 (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  75. if(p.name[8]!='G') continue;
  76. if(p.name[9]=='~') continue;
  77. //if(cnt++!=nr) continue;
  78. createFilename(filename,p);
  79. if(lsAction==LS_SerialPrint)
  80. {
  81. SERIAL_PROTOCOL(prepend);
  82. SERIAL_PROTOCOLLN(filename);
  83. }
  84. else if(lsAction==LS_Count)
  85. {
  86. nrFiles++;
  87. }
  88. else if(lsAction==LS_GetFilename)
  89. {
  90. if(cnt==nrFiles)
  91. return;
  92. cnt++;
  93. }
  94. }
  95. }
  96. void CardReader::ls()
  97. {
  98. lsAction=LS_SerialPrint;
  99. if(lsAction==LS_Count)
  100. nrFiles=0;
  101. root.rewind();
  102. lsDive("",root);
  103. }
  104. void CardReader::initsd()
  105. {
  106. cardOK = false;
  107. #if SDSS >- 1
  108. if(root.isOpen())
  109. root.close();
  110. if (!card.init(SPI_FULL_SPEED,SDSS))
  111. {
  112. //if (!card.init(SPI_HALF_SPEED,SDSS))
  113. SERIAL_ECHO_START;
  114. SERIAL_ECHOLNPGM("SD init fail");
  115. }
  116. else if (!volume.init(&card))
  117. {
  118. SERIAL_ERROR_START;
  119. SERIAL_ERRORLNPGM("volume.init failed");
  120. }
  121. else if (!root.openRoot(&volume))
  122. {
  123. SERIAL_ERROR_START;
  124. SERIAL_ERRORLNPGM("openRoot failed");
  125. }
  126. else
  127. {
  128. cardOK = true;
  129. SERIAL_ECHO_START;
  130. SERIAL_ECHOLNPGM("SD card ok");
  131. }
  132. curDir=&root;
  133. #endif //SDSS
  134. }
  135. void CardReader::release()
  136. {
  137. sdprinting = false;
  138. cardOK = false;
  139. }
  140. void CardReader::startFileprint()
  141. {
  142. if(cardOK)
  143. {
  144. sdprinting = true;
  145. }
  146. }
  147. void CardReader::pauseSDPrint()
  148. {
  149. if(sdprinting)
  150. {
  151. sdprinting = false;
  152. }
  153. }
  154. void CardReader::openFile(char* name,bool read)
  155. {
  156. if(!cardOK)
  157. return;
  158. file.close();
  159. sdprinting = false;
  160. if(read)
  161. {
  162. if (file.open(&root, name, O_READ))
  163. {
  164. filesize = file.fileSize();
  165. SERIAL_PROTOCOLPGM("File opened:");
  166. SERIAL_PROTOCOL(name);
  167. SERIAL_PROTOCOLPGM(" Size:");
  168. SERIAL_PROTOCOLLN(filesize);
  169. sdpos = 0;
  170. SERIAL_PROTOCOLLNPGM("File selected");
  171. }
  172. else
  173. {
  174. SERIAL_PROTOCOLLNPGM("file.open failed");
  175. }
  176. }
  177. else
  178. { //write
  179. if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  180. {
  181. SERIAL_PROTOCOLPGM("open failed, File: ");
  182. SERIAL_PROTOCOL(name);
  183. SERIAL_PROTOCOLLNPGM(".");
  184. }
  185. else
  186. {
  187. saving = true;
  188. SERIAL_PROTOCOLPGM("Writing to file: ");
  189. SERIAL_PROTOCOLLN(name);
  190. }
  191. }
  192. }
  193. void CardReader::getStatus()
  194. {
  195. if(cardOK){
  196. SERIAL_PROTOCOLPGM("SD printing byte ");
  197. SERIAL_PROTOCOL(sdpos);
  198. SERIAL_PROTOCOLPGM("/");
  199. SERIAL_PROTOCOLLN(filesize);
  200. }
  201. else{
  202. SERIAL_PROTOCOLLNPGM("Not SD printing");
  203. }
  204. }
  205. void CardReader::write_command(char *buf)
  206. {
  207. char* begin = buf;
  208. char* npos = 0;
  209. char* end = buf + strlen(buf) - 1;
  210. file.writeError = false;
  211. if((npos = strchr(buf, 'N')) != NULL)
  212. {
  213. begin = strchr(npos, ' ') + 1;
  214. end = strchr(npos, '*') - 1;
  215. }
  216. end[1] = '\r';
  217. end[2] = '\n';
  218. end[3] = '\0';
  219. file.write(begin);
  220. if (file.writeError)
  221. {
  222. SERIAL_ERROR_START;
  223. SERIAL_ERRORLNPGM("error writing to file");
  224. }
  225. }
  226. void CardReader::checkautostart(bool force)
  227. {
  228. if(!force)
  229. {
  230. if(!autostart_stilltocheck)
  231. return;
  232. if(autostart_atmillis<millis())
  233. return;
  234. }
  235. autostart_stilltocheck=false;
  236. if(!cardOK)
  237. {
  238. initsd();
  239. if(!cardOK) //fail
  240. return;
  241. }
  242. static int lastnr=0;
  243. char autoname[30];
  244. sprintf(autoname,"auto%i.g",lastnr);
  245. for(int8_t i=0;i<(int)strlen(autoname);i++)
  246. autoname[i]=tolower(autoname[i]);
  247. dir_t p;
  248. root.rewind();
  249. bool found=false;
  250. while (root.readDir(p) > 0)
  251. {
  252. for(int8_t i=0;i<(int)strlen((char*)p.name);i++)
  253. p.name[i]=tolower(p.name[i]);
  254. //Serial.print((char*)p.name);
  255. //Serial.print(" ");
  256. //Serial.println(autoname);
  257. if(p.name[9]!='~') //skip safety copies
  258. if(strncmp((char*)p.name,autoname,5)==0)
  259. {
  260. char cmd[30];
  261. sprintf(cmd,"M23 %s",autoname);
  262. enquecommand(cmd);
  263. enquecommand("M24");
  264. found=true;
  265. }
  266. }
  267. if(!found)
  268. lastnr=-1;
  269. else
  270. lastnr++;
  271. }
  272. void CardReader::closefile()
  273. {
  274. file.sync();
  275. file.close();
  276. saving = false;
  277. }
  278. void CardReader::getfilename(const uint8_t nr)
  279. {
  280. lsAction=LS_GetFilename;
  281. nrFiles=nr;
  282. curDir->rewind();
  283. lsDive("",*curDir);
  284. }
  285. uint16_t CardReader::getnrfilenames()
  286. {
  287. lsAction=LS_Count;
  288. nrFiles=0;
  289. curDir->rewind();
  290. lsDive("",*curDir);
  291. return nrFiles;
  292. }
  293. void CardReader::cd(char * absolutPath)
  294. {
  295. }
  296. #endif //SDSUPPORT