My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cardreader.pde 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. void CardReader::initsd()
  20. {
  21. cardOK = false;
  22. #if SDSS >- 1
  23. if(root.isOpen())
  24. root.close();
  25. if (!card.init(SPI_FULL_SPEED,SDSS))
  26. {
  27. //if (!card.init(SPI_HALF_SPEED,SDSS))
  28. SERIAL_ECHOLN("SD init fail");
  29. }
  30. else if (!volume.init(&card))
  31. {
  32. SERIAL_ERRORLN("volume.init failed");
  33. }
  34. else if (!root.openRoot(&volume))
  35. {
  36. SERIAL_ERRORLN("openRoot failed");
  37. }
  38. else
  39. {
  40. cardOK = true;
  41. SERIAL_ECHOLN("SD card ok");
  42. }
  43. #endif //SDSS
  44. }
  45. void CardReader::release()
  46. {
  47. sdprinting = false;
  48. cardOK = false;
  49. }
  50. void CardReader::startFileprint()
  51. {
  52. if(cardOK)
  53. {
  54. sdprinting = true;
  55. }
  56. }
  57. void CardReader::pauseSDPrint()
  58. {
  59. if(sdprinting)
  60. {
  61. sdprinting = false;
  62. }
  63. }
  64. void CardReader::selectFile(char* name)
  65. {
  66. if(cardOK){
  67. sdprinting = false;
  68. file.close();
  69. if (file.open(&root, name, O_READ)) {
  70. filesize = file.fileSize();
  71. SERIAL_PROTOCOLLN("File opened:"<<name<<" Size:"<<filesize);
  72. sdpos = 0;
  73. SERIAL_PROTOCOLLN("File selected");
  74. }
  75. else{
  76. SERIAL_PROTOCOLLN("file.open failed");
  77. }
  78. }
  79. }
  80. void CardReader::startFilewrite(char *name)
  81. {
  82. if(cardOK)
  83. {
  84. file.close();
  85. sdprinting = false;
  86. if (!file.open(&root, name, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
  87. {
  88. SERIAL_PROTOCOLLN("open failed, File: "<<name<<".");
  89. }
  90. else{
  91. saving = true;
  92. SERIAL_PROTOCOLLN("Writing to file: "<<name);
  93. }
  94. }
  95. }
  96. void CardReader::getStatus()
  97. {
  98. if(cardOK){
  99. SERIAL_PROTOCOLLN("SD printing byte "<<sdpos<<"/"<<filesize);
  100. }
  101. else{
  102. SERIAL_PROTOCOLLN("Not SD printing");
  103. }
  104. }
  105. void CardReader::write_command(char *buf)
  106. {
  107. char* begin = buf;
  108. char* npos = 0;
  109. char* end = buf + strlen(buf) - 1;
  110. file.writeError = false;
  111. if((npos = strchr(buf, 'N')) != NULL)
  112. {
  113. begin = strchr(npos, ' ') + 1;
  114. end = strchr(npos, '*') - 1;
  115. }
  116. end[1] = '\r';
  117. end[2] = '\n';
  118. end[3] = '\0';
  119. file.write(begin);
  120. if (file.writeError)
  121. {
  122. SERIAL_ERRORLN("error writing to file");
  123. }
  124. }
  125. void CardReader::checkautostart(bool force)
  126. {
  127. if(!force)
  128. {
  129. if(!autostart_stilltocheck)
  130. return;
  131. if(autostart_atmillis<millis())
  132. return;
  133. }
  134. autostart_stilltocheck=false;
  135. if(!cardOK)
  136. {
  137. initsd();
  138. if(!cardOK) //fail
  139. return;
  140. }
  141. static int lastnr=0;
  142. char autoname[30];
  143. sprintf(autoname,"auto%i.g",lastnr);
  144. for(int8_t i=0;i<(int)strlen(autoname);i++)
  145. autoname[i]=tolower(autoname[i]);
  146. dir_t p;
  147. root.rewind();
  148. bool found=false;
  149. while (root.readDir(p) > 0)
  150. {
  151. for(int8_t i=0;i<(int)strlen((char*)p.name);i++)
  152. p.name[i]=tolower(p.name[i]);
  153. //Serial.print((char*)p.name);
  154. //Serial.print(" ");
  155. //Serial.println(autoname);
  156. if(p.name[9]!='~') //skip safety copies
  157. if(strncmp((char*)p.name,autoname,5)==0)
  158. {
  159. char cmd[30];
  160. sprintf(cmd,"M23 %s",autoname);
  161. enquecommand(cmd);
  162. enquecommand("M24");
  163. found=true;
  164. }
  165. }
  166. if(!found)
  167. lastnr=-1;
  168. else
  169. lastnr++;
  170. }
  171. void CardReader::closefile()
  172. {
  173. file.sync();
  174. file.close();
  175. saving = false;
  176. }
  177. void CardReader::getfilename(const uint8_t nr)
  178. {
  179. dir_t p;
  180. root.rewind();
  181. uint8_t cnt=0;
  182. filename[0]='\0';
  183. while (root.readDir(p) > 0)
  184. {
  185. if (p.name[0] == DIR_NAME_FREE) break;
  186. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  187. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  188. if(p.name[8]!='G') continue;
  189. if(p.name[9]=='~') continue;
  190. if(cnt++!=nr) continue;
  191. //Serial.println((char*)p.name);
  192. uint8_t writepos=0;
  193. for (int8_t i = 0; i < 11; i++)
  194. {
  195. if (p.name[i] == ' ') continue;
  196. if (i == 8) {
  197. filename[writepos++]='.';
  198. }
  199. filename[writepos++]=p.name[i];
  200. }
  201. filename[writepos++]=0;
  202. }
  203. }
  204. uint8_t CardReader::getnrfilenames()
  205. {
  206. dir_t p;
  207. root.rewind();
  208. uint8_t cnt=0;
  209. while (root.readDir(p) > 0)
  210. {
  211. if (p.name[0] == DIR_NAME_FREE) break;
  212. if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.'|| p.name[0] == '_') continue;
  213. if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
  214. if(p.name[8]!='G') continue;
  215. if(p.name[9]=='~') continue;
  216. cnt++;
  217. }
  218. return cnt;
  219. }
  220. #endif //SDSUPPORT