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

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